/* preload background images */
try { document.execCommand("BackgroundImageCache", false, true) } catch (err) { }

// news ticker variables (LCN002)
var curNewsItem = 1;
var newsTrans = false;
var newsTimer;
var newsInterval = 7000;

// Homepage What's New flipper  variables (GCN001)
var curWNew = 1;
var whatsnewInterval = 7000;
var whatsnew_automatic = true;
var whatsnew_arrows = true;
var whatsnewTrans = false;

// success story flipper  variables (GCN007)
var curSStory = 1;
var successInterval = 7000;
var success_automatic = true;
var successTrans = false;
var successTimer;

// support team flipper  variables (LCN040]
var STMAX = 5; // max. number of support team members to display
var curTeamMember = 1;
var supportInterval = 7000;
var support_automatic = true;
var supportTrans = false;
var supportTimer;

// top navigation variables
var uptimer;
var cur = "";
var trans = false;

// header navigation variables
var headerUpTimer;
var headerCur = "";
var headerTrans = false;

// accordion variables
var accordionTrans = false;

$(document).ready(function() {

	//Google Maps api.  Function must be called whed document is ready.
	//Map Key,Function declaration, and address variables set in master page
	try { loadMap(); } catch (err) { }

	/*********************************GCN014L**************/
	var $support_numbers_list = $("#GCN014L > .selectDropdown > ul > li");
	if ($support_numbers_list.length > 1) {
		//enable dropdown if it contains multiple phone numbers
		$("#GCN014L > .supnumber").css({ 'display': 'none' });
		$("#GCN014L > .selectDropdown").css({ 'display': 'block' });
	}
	/*********************************GCN014L*****END*SECTION**********/

	/*********************************Homepage GCN001 (NEW) **************/

	// if we have multiple whats new items, setup the auto-flipper
	if ($("#whats_new").length) {
		if ($("#whats_new").find(".whats_new_item").length > 0) {
			var wniid = 0;
			$("#whats_new").find(".whats_new_item").each(function() {
				this.id = "whats_new_item_no" + wniid;
				$("#" + this.id).hide();
				wniid++;
			});

			$whatsnew_index = 0;
			var wnstr = "#whats_new_item_no" + $whatsnew_index;

			whatsnewTrans = true;
			$("#whats_new").fadeOut("fast", function() {
				$(wnstr).show();
				$("#whats_new").fadeIn("slow", function() { whatsnewTrans = false });
			});
			if (whatsnew_automatic) whatsnewTimer = setInterval("nextWNew();", whatsnewInterval);
		}
	}

	$("#wnal").bind("click", function(e) {
		whatsnew_automatic = false; // switch from automatic to manual
		if (!whatsnewTrans) prevWNew();
	});

	$("#wnar").bind("click", function(e) {
		whatsnew_automatic = false; // switch from automatic to manual
		if (!whatsnewTrans) nextWNew();
	});

	/*********************************GCN001 *****END*SECTION**********/

	/*********************************GCN007A**************/

	// if we have success stories, setup the auto-flipper
	//alert ('got here');
	if ($("#success_data").length) {
		//alert ("found the thing");
		if ($("#success_data").find("img").length > 0) {
			var ssid = 1;
			$("#success_data").find("img").each(function() {
				this.id = "success_pict" + ssid++;
			});
			ssid = 1;
			$("#success_data").find("div").each(function() {
				this.id = "success_div" + ssid++;
			});
			ssid = 1;
			$("#success_links").find("div").each(function() {
				this.id = "success_link" + ssid++;
			});
			
			$success_index = 0;
			successTrans = true;
			$("#success_pict").fadeOut("fast", function() {
				$img = $("#success_data > img").get($success_index);
				$("#success_pict").attr("src", $img.src);
				$("#success_pict").bind("click", function(e) {
					//success_automatic = false;
					if (!successTrans) {
						$sslink = $("#success_links > div").get($success_index);
						window.open( $($sslink).text() , "_blank");
					}
				});
				$("#success_pict").fadeIn("slow");
			});
			$("#content_window_content").fadeOut("fast", function() {
				$text_elem = $("#success_data > div").get($success_index);
				$text = $($text_elem).text();
				$("#content_window_content").text($text);
				$("#content_window_content").bind("click", function(e) {
					//success_automatic = false;
					if (!successTrans) {
						$sslink = $("#success_links > div").get($success_index);
						window.open( $($sslink).text() , "_blank");
					}
				});
				$("#content_window_content").fadeIn("slow", function() { successTrans = false });
			});

			if (success_automatic) successTimer = setInterval("nextSStory();", successInterval);
		} else {
			//alert ("didn't find the thing")
			//if container is empty, do not display
			$(".GCN007a").hide();
		}
	}

	$("#l_arrow").bind("click", function(e) {
		success_automatic = false; // switch from automatic to manual
		if (!successTrans) prevSStory();
	});

	$("#r_arrow").bind("click", function(e) {
		success_automatic = false; // switch from automatic to manual
		if (!successTrans) nextSStory();
	});

	/*********************************GCN007A*****END*SECTION**********/

	/*********************************LCN040**************/
	// LCN040 Meet Support Team container
	// 3.0.html support team select dropdown (OLD)
	if ($("#support_team_dropdown").length) {
		$("#support_team_dropdown ul li a").each(function() {
			$(this).click(function() {
				$("#support_team .teamMember").hide();
				$("#" + $(this).attr("title")).show();
			});
		});
	}
	
	// support team XML file (NEW)
	if ($("#LCN040").length) {
		var supportStaff = $.ajax
				({
					type: "GET",
					
					// xxx
					// change path 
					// this is the path to stylesheets, for reference: /stylesheets/www/ru_RU/main_patch.css
					//url: '/ru_RU/xml/www/support.xml', // this is the CMS pathname
					//url: 'http://appstg01:7088/xml/www/ru_RU/support.xml', // this is the pathname in the css-style reversed order
					//url: '/ru_RU/xml/www/ru_RU/support.xml',
					url: '/xml/www/spanish/support.xml',
					// end change path
					// xxx
					dataType: "text/xml",
					async: false,
					cache: false,
					success: function(msg) {
						//alert( "Data loaded: " + msg );
					}
				}).responseXML;
		var caIndex=0;
		var codeArray = new Array();
		$("staffMember", supportStaff).each(function() {
			codeArray[caIndex] = '<img src="' + $(this).find("imageSource").text() + '" /><span class="contentWindowName">' + $(this).find("name").text() + '</span><div>' + $(this).find("description").text() + '</div>'   ;
			caIndex++
		});
		function myrandomizer() {  return Math.round(Math.random())-0.5 ; };
		codeArray.sort( myrandomizer );
		var newSeed = codeArray.slice(0,STMAX);
		for (var rt=0; rt<STMAX;rt++) {
			$("#support_data").append( newSeed[rt] );
		}
		initLCN040();
		// still need to init display of first item
	/*
		var firstSupport = true;
		$("staffMember", supportStaff).each(function() {
													 
			if (firstSupport) {
				// fill the initial image and content
				$("#LCN040 #content_window img").attr("src", $(this).find("imageSource").text());
				$("#LCN040 #content_window_name").html($(this).find("name").text());
				$("#LCN040 #content_window_content").html($(this).find("description").text());
				firstSupport = false;
				return(false);
			}
			
			//$("#support_data").append('<img src="' + $(this).find("imageSource").text() + '" /><span class="contentWindowName">' + $(this).find("name").text() + '</span><div>' + $(this).find("description").text() + '</div>');
			
		});
		*/
	}

	/*********************************LCN040*****END*SECTION**********/

	/*********************************LCN085**************/

	var $ps_images = $("#GCN008 > img");
	var $ps_content = $("#GCN008 > div");

	var $pslength = $ps_images.length;
	var $i = 0;

	$("#LCN085 > div > .left_arrow").bind("click", function(e) {
		$i--;
		if ($i < 0) $i = $pslength - 1;

		$(".content_to_swap").empty();
		$(".image_to_swap").empty();

		$content_container = $(".content_to_swap").get(0);
		$img_container = $(".image_to_swap").get(0);

		$content_container.appendChild($ps_content.get($i));
		$img_container.appendChild($ps_images.get($i));

	});

	$("#LCN085 > div > .right_arrow").bind("click", function(e) {
		$i++;
		if ($i >= $pslength) $i = 0;

		$(".content_to_swap").empty();
		$(".image_to_swap").empty();

		$content_container = $(".content_to_swap").get(0);
		$img_container = $(".image_to_swap").get(0);

		$content_container.appendChild($ps_content.get($i));
		$img_container.appendChild($ps_images.get($i));
	});
	/*********************************JS Image Changer*****END*SECTION**********/
	/*********************************JS Image Changer******LCN085new**************/

	var $ps_images = $("#GCN008 > img");
	var $ps_content = $("#GCN008 > div");

	var $pslength = $ps_images.length;
	var $i = 0;

	$("#LCN085new > div > .left_arrow").bind("click", function(e) {
		$i--;
		if ($i < 0) $i = $pslength - 1;

		$(".content_to_swap").empty();
		$(".image_to_swap").empty();

		$content_container = $(".content_to_swap").get(0);
		$img_container = $(".image_to_swap").get(0);

		$content_container.appendChild($ps_content.get($i));
		$img_container.appendChild($ps_images.get($i));

	});

	$("#LCN085new > div > .right_arrow").bind("click", function(e) {
		$i++;
		if ($i >= $pslength) $i = 0;

		$(".content_to_swap").empty();
		$(".image_to_swap").empty();

		$content_container = $(".content_to_swap").get(0);
		$img_container = $(".image_to_swap").get(0);

		$content_container.appendChild($ps_content.get($i));
		$img_container.appendChild($ps_images.get($i));
	});
	/*********************************JS Image Changer*****END*SECTION**********/
	// hide the menus

	// setup accordion
	// show necessary inner thingies
	if ($(".accordionCloseButton").length) {
		$(".accordionCloseButton").parent().find(".accordionBucketInnerContent").css("display", "block");
	}
	if ($(".accordionContainer").length) {
		$(".accordionOpenCloseButton").click(function() {
			if (!accordionTrans) {
				accordionTrans = true;
				if ($(this).parent().find(".accordionBucketInnerContent").css("display") == "block") {
					$(this).parent().find(".accordionBucketInnerContent").slideUp("slow", function() { accordionTrans = false; });
					$(this).removeClass("accordionCloseButton");
				} else {
					$(this).parent().find(".accordionBucketInnerContent").slideToggle("slow", function() { accordionTrans = false; });
					$(this).addClass("accordionCloseButton");
				}
			}
		});
	}

	// if we have news, setup the ticker
	if ($("#news_ticker").length) {
		if ($("#news_ticker").find("li").length > 0) {
			var id = 1;
			$("#news_ticker").find("li").each(function() {
				this.id = "news_item_" + id;
				$("#news_item_" + id++).hide();
			});
			$("#news_item_1").fadeIn("slow");
			newsTimer = setInterval("nextNewsItem();", newsInterval);
		} else {
			document.getElementById("news_ticker").innerHTML = "<li>Vuelva de nuevo para consultar los titulares de las noticias más recientes!</li>";
		}
	}

	// if we have the help_us_buy thing, set that up
	if ($(".productFinderContent").length) {
		$(".productFinderContent").hide();
		$(".productFinderTitle").click(function() {
			if ($(".productFinderContent").css("display") == "none") {
				$(".productFinderContent").slideDown("fast");
				$(".productFinderTitle").addClass("selected");
			} else {
				$(".productFinderContent").slideUp("fast");
				$(".productFinderTitle").removeClass("selected");
			}
		});
	}
	// if we have the any select drop downs, set 'em up
	if ($(".selectDropdown").length) {
		// hide the dropdowns
		$(".selectDropdown ul").hide();
		// add the main click function to drop the thing down
		$(".selectDropdown").click(function() {
			$(this).find("ul").slideToggle("fast");
		});
		// add the function to click each A tag under it
		$(".selectDropdown ul li a").each(function() {
			$(this).click(function() {
				$(this).parent().parent().parent().find(".displayText").html($(this).text());
				//$(this).parent().hide(); //ie7
				//$(this).hide();
				$(this).parent().parent().slideToggle("fast");
				return false;
			});
		})
	}

	//Drop down for LCN044 states
	// if we have the any select drop downs, set 'em up
	if ($(".s_dropdown").length) {
		// hide the dropdowns
		$(".s_dropdown ul").hide();
		// add the main click function to drop the thing down
		$(".s_dropdown").click(function() {
			$(".s_dropdown ul").slideToggle("fast");
		});
		$(".s_dropdown ul").mouseover(function() {
			$(".s_dropdown ul").css({ 'cursor': 'pointer' });
		});
		$(".s_dropdown ul").mouseout(function() {
			$(".s_dropdown ul").css({ 'cursor': 'pointer' });
		});
		// add the function to click each li tag under it
		$(".s_dropdown ul li").each(function() {
			$(this).click(function() {
				$(this).parent().parent().find(".displayState").html($(this).text());
				$(this).parent().hide(); //ie7
				$(this).hide();
				return false;
			});
		})
	}

	//Drop down for LCN044 search radius
	// if we have the any select drop downs, set 'em up
	if ($(".rad_dropdown").length) {
		// hide the dropdowns
		$(".rad_dropdown ul").hide();
		// add the main click function to drop the thing down
		$(".rad_dropdown").click(function() {
			$(".rad_dropdown ul").slideToggle("fast");
		});
		$(".rad_dropdown ul").mouseover(function() {
			$(".rad_dropdown ul").css({ 'cursor': 'pointer' });
		});
		$(".rad_dropdown ul").mouseout(function() {
			$(".rad_dropdown ul").css({ 'cursor': 'pointer' });
		});
		// add the function to click each li tag under it
		$(".rad_dropdown ul li").each(function() {
			$(this).click(function() {
				$(this).parent().parent().find(".displayRad").html($(this).text());
				$(this).parent().hide(); //ie7
				$(this).hide();
				return false;
			});
		})
	}
	/*******************************home_tab_bucket content changer*****************************/
	$("#new_button").bind("click", function(e) {
		wnewBtnsEnable();
		whatsnewTimer = setInterval("nextWNew();", whatsnewInterval);
		$("#new_button").removeClass("inactive");
		$("#prod_button").addClass("inactive");
		$("#promo_button").addClass("inactive");
		$("#events_button").addClass("inactive");
		$("#featured_product").hide(0);
		$("#current_promotions").hide(0);
		$("#events").hide(0);
		$("#whats_new").show(0);
	});

	$("#prod_button").bind("click", function(e) {
		clearInterval(whatsnewTimer);
		if (whatsnew_arrows) wnewBtnsDisable();
		$("#prod_button").removeClass("inactive");
		$("#new_button").addClass("inactive");
		$("#promo_button").addClass("inactive");
		$("#events_button").addClass("inactive");
		$("#whats_new").hide(0);
		$("#current_promotions").hide(0);
		$("#events").hide(0);
		$("#featured_product").show(0);
	});

	$("#promo_button").bind("click", function(e) {
		clearInterval(whatsnewTimer);
		if (whatsnew_arrows) wnewBtnsDisable();
		$("#promo_button").removeClass("inactive");
		$("#new_button").addClass("inactive");
		$("#prod_button").addClass("inactive");
		$("#events_button").addClass("inactive");
		$("#whats_new").hide(0);
		$("#featured_product").hide(0);
		$("#events").hide(0);
		$("#current_promotions").show(0);
	});

	$("#events_button").bind("click", function(e) {
		clearInterval(whatsnewTimer);
		if (whatsnew_arrows) wnewBtnsDisable();
		$("#events_button").removeClass("inactive");
		$("#new_button").addClass("inactive");
		$("#prod_button").addClass("inactive");
		$("#promo_button").addClass("inactive");
		$("#whats_new").hide(0);
		$("#featured_product").hide(0);
		$("#current_promotions").hide(0);
		$("#events").show(0);
		/********************END********home_tab_bucket content changer*****************************/
	});

	//			// welcome screen with cookies
	//			if(readCookie(welcomeCookieName) == null || readCookie(welcomeCookieName) == ""){
	//				GB_show('', '/overlays/Welcome_overlay.html', 342, 612, null);
	//				createCookie(welcomeCookieName, "1", 4);
	//			}


	/* GCN017 (on page 6.0 only) clickthru code HERE: */
	$("#GCN017").mouseover(function() {
		showLogin60();
	});
	$("#GCN017_links").mouseover(function() {
		showLogin60();
	});
	$("#GCN017").mouseout(function() {
		hideLogin60();
	});
	function showLogin60() {
		$("#GCN017_links").css("display", "block");
		$("#GCN017_btn").css("background-position", "bottom");
	}
	function hideLogin60() {
		$("#GCN017_links").css("display", "none");
		$("#GCN017_btn").css("background-position", "top");
	}

	/* LCN058 menu/tab code */
	var $p_info_btns = $(".partnersPanel");
	var $p_info_tabs = $(".partnersNav > ul li");

	$("#p6_a").bind("click", function(e) {
		$("#p6_b").removeClass("selected");
		$("#p6_c").removeClass("selected");
		$("#p6_d").removeClass("selected");
		$("#LCN058b").hide(0);
		$("#LCN058c").hide(0);
		$("#LCN058d").hide(0);
		$("#p6_a").addClass("selected");
		$("#LCN058a").show(0);
		return false;
	});

	$("#p6_b").bind("click", function(e) {
		$("#p6_a").removeClass("selected");
		$("#p6_c").removeClass("selected");
		$("#p6_d").removeClass("selected");
		$("#LCN058a").hide(0);
		$("#LCN058c").hide(0);
		$("#LCN058d").hide(0);
		$("#p6_b").addClass("selected");
		$("#LCN058b").show(0);
		return false;
	});

	$("#p6_c").bind("click", function(e) {
		$("#p6_a").removeClass("selected");
		$("#p6_b").removeClass("selected");
		$("#p6_d").removeClass("selected");
		$("#LCN058a").hide(0);
		$("#LCN058b").hide(0);
		$("#LCN058d").hide(0);
		$("#p6_c").addClass("selected");
		$("#LCN058c").show(0);
		return false;
	});

	$("#p6_d").bind("click", function(e) {
		$("#p6_a").removeClass("selected");
		$("#p6_b").removeClass("selected");
		$("#p6_c").removeClass("selected");
		$("#LCN058a").hide(0);
		$("#LCN058b").hide(0);
		$("#LCN058c").hide(0);
		$("#p6_d").addClass("selected");
		$("#LCN058d").show(0);
		return false;
	});
	/* END LCN058 */

	/*  LCN061, LCN082A: Locations Selector
	- get xml
	- assign drop selections
	- click handler for drop selection
	*/
	var locations = $.ajax
			({
				type: "GET",
				url: '/xml/www/locations.xml',
				dataType: "text/xml",
				async: false,
				cache: false,
				success: function(msg) {
					//alert( "Data Saved: " + msg );
				}

			}).responseXML;

	/* temp array to track duplicate entries for selectors*/
	var selectors_array = new Array();
	$("location[country='US']", locations).each(function() {
		if (!selectors_array[$(this).attr("selector")]) {
			selectors_array[$(this).attr("selector")] = true;
			$("#US_locations").append("<li><a href='' title=''>" + $(this).attr("selector") + "</a></li>");
		}
	});

	$("location[country='GLOBAL']", locations).each(function() {
		if (!selectors_array[$(this).attr("selector")]) {
			selectors_array[$(this).attr("selector")] = true;
			$("#GLOBAL_locations").append("<li><a href='' title=''>" + $(this).attr("selector") + "</a></li>");
		}
	});

	$("#LCN061b .selectDropdown ul li a").click(function() {
		var display_text = '';
		var current_locations = $("location[selector='" + $(this).text() + "']", locations);
		var current_location_country = $(current_locations[0]).attr("country");

		// hide the current display
		$("#" + current_location_country + "_location").css("display", "none");

		for (var current_location = 0; current_location < current_locations.length; current_location++) {
			//TITLE
			display_text += "<strong>" + $("title", current_locations[current_location]).text() + "<\/strong>";
			//ADDRESS
			display_text += $("address", current_locations[current_location]).text();
			display_text += "<br/>";
			//PHONE: may be multiple lines
			display_text += $("phone", current_locations[current_location]).text();
			display_text += "<br/>";
			//FAX: may be multiple lines
			display_text += $("fax", current_locations[current_location]).text();
			display_text += "<br/>";
			//EXTRA DATA: notes xml tag
			display_text += $("notes", current_locations[current_location]).text();
			//END
			display_text += "<br/>";

			//convert line breaks to HTML
			display_text = display_text.replace(/\n/g, "<br/>");
		}

		//alert(display_text);
		$("#" + current_location_country + "_location").empty();
		$("#" + current_location_country + "_location").html(display_text);
		$("#" + current_location_country + "_location").css("display", "block");

		$(this).parent().parent().parent().find(".displayText").html($(this).text());
		//$(this).parent().hide(); //ie7
		//$(this).hide();
		$(this).parent().parent().slideToggle("fast");
		return false;
	});

	$("#LCN061c .selectDropdown ul li a").click(function() {
		var display_text = '';
		var current_locations = $("location[selector='" + $(this).text() + "']", locations);
		var current_location_country = $(current_locations[0]).attr("country");

		// hide the current display
		$("#" + current_location_country + "_location").css("display", "none");

		for (var current_location = 0; current_location < current_locations.length; current_location++) {
			//TITLE
			display_text += "<strong>" + $("title", current_locations[current_location]).text() + "<\/strong>";
			//ADDRESS
			display_text += $("address", current_locations[current_location]).text();
			display_text += "<br/>";
			//PHONE: may be multiple lines
			display_text += $("phone", current_locations[current_location]).text();
			display_text += "<br/>";
			//FAX: may be multiple lines
			display_text += $("fax", current_locations[current_location]).text();
			display_text += "<br/>";
			//EXTRA DATA: notes xml tag
			display_text += $("notes", current_locations[current_location]).text();
			//END
			display_text += "<br/>";

			//convert line breaks to HTML
			display_text = display_text.replace(/\n/g, "<br/>");
		}

		$("#" + current_location_country + "_location").empty();
		$("#" + current_location_country + "_location").html(display_text);
		$("#" + current_location_country + "_location").css("display", "block");

		$(this).parent().parent().parent().find(".displayText").html($(this).text());
		//$(this).parent().hide(); //ie7
		//$(this).hide();
		$(this).parent().parent().slideToggle("fast");
		return false;
	});
	/*  END - LCN061 */

	/*  LCN082A */
	$("#LCN082A .selectDropdown ul li a").click(function() {
		var display_text = '';
		var current_locations = $("location[selector='" + $(this).text() + "']", locations);
		var current_location_country = $(current_locations[0]).attr("country");

		// hide the current display
		$("#phone_support").fadeOut("fast", function() {

			for (var current_location = 0; current_location < current_locations.length; current_location++) {
				//TITLE
				display_text += $("title", current_locations[current_location]).text() + ":<br/>";
				//PHONE: may be multiple lines
				display_text += "<span class='phone_num'>" + $("phone", current_locations[current_location]).text() + "</\span>";
				display_text += "<br/>";
				//END
				display_text += "<br/>";

				//convert line breaks to HTML
				display_text = display_text.replace(/\n/g, "<br/>");
			}

			//alert(display_text);
			$("#phone_support").empty();
			$("#phone_support").html(display_text);
			$("#phone_support").fadeIn("fast");
		});

		$(this).parent().parent().parent().find(".displayText").html($(this).text());
		//$(this).parent().hide(); //ie7
		//$(this).hide();
		$(this).parent().parent().slideToggle("fast");
		return false;
	});
	/*  END - LCN082A */
	
	/* .content_section and .container_type1 */
	var maxht;
	var ht;
	if ($(".container_type1").length) {
		maxht = 0;
		$(".container_type1").each(function() {
			ht = this.offsetHeight;
			if(ht > maxht)
				maxht = ht;
		});
		$(".container_type1").each(function() {
			ht = this.offsetHeight;
			$(this).find(".container_type1-link").css("top", ((maxht - ht + 5) + "px"));
			this.style.height = maxht + "px";
		});
		$(".content_section").each(function() {
			this.style.height = (maxht + 25) + "px";
		});
		$(".container_type1-link").each(function() {
			this.style.visibility = "visible";
		});
	}
	else if ($(".content_section").length) {
		maxht = 0;
		$(".content_section").each(function() {
			ht = this.offsetHeight;
			if(ht > maxht)
				maxht = ht;
		});
		$(".content_section").each(function() {
			this.style.height = (maxht + 55) + "px";
		});
	}
	/* END - .content_section and .container_type1 */

	/* .accordionListItem */
	/* This cleans up some garbage in the product DCRs where some bogus <font> tags got inserted */
	$(".accordionListItem font font p").each(function() {
		var thisp = this;
		var wrapper = $(this).parent().parent();
		$(thisp).insertAfter(wrapper);
		$(wrapper).remove();		
	});
	/* END - .accordionListItem */


});

