//**************************************************************************************************
//	QUERYSTRING RETRIEVAL & STORAGE
//
//		Creates an array of all querystrings passed
//		to the page for easy access by any script.
//
//		**  This should probably stay at the top, **
//		**   as other functions may access this   **
//**************************************************
var pgParams = new Array();	//IMPORTANT! This is where the params are stored

// no 'Ready' function since it doesn't need to wait for the DOM
var qString = window.location.search.substring(1);
var qParams = qString.split('&');
for (var i = 0; i < qParams.length; i++) {
	var eq = qParams[i].indexOf('=');
	if (eq > 0) {
		var key = qParams[i].substring(0,eq);
		var val = qParams[i].substring(eq+1);
		pgParams[key] = val;	//this is where the keys are assembled into the array
	}
}

//**************************************************************************************************
//	AUTO PRINT
//
//		Will automatically print page if URL parameter
//		'print=auto' is passed.
//*************************************************
function readyAutoPrint(){
	if (pgParams['print'] == 'auto') {
		window.print();
	}
}

//**************************************************************************************************
//	POPOVER WINDOW FUNCTIONS
//
//		Opens a "popover" window and loads a remote
//		page into it. These tend to be complex.
//*************************************************

//	fixed-size popover READY function
function readyPopWin(){
	$("a[rel='pop']").click(function (e){
		e.preventDefault();
		$.modal(this.href + " #content > *", {
			containerId: 'popWin',
			onOpen: modalOpenPop,
			onClose: modalClosePop,
			append: 'div#content'
		});
		$("#modalOverlay").click(function() {
			$.modal.close();
		});
	});
}

//	auto-sized popover READY function
function readyPopWinAuto(){
	$("a[rel='popAuto']").click(function (e){
		e.preventDefault();
		$.modal(this.href + " #content > *", {
			containerId: 'popWinAuto',
			onOpen: modalOpenPop,
			onClose: modalClosePop,
			append: 'div#content'
		});
		$("#modalOverlay").click(function() {
			$.modal.close();
		});
	});
}

//	Access Your Accounts window
function readyAccess () {
	$("a[rel='access']").click(function (e){
		e.preventDefault();
		$.modal(this.href, {
			containerId: 'accessModal',
			onOpen: modalOpenPop,
			onClose: modalClosePop,
			append: 'div#content'
		});
		$("#modalOverlay").click(function() {
			$.modal.close();
		});
	});
}

//	Offsite Links Window
function readyOffsite () {
	$("a[rel='offsite']").click(function (e){
		e.preventDefault();
		$.modal("/content/modals/offsite/?offsite=" + this.href, {
			containerId: 'exitModal',
			onOpen: modalOpenPop,
			onClose: modalClosePop,
			append: 'div#content'
		});
		$("#modalOverlay").click(function() {
			$.modal.close();
		});
	});
}

//*************************************************
//	popover OPEN function
function modalOpenPop(dialog){
	var getURL = dialog.data.text();		//get the url to load out of the dialog object
	dialog.data.empty();					//clear the data div
	dialog.container.prepend("<h1 id='modalTitle'>Loading...</h1>");	//generate temporary title
	
	dialog.overlay.fadeIn(200, function () {
		dialog.container.show("puff", { percent: 120 }, 300, function () {
			
			//*************************************************
				//LOAD the destination page
			dialog.data.load(getURL, function(){
				
				dialog.data.find("#disclosure").siblings().replaceWith("");		//find if it's a disclosure, and trim surrounding content
				
				var $title = dialog.container.find("#modalTitle");		//find the temporary title h1, jQuery object
				var $heading = dialog.data.find("h1:first");			//find the first h1 in the loaded content, jQuery object
				var headText;	//hold the new title text
				
				//*************************************************
					//Get the title for the modal window
				if ($heading.is("h1")) {			//if an h1 was found,
					headText = $heading.html();		//set headText to its contents
					
					if ($heading.parent().is("div.modalData")) {	//if the found h1 was a direct child of the loaded content,
						$heading.replaceWith("");					//remove it from the content to avoid redundancy
					}
				} else {
					headText = "&nbsp;";	//if no h1 found, title will be blank
				}
				
				//*************************************************
					//Add close events to content
				dialog.data.find(".modalClose").click(function (e) {
					e.preventDefault();
					modalClosePop(dialog);
				});
				
				//*************************************************
					//Browser-specific content functions
				if ($.browser.msie) {
					//*************************************************
						//INTERNET EXPLORER
					
					$title.html(headText);		//swap titles, no animation because IE hates that
					dialog.container.find("a.modalCloseImg").hover(function(){		//this covers the old "hover" problem that IE has
						$(this).addClass("modalCloseHover");
					}, function(){
						$(this).removeClass("modalCloseHover");
					});
					dialog.data.slideDown({		//much more complicated data reveal
						duration: 500,
						easing: "easeInOutQuad",
						complete: function(){
							if (this.scrollHeight > this.offsetHeight) {	//if the content is larger than the container,
								$(this).animate({							//adjust the container to properly fit scrollbars
									paddingRight: "+=10px",					//(all other browsers do this automatically)
									width: "-=10px"
								}, 100);
							}
						}
					});
				} else {				
					//*************************************************
						//EVERYONE ELSE
					
					$title.animate({ opacity: 0 }, 100, function(){		//fade out the 'Loading' heading,
						$(this).html(headText).animate({ opacity: 100 }, 100);	//update it and fade it back in
					});
					dialog.data.slideDown(500, "easeInOutQuad");	//reveal data container
				}
			});
		});
	});
}

