// --- START GLOBAL CONFIG PARAMS ---

var hoverDelayShow	= 300;				// in milliseconds, the top menu disappear delay
var hoverDelayHide	= 700;				// in milliseconds, the submenu disappear delay
var siteFeaturesDelay = 5000;			// in milliseconds, the Site Features appear delay
var tabAttentionWaitCount = 8;			// the number of times a tab would highlight to wait before actually doing the highlights
var tabAttentionDelay = 400;			// in milliseconds, the time between the attention-grabbing attempt of each javascript Tab
var logoPopupDelay = 500;				// in milliseconds, the time for the logo info box to stay before fading away

// --- END GLOBAL CONFIG PARAMS ---

// --- LIBRARY CODE ---
function findPos(obj) {
	var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function OpenEmailCMS() {
	emailWind=window.open("/home/email.asp?URLemail="+escape(window.location)+"&Title="+escape(document.title),"Email","status=no,toolbar=no,resizable=yes,location=no,width=320,height=350");
	emailWind.focus();
}

function OpenPrintCMS(fromLegacy) {
	var url = window.location.href;	
	url += (url.indexOf('?') > -1 || !fromLegacy) ? '&vPrint=1' : '?vPrint=1'; //non-legacy pages always use &; for legacy pages, if query string parm exists then append, otherwise create (AiG:JG 10-10-07)
	//alert(url);
	printWind=window.open(url,"PrintFriendly","status=0,toolbar=0,menubar=1,resizable=1,location=0,scrollbars=1,width=800,height=600");
	printWind.focus();
	//alert();
	//alert($(printWind.document.getElementsByTagName('link')).find('link[@media=print]').attr("media"));
	//alert($("link[@media='screen']", printWind.document.getElementsByTagName('link')).get().length);
	//$("link[@media='screen']", printWind.document.getElementsByTagName('link')).remove();
	//$(printWind.document.getElementsByTagName('link')).find("link[@media='print']").attr({media: 'screen'});
	//alert($(printWind.document.getElementsByTagName('link')).find("link[@media='screen']").attr("media"));
}

$(document).ready(function() {
//aigLoadObjects.push(function() {
	var url = window.location.href;
	if (url.indexOf('vPrint=1') > -1) {
		$("link[@media='screen']").remove();
		$("link[@media='print']").attr("media", "all").each(function() {
			$(this).attr("href", $(this).attr("href") + '?');
		});
	}
});


// --- START TICKER CODE ---

var siteFeatures = null;
var featureIndex = 0;
var isFocused = 0;

$(document).ready(function() {
//aigLoadObjects.push(function() {
	
    siteFeatures = $("#siteFeatures li").hide().size();
    $("#siteFeatures li:eq(" + featureIndex + ")").show();
    $("#siteFeatures").hover(function() {
    	isFocused = 1;
    }, function() {
    	isFocused = 0;
    });
    setInterval(updateSiteFeatures, siteFeaturesDelay);
});

function updateSiteFeatures() {
	if (isFocused) return;
    $("#siteFeatures li:eq(" + featureIndex + ")").fadeOut('slow', function() {
    	$(this).hide();
    	featureIndex = (featureIndex + 1) % siteFeatures;
    	$("#siteFeatures li:eq(" + featureIndex + ")").show().fadeIn('slow');
    });
}

// --- END TICKER CODE ---




// --- START SEARCH CODE ---

$(document).ready(function() {
//aigLoadObjects.push(function() {
	//$("input[@type=text].search").each(function() {
	$("input.search").each(function() {
		var searchText = this.value;
		$(this)
			.focus(function() {
				if (this.value == searchText) this.value = '';
			})
			.blur(function() {
				if (this.value == '') this.value = searchText;
			})
		;
	});
});

// --- END SEARCH CODE ---



// --- START NAV CODE ---

setupSubNav = function(theTimeout, targetA, delay, showing) {
	clearTimeout(theTimeout);

	return theTimeout = setTimeout(function() {
		//var thisName = $(targetA).get(0).data.toString().replace(/(\u00a0|&nbsp;|\s)+/ig, '_').replace(/\s/g, '*').replace(/\u00a0/, '*').replace(/[^a-z]/g, '&');
		var thisName = $(targetA).get(0).firstChild.data.toString();
		var subNav = $("#secondaryNav").find("[@title='" + thisName + "']");
		var isDropDown = ($('#secondaryNav').css('position') == 'absolute') ? 1 : 0;

		$("#topNav li a.active").removeClass("active");
		$(targetA).addClass("active");
		
		// make drop-down list types work
		if (isDropDown) {
			$("#secondaryNav").css("left", findPos($(targetA).get(0))[0] - findPos($("#topNav").get(0))[0] );
		}
		
		$("#secondaryNav div").hide().find('ul').hide();
		if (isDropDown) {
			if (showing) subNav.hide().show().find("ul").show();
		} else {
			subNav.hide().show().find("ul").fadeIn('fast');
		}
		
		//setTimeout(function() { subNav.show().find("ul").show().fadeIn('fast'); }, 30);
	}, delay);
}

$(document).ready(function() {
//aigLoadObjects.push(function() {
	var viewingA = null;		// which nav item we are currently viewing (the loaded page)
	var theTimeout = null;
	var subNavDiv = $("#secondaryNav");
	
	viewingA = $("#topNav li a.active").get(0);
	if (viewingA == null) viewingA = $("#topNav li").get(0);

	$("#topNav li").each(function(i) {
		var thisName = $("a", this).get(0).firstChild.data.toString();	// setup classes for drop-downs
		var childCount = $("#secondaryNav").find("[@title='" + thisName + "']").length;
		if (childCount > 0 && thisName.toUpperCase() != 'HOME') $(this).addClass('hasChildren');
		
		$(this)
			.hover(function() {
				theTimeout = setupSubNav(theTimeout, $("a", this).get(0), hoverDelayShow, true);
			}, function() {
				theTimeout = setupSubNav(theTimeout, viewingA, hoverDelayHide, false);
			})
		;
	});
	$("#secondaryNav")
		.hover(function() {
			clearTimeout(theTimeout);
		}, function() {
			theTimeout = setupSubNav(theTimeout, viewingA, hoverDelayHide, false);
		})
	;
});

// --- END NAV CODE ---


// --- START COUNTRY SELECT CODE ---

function setupCountryMenu(theTimeout, menuIDname, delay, showing) {
	clearTimeout(theTimeout);
	return setTimeout(function() {
		if (showing)	$('#' + menuIDname).slideDown('fast');
		else			$('#' + menuIDname).slideUp();
	}, delay);
}

$(document).ready(function() {
//aigLoadObjects.push(function() {
	var countryTimeout = null;
	$("#countryActuator")
		.hover(function() {
			countryTimeout = setupCountryMenu(countryTimeout, 'countryMenu', hoverDelayShow, true);
		}, function() {
			countryTimeout = setupCountryMenu(countryTimeout, 'countryMenu', hoverDelayHide, false);
		})
	;
	$("#countryMenu")
		.hover(function() {
			clearTimeout(countryTimeout);
		}, function() {
			countryTimeout = setupCountryMenu(countryTimeout, 'countryMenu', hoverDelayHide, false);
		})
	;
});

// --- END COUNTRY SELECT CODE ---



// --- START NEWSLETTER CODE ---

$(document).ready(function() {
//aigLoadObjects.push(function() {
	$("div.emailForm").hide();
	
	//$("input[@type=text].email").each(function() {
	$("input.email").each(function() {
		$(this)
			.focus(function() {
				$("div.emailForm:hidden").show("normal");
				var field = $("input.email").get()[0];
				if (field != this) $(field).focus();
			})
			.blur(function() {
				//$("div.emailForm").hide("normal");
			})
		;
	});
});

// -- END NEWSLETTER CODE ---


// --- START TAB CODE ---

//aigLoadObjects.push(function() {
$("div.tabContainer").ready(function() {
	var cancelTimeout = false;
	$("div.tabContainer").each(function() {
		var tabContainer = this;
		if (this.className.indexOf('tabHover') != -1) {		// added by JL 6.28.07 -- only set height for hover tabs
			var maxHeight = 0;
			$("div.tab-ct", tabContainer).each(function() {
				if (this.scrollHeight > maxHeight) maxHeight = this.scrollHeight;
			});
			$("div.tab-ct", tabContainer).height(maxHeight + 'px');
		}
		
		$("div.tab-ct:gt(0)", this).hide();
		$($(tabContainer).find("h3.tab").addClass('tabTitle').hover(function() {
			if (this.className.indexOf('active') != -1) return;
			if (tabContainer.className.indexOf('tabHover') == -1) return;
			cancelTimeout = true;
			$("h3.tab", tabContainer).removeClass("active");
			$(this).addClass("active");
			$("div.tab-ct", tabContainer).hide();
			$($("div.tab-ct", tabContainer).get($("h3.tab", tabContainer).index(this))).show();
		}, function() {}).click(function() {
			if (this.className.indexOf('active') != -1) return;
			cancelTimeout = true;
			$("h3.tab", tabContainer).removeClass("active");
			$(this).addClass("active");
			$("div.tab-ct", tabContainer).hide();
			$($("div.tab-ct", tabContainer).get($("h3.tab", tabContainer).index(this))).show();
			return false;
		}).get(0)).addClass('active').after( $("h3.tab:gt(0)", tabContainer).get() );
		$(tabContainer).find("h3.tab:first").addClass('first');		// added by JL 6.28.07 -- need first & last classes on h3's
		$(tabContainer).find("h3.tab:last").addClass('last');
	});
	$("h3.tab").each(function() {
		var thisH3 = this;

		setTimeout(function() {
			if (cancelTimeout) return;
			$(thisH3).addClass("tabAttention");
			
			setTimeout(function() {
				$(thisH3).removeClass("tabAttention");

			}, tabAttentionDelay);

		}, ($("h3.tab").index(thisH3) + tabAttentionWaitCount)*tabAttentionDelay*2);
	})
});

// -- END TAB CODE ---

// --- START AUTOHIDE CODE ---

$(document).ready(function() {
//aigLoadObjects.push(function() {
	$(".hideonclick").click(function() { $(this).hide(); }).focus(function() {
		$(this).hide();
		$('#newsletterFrame').each(function() {
			$(window.frames.newsletterFrame).focus();
			window.frames.newsletterFrame.focusOnEmail();
		});
	});
});

// -- END NEWSLETTER CODE ---


// -- START ARTICLE TABLE FORMAT CODE --- JNL 3.23.07


$(document).ready(function() {
//aigLoadObjects.push(function() {
	$("div.article table tr:odd").addClass("odd");
	$("td.sidenote").parent().parent().parent().addClass("sidenote");
});

// -- ARTICLE SHOW-HIDE ONCLICK CODE --- JNL 5.17.07
$(document).ready(function() {
//aigLoadObjects.push(function() {
	$(".hide-until-click").hide();
	$("a.click-to-show").each( function(i) {
		$(this).click( function() {
			$(".hide-until-click").eq(i).show("normal");
			$(this).hide();
			return false;
		});
	});
});


// -- START AIG WEB 2 FORMAT CODE --- JNL 5.25.07

$(document).ready(function() {
//aigLoadObjects.push(function() {
	$(".pageTitle").prepend("<span class='tl'>&nbsp;</span><span class='tr'>&nbsp;</span>");
	$(".col-left h2").each(function() {
		if ($("span", this).get().length == 0) {
			$(this).get(0).innerHTML = '<span>' + $(this).get(0).innerHTML + '</span>';
		}
	});
	
	var logoPopupFadeTimer = null;
	$("h1.headerHidden a").hover(function() {
		clearTimeout(logoPopupFadeTimer);
		if ($("div.logoPop").css("display") == "none") $("div.logoPop").fadeIn();
	}, function() {
		logoPopupFadeTimer = setTimeout(function () { $("div.logoPop").fadeOut(); }, logoPopupDelay);
	});
	$("div.logoPop").hover(function() {
		clearTimeout(logoPopupFadeTimer);
		if ($("div.logoPop").css("display") == "none") $("div.logoPop").fadeIn();
	}, function() {
		logoPopupFadeTimer = setTimeout(function () { $("div.logoPop").fadeOut(); }, logoPopupDelay);
	});
});

// -- START AIG PAGINATION CODE --- JNL 8.1.07
$("#paginate").ready(function() {
	var curPage = 0;
	$("#paginate").append("<p class='button prev'><a href='#paginateTop'>Previous</a></p> <p class='button next'><a href='#paginateTop'>Next</a></p><div class='clearPages'>&nbsp;</div>")
				.before("<a name='paginateTop'></a>");
	updatePage(curPage);
	$("#paginate .prev a").click(function() { curPage--; updatePage(curPage); });
	$("#paginate .next a").click(function() { curPage++; updatePage(curPage); });
});

function updatePage(p) {
	$("div.page:visible").hide();
	$("div.page").eq(p).show();
	$("#paginate .next").show();
	$("#paginate .prev").show();
	if (p == 0) { $("#paginate .prev").hide(); }
	if (p == $("div.page").length - 1) { $("#paginate .next").hide(); }
}


// -- Search box formatting AiG:ARS 07/10/07
var lastSearchBoxValue = null;
function formatSearchString(searchBox) {
	if (searchBox.value == lastSearchBoxValue) return;

	lastSearchBoxValue = searchBox.value;
	searchBox.value = searchBox.value.replace(/(['])/g, "’"); // watch file encoding for this character (utf-8)
}
$(document).ready(function() {
	$(".search").keydown(function() { formatSearchString(this); }).keyup(function() { formatSearchString(this); }).change(function() { formatSearchString(this); });
});