// ********************************************************************************************************************************** //
//  function declarations below this line:
// ********************************************************************************************************************************** //

// welcome dialog cookie stuff
var welcomeCookieName = "3comBetaWelcome";
var surveyCookieName = "3comBetaSurvey";
function clickReturn3Com(returnLink) {
	if (readCookie(surveyCookieName) == null || readCookie(welcomeCookieName) == "") {
		createCookie(surveyCookieName, "1", 9999);
		return GB_show('', 'http://www.polldaddy.com/s/D14BD2922200B892/', 500, 790, function() { window.location = returnLink; });
	} else {
		return GB_show('', '/overlays/Return3com_overlay.html', 301, 612, function() { window.location = returnLink; });
	}
}
function clickSurvey() {
	createCookie(surveyCookieName, "1", 9999);
	return GB_show('', 'http://www.polldaddy.com/s/D14BD2922200B892/', 500, 790, null);
}

function createCookie(name, value, hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

// ********************************************************************************************************************************** //
//  Navigation functions
// ********************************************************************************************************************************** //

// top nav javascript
var goingUp = "none";
function topMenuOver(id) {
	if (goingUp != id) {
		$("#" + id).stop(true, true);
		$("#" + id).slideDown("fast");
	} else {
		clearTimeout(uptimer);
		goingUp = "none";
	}
}
function topMenuOff(id) {
	goingUp = id;
	$("#" + id).stop(true, true);
	uptimer = setTimeout("$('#" + id + "').slideUp('fast', function() { goingUp = 'none'; });", 100);
}
function topMenuClose() {
	cur = "";
	$('.menuDropdown').slideUp('fast');
}

// top nav javascript
function headerMenuOver(id) {
	clearTimeout(headerUpTimer);
	if (id != cur && cur != "") {
		if (!headerTrans) {
			headerTrans = true;
			$('#' + cur).slideUp(25, function() {
				$('#' + cur).slideDown('fast');
				headerTrans = false;
			});
		}
		cur = id;
	} else {
		$('#' + id).slideDown('fast');
		cur = id;
	}
}
function headerMenuOff(id) {
	headerUpTimer = setTimeout("$('.headerDropDown').slideUp('fast'); cur='';", 400);
}
function headerMenuClose() {
	cur = "";
	$('.headerDropDown').slideUp('fast');
}
// input text field clear on focus:
var SearchBox_Handler = {
	clearedOnce: false,
	clear: function(field) {
		if (this.clearedOnce == false) {
			field.value = '';
			this.clearedOnce = true;
		}
	}
}

// ********************************************************************************************************************************** //
//  LCN002 - News ticker
// ********************************************************************************************************************************** //
// news ticker javascript
function nextNewsItem() {
	if (!newsTrans) {
		clearInterval(newsTimer);
		var old = "news_item_" + curNewsItem;
		curNewsItem++;
		if (curNewsItem > $("#news_ticker").find("li").length) { curNewsItem = 1; }
		newsTrans = true;
		$("#" + old).fadeOut("slow", function() {
			$("#news_item_" + curNewsItem).fadeIn("slow");
			newsTrans = false;
		});
		newsTimer = setInterval("nextNewsItem();", newsInterval);
	}
}

function lastNewsItem() {
	if (!newsTrans) {
		clearInterval(newsTimer);
		var old = "news_item_" + curNewsItem;
		curNewsItem--;
		if (curNewsItem < 1) { curNewsItem = $("#news_ticker").find("li").length; }
		newsTrans = true;
		$("#" + old).fadeOut("slow", function() {
			$("#news_item_" + curNewsItem).fadeIn("slow");
			newsTrans = false;
		});
		newsTimer = setInterval("nextNewsItem();", newsInterval);
	}
}

// ********************************************************************************************************************************** //
//  GCN005
// ********************************************************************************************************************************** //
var GCN005_input1_Handler = {
	clearedOnce: false,
	clear: function(field) {
		if (this.clearedOnce == false) {
			field.value = '';
			this.clearedOnce = true;
		}
	}
}

var GCN005_input2_Handler = {
	clearedOnce: false,
	clear: function(field) {
		if (this.clearedOnce == false) {
			field.value = '';
			this.clearedOnce = true;
		}
	}
}

// ********************************************************************************************************************************** //
//  LCNO63 Executive Team ||  Board of Directors
// ********************************************************************************************************************************** //
function showProfiles(profiles) {
	//first hide the current profile list
	hideProfiles(profiles == "exec" ? "board" : "exec");
	unsetMenuState(profiles == "exec" ? "board" : "exec");
	//show other profile list
	$("#" + profiles + "_profiles").css("display", "block");
	setMenuState(profiles)
}
function hideProfiles(profiles) {
	$("#" + profiles + "_profiles").css("display", "none");
}
function setMenuState(profiles) {
	$("#" + profiles + "_link").addClass("selected");
}
function unsetMenuState(profiles) {
	$("#" + profiles + "_link").removeClass("selected");
}

// ********************************************************************************************************************************** //
/* Google Maps load function */
function loadMap() {
	try {
		var map = new GMap2(document.getElementById("map"));
	} catch (e) {
		return;
	}

	map.addControl(new GSmallMapControl());
	var geocoder = new GClientGeocoder();

	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.setCenter(point, 13);
				map.panDirection(0, 1);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml(displayAddress);
			}
		}
	  );
}
// ********************************************************************************************************************************** //
//  GCN007 -- Success Stories Flipper 
// ********************************************************************************************************************************** //
var $success_index = 0;
function nextSStory() {
	clearInterval(successTimer);
	$success_index++;
	if ($success_index >= $("#success_data > img").length) $success_index = 0;

	successTrans = true;
	$("#success_pict").fadeOut("fast", function() {
		$img = $("#success_data > img").get($success_index);
		$("#success_pict").attr("src", $img.src);
		$("#success_pict").fadeIn("slow");
	});
	if ($("#success_data span.contentWindowName").length) {
		$("#content_window_name").fadeOut("fast", function() {
			$("#content_window_name").text($("#success_data span.contentWindowName:eq(" + $success_index + ")").text());
			$("#content_window_name").fadeIn("slow");
		});
	}
	$("#content_window_content").fadeOut("fast", function() {
		$text_elem = $("#success_data > div").get($success_index);
		$text = $($text_elem).html();
		$("#content_window_content").text($text);
		$("#content_window_content").fadeIn("slow", function() { successTrans = false });
	});
	if (success_automatic) successTimer = setInterval("nextSStory();", successInterval);
}