//*************************************************
//	popover CLOSE function
function modalClosePop(dialog){
	dialog.data.animate({
		height: 0,
		paddingTop: 0,
		paddingBottom: 0
	}, 300, "easeInOutQuad", function(){
		dialog.data.hide();//empty();
		dialog.container.hide("puff", { percent: 120 }, 200, function () {
			dialog.overlay.fadeOut(200, function () {
				$.modal.close();
			});
		});
	});
}

//**************************************************************************************************
//	Table Striping & Row Hovering
function readyTableStripe () {
	$("#content table.compare:not(.lefty)").each(function () {
		var $tr = $(this).find("tr:not(.topHeader, .tableFooter)");
		$tr.filter(":even").addClass("row-even");
		$tr.filter(":odd").addClass("row-odd");
		/*$tr.hover(function () {
			$(this).addClass("row-hover");
		},function () {
			$(this).removeClass("row-hover");
		});*/
	});
}


//**************************************************************************************************
//	Top Navigation dropdowns
function readyDropDowns () {
	//$("#nav-one").empty().load("/content/dropdowns.asp #nav-one > li", function () {
		$("#nav-one > li").hover(
			function () {
				$(this).addClass("sfHover");
				$("ul", this).bgiframe();
			}, 
			function () {
				$(this).removeClass("sfHover");
			} 
		);
	//});
}

//**************************************************************************************************
//	Tabs
function readyTabs () {
	var hashTab = window.location.hash;
	var $tabBox = $("#tabBox");
	
	if ((hashTab.length > 0) && ($("#tabBox").find("a[href=" + hashTab + "]").length > 0)) {
		alert(hashTab);
		$tabBox.show().children("ul").tabs().tabs("select", hashTab);
	} else {
		$tabBox.show().children("ul").tabs();
	}
}


//**************************************************************************************************
//	clueTips
function readyClueTip () {}

//**************************************************************************************************
//	FAQs
function readyFaq(){
	$("#content ul.faq li:has(:header)").each(function(){
		var $kids = $(this).children(":not(:header:first)").wrapAll("<div style='padding-left:12px'></div>").parent();	//wrap child elements in a div for better animation
		var $head = $(this).children(":header:first");
		
		$kids.hide();	//hide child elements
		$head.css({cursor: "pointer", textDecoration: "underline"})		//make images look like links
			.toggle(function(){				//add hide/reveal effect to child elements
				$kids.slideDown(200);
			}, function(){
				$kids.slideUp(200);
			})
			.hover(function(){			//add hover effect for 'pseudo-link' heading
				$(this).css({textDecoration: "none"});
			}, function(){
				$(this).css({textDecoration: "underline"});
		});
	});
}

//**************************************************************************************************
//	Append PDF text links
function readyPDF(){
	$("a[href$='PDF'],a[href$='pdf']").not(":has(img)").append("&nbsp;<sup>[PDF]</sup>");	/* PDF labels */
}

//**************************************************************************************************
//	BREADCRUMB FIX
//
//		Trims spans from the breadcrumb trail if it wraps
//*************************************************
function readyCrumbTrim(){
	var safety = 0;	//to avoid infinite loops
	
	while (($("#breadCrumbs").height() > 40) && (safety < 10)) {
		$("#breadCrumbs span:nth-child(2)").remove();
		safety++;
	}
}

//**************************************************************************************************
//**************************************************************************************************
//	Execute ALL jQuery 'ready' functions
//**************************************************************************************************
//**************************************************************************************************
$(function () {
	readyDropDowns();
	readyCrumbTrim();
	readyTabs();
	readyTableStripe();
	readyClueTip();
	readyAccess();
	readyOffsite();
	readyPopWin();
	readyPopWinAuto();
	readyFaq();
	readyPDF();
	readyAutoPrint();	//this should be last in the list, so the document is fully assembled before printing
});

//**************************************************************************************************
//	IE background-image flicker bug fix

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

