// Deals with displaying and initially creating the .subnav element for the LI
function showPopOut(id) {
// If the .subnav doesn't already exist
	if (!$("."+id).length) {
		var pageid = id.substring(4);
		var poffset = $("#"+id).parent("ul").offset();
		var offset = $("#"+id).offset();

	// Creates our new <div> and adds the #text - Loading...
		$("<div><span class=\"loading\">Loading...</span></div>")
		// Add the class "subnav"
			.addClass(id).addClass("subnav")
		// Give it the same mouseover and mouseout abilities as the LI it will represent
			.mouseover(function(){ $("#mainnavigation #"+id).mouseover(); })
			.mouseout(function(){ $("#mainnavigation #"+id).mouseout(); })
		// Add to the <body>
			.appendTo("body");
			
		var idWidth = $("#"+id).width() + parseInt($("#"+id).css("paddingLeft").replace(/px/,"")) + parseInt($("#"+id).css("paddingRight").replace(/px/,""));
		var clWidth = $("."+id).width() + parseInt($("."+id).css("paddingLeft").replace(/px/,"")) + parseInt($("."+id).css("paddingRight").replace(/px/,""));
		var winWidth = $(window).width();
		var idLeft = offset.left;
		if ( (clWidth+idLeft) > winWidth) { idLeft = (offset.left + idWidth) - clWidth; }
			
	// Get the .subnav we just created
		$("."+id).css({
		// Positioning this .subnav under its li#
			position:"absolute",
		// We get want this to be just below the UL that houses our LI
			top:(poffset.top + $("#"+id).parent("ul").height()),
		// We want it to left align with the LI
			left:idLeft});

	// Get the child pages via AJAX
		if(pageid !== null && !isNaN(pageid)) {
		// Perform a GET
			$.get("/ClassLibrary/Page/SiteNavigation/ajax_childpages.cfml?page="+pageid,
				// Callback function calls another function
					function(data, textStatus){ writePopOut(data, textStatus, id); },
				// Data should be returned as JSON object
					"json");
		}
	}
}

// Populates the .subnav with the results of the AJAX call
function writePopOut(data, textStatus, id) {
	if ((textStatus == "success") && data.length) {
	// Create a JSON object from the data
		var children = eval("(" + data + ")");
	// Get the appropriate .subnav
		$subnav = $("."+id);
	// Create a new <ul> and add it to the .subnav after we clean it out
		$ul = $("<ul></ul>").appendTo($subnav.empty());
	// For each item in the JSON object...
		$.each(children, function(i){
		// ...give odd numbered items a class of "odd", evens a class of "even"
			if (i % 2) { className = "odd"; } else { className = "even"; }
		// ...create a new <li> add it to the <ul> above and give it appropriate class	
			$("<li></li>").appendTo($ul).addClass(className)
			// ...fill it with this HTML - an anchor with href and title
				.html('<a href="http://www.greatwesternbank.com'+this.url.replace(/index\.cfm/gi,"")+'" title="'+this.pagename+'">'+this.pagename+'</a>');
         });
	}
}

// jQuery equivalent of "onload"
$(document).ready(function(){
// Get all list items in #mainnavigation
	$("#mainnavigation li")
	// give them a mouseover
		.mouseover(function(){
			id = this.getAttribute("id");
		// Hide all other .subnavs
			$('.subnav').not("."+id).hide();
		// Add class of "hover"
			$(this).addClass("hover");
		// If there IS an id then...
			if (id.length) {
			// ...call showPopOut
				showPopOut(id);
			// ...show the resultant .subnav
				$("."+id).show();
			}
		})
	// give them a mouseout
		.mouseout(function(){
			id = this.getAttribute("id");
		// Remove class of "hover"
			$(this).removeClass("hover");
		// Hide all .subnavs
			$('.subnav').hide()
		});
		
	//Change the https to http for anything that doesn't have absolute url. This fixes the redirect problems.
	$("a[href^='/']").each(function(){
		if (this.href.indexOf('ClassLibrary') == -1 && this.href.indexOf('dev') == -1)
		this.href = this.href.replace(/https:\/\/[^\/]+/i,"http://www.greatwesternbank.com");
	});
});