function prevSStory() {
	clearInterval(successTimer);
	$success_index--;
	if ($success_index < 0) $success_index = $("#success_data > img").length - 1;

	successTrans = true;
	$("#success_pict").fadeOut("fast", function() {
		$img = $("#success_data > img").get($success_index);
		$("#success_pict").attr("src", $img.src);
		$("#success_pict").fadeIn("slow");
	});
	if ($("#success_data span.contentWindowName").length) {
		$("#content_window_name").fadeOut("fast", function() {
			$("#content_window_name").text($("#success_data span.contentWindowName:eq(" + $success_index + ")").text());
			$("#content_window_name").fadeIn("slow");
		});
	}
	$("#content_window_content").fadeOut("fast", function() {
		$text_elem = $("#success_data > div").get($success_index);
		$text = $($text_elem).html();
		$("#content_window_content").text($text);
		$("#content_window_content").fadeIn("slow", function() { successTrans = false });
	});
	if (success_automatic) successTimer = setInterval("nextSStory();", successInterval);
}

// ********************************************************************************************************************************** //
//  LCN040 -- Support Team Flipper 
// ********************************************************************************************************************************** //
function initLCN040() {
	// if we have Support Team container, setup the auto-flipper
	if ($("#support_data").length) {
		if ($("#support_data").find("img").length > 0) {
			var ssid = 1;
			$("#support_data").find("img").each(function() {
				this.id = "support_pict" + ssid++;
			});
			ssid = 1;
			$("#support_data").find("div").each(function() {
				this.id = "support_div" + ssid++;
			});
			$support_index = STMAX;
			nextTMember(); 
		} else {
			$("#LCN040").hide(); //if container is empty, do not display
		}
	}
//#support_team_flipper arrow  buttons:
	$("#stmal").bind("click", function(e) {
		support_automatic = false; // switch from automatic to manual
		if (!supportTrans) prevTMember();
	});

	$("#stmar").bind("click", function(e) {
		support_automatic = false; // switch from automatic to manual
		if (!supportTrans) nextTMember();
	});
	
};
var $support_index = 0;
function nextTMember() {
	clearInterval(supportTimer);
	$support_index++;
	if ($support_index >= $("#support_data > img").length) $support_index = 0;

	supportTrans = true;
	$("#support_pict").fadeOut("fast", function() {
		$img = $("#support_data > img").get($support_index);
//		$("#support_pict")
//			.load(function () { $(this).fadeIn("slow"); }) 
//			.error(function () {  alert("file "+$img.src+" failed to load.");}) ;
		$("#support_pict").attr("src", $img.src);
		// if the image takes too long to load, this fade can be too early
		$("#support_pict").fadeIn("slow");
		
	});
	$("#content_window_name").fadeOut("fast", function() {
		$("#content_window_name").text($("#support_data span.contentWindowName:eq(" + $support_index + ")").text());
		$("#content_window_name").fadeIn("slow");
	});
	$("#content_window_content").fadeOut("fast", function() {
		$text_elem = $("#support_data > div").get($support_index);
		$text = $($text_elem).html();
		$("#content_window_content").text($text);
		$("#content_window_content").fadeIn("slow", function() { supportTrans = false });
	});
	if (support_automatic) supportTimer = setInterval("nextTMember();", supportInterval);
}

