//*************************************************
//	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").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");
			} 
		);
	//});
}

//*************************************************
//	Smooth Scrolling
/*
$(document).ready(function() {
	$.localScroll({
		hash:true,
		easing:"easeInOutQuint",
		duration:750
	});
	$.localScroll.hash({
		hash:true,
		easing:"easeInOutQuint",
		duration:750
	});
});
*/


//*************************************************
//	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 () {
	$("span.tip").attr("title", function(){
		return (this.innerHTML + "|" + this.title);
	}).css({borderBottom: '1px dotted #666'}).cluetip({
		splitTitle: '|',
		positionBy: 'mouse',
		width: 300,
		leftOffset: 30,
		cluetipClass: 'jtip'
	});
	$("span.qtip").attr("title", function(){	/* developer comment tooltips */
		return ("Developer Question/Comment:|" + this.title);
	}).css({borderBottom: '1px dotted #666', backgroundColor: '#FF0000'}).cluetip({
		splitTitle: '|',
		positionBy: 'mouse',
		width: 400,
		leftOffset: 30,
		cluetipClass: 'jtip'
	});
}

//*************************************************
//	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"});
		});
	});
	/*
	$("#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 */
}

//*************************************************
//	Execute ALL jQuery 'ready' functions
$(function () {
	readyDropDowns();
	readyTabs();
	readyTableStripe();
	readyClueTip();
	readyAccess();
	readyOffsite();
	readyPopWin();
	readyPopWinAuto();
	readyFaq();
	readyPDF();
});

//*************************************************
//	IE background-image flicker bug fix
//	This should probably be the last JS executed

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