function prevTMember() {
	clearInterval(supportTimer);
	$support_index--;
	if ($support_index < 0) $support_index = $("#support_data > img").length - 1;

	supportTrans = true
	$("#support_pict").fadeOut("fast", function() {
		$img = $("#support_data > img").get($support_index);
		$("#support_pict").attr("src", $img.src);
		$("#support_pict").fadeIn("slow");
	});
	if ($("#support_data span.contentWindowName").length) {
		$("#content_window_name").fadeOut("fast", function() {
			$("#content_window_name").text($("#support_data span.contentWindowName:eq(" + $support_index + ")").text());
			$("#content_window_name").fadeIn("slow");
		});
	}
	$("#content_window_content").fadeOut("fast", function() {
		$text_elem = $("#support_data > div").get($support_index);
		$text = $($text_elem).html();
		$("#content_window_content").text($text);
		$("#content_window_content").fadeIn("slow", function() { supportTrans = false });
	});
	if (support_automatic) supportTimer = setInterval("nextTMember();", supportInterval);
}

// ********************************************************************************************************************************** //
//  GCN001 -- What's New (homepage) Flipper
// ********************************************************************************************************************************** //
var $whatsnew_index = 0;
function nextWNew() {
	if (whatsnew_arrows) {
		clearInterval(whatsnewTimer);
		var wnstr = "#whats_new_item_no" + $whatsnew_index;
		$(wnstr).hide();
		$whatsnew_index++;
		if ($whatsnew_index >= $("#whats_new > div").length) $whatsnew_index = 0;
		wnstr = "#whats_new_item_no" + $whatsnew_index;

		whatsnewTrans = true
		$("#whats_new").fadeOut("fast", function() {
			$(wnstr).show();
			$("#whats_new").fadeIn("slow", function() { whatsnewTrans = false });
		});
		if (whatsnew_automatic) whatsnewTimer = setInterval("nextWNew();", whatsnewInterval);
	}
}

function prevWNew() {
	if (whatsnew_arrows) {
		clearInterval(whatsnewTimer);
		var wnstr = "#whats_new_item_no" + $whatsnew_index;
		$(wnstr).hide();
		$whatsnew_index--;
		if ($whatsnew_index < 0) $whatsnew_index = $("#whats_new > div").length - 1;
		wnstr = "#whats_new_item_no" + $whatsnew_index;

		whatsnewTrans = true
		$("#whats_new").fadeOut("fast", function() {
			$(wnstr).show();
			$("#whats_new").fadeIn("slow", function() { whatsnewTrans = false });
		});
		if (whatsnew_automatic) whatsnewTimer = setInterval("nextWNew();", whatsnewInterval);
	}
}

function wnewBtnsDisable() {
	whatsnew_arrows = whatsnew_automatic = false;
	$("#wnal").attr("src", "/images/www/home/wnew_arrowl_off.gif");
	$("#wnar").attr("src", "/images/www/home/wnew_arrowr_off.gif");
	$("#wnal").css({ 'cursor': 'default' });
	$("#wnar").css({ 'cursor': 'default' });
}
function wnewBtnsEnable() {
	whatsnew_arrows = whatsnew_automatic = true;
	$("#wnal").attr("src", "/images/www/home/wnew_arrowl_on.gif");
	$("#wnar").attr("src", "/images/www/home/wnew_arrowr_on.gif");
	$("#wnal").css({ 'cursor': 'pointer' });
	$("#wnar").css({ 'cursor': 'pointer' });
}
