var currentSel;
var timeout;
(function($) {
	// ONLOAD FUNCTION.
	$(document).ready(function() {
		$('#accordian').accordion({ autoHeight: true, collapsible: true, active: 3});
		var blog = false;
		var urlArr = document.URL.split("/");
		for( var i=0; i<urlArr.length; i++)
		{
			if(urlArr[i] == "blog")
			{
				blog = true;
			}
		}
		var pageNameArr = urlArr[urlArr.length -1];
	  	//alert("The page is" + urlArr[urlArr.length -2]);
		//if(!blog)
		//{
			hideNavEls(pageNameArr);
		//}
		createStoryItemsNav();
		//$('#storyTabsNoJS').attr("id","storyTabs");
		
		$('.storyTabDiv').hide("fast");
		$("#tab1Content").show("fast");
		if($('.storyTabDiv h3').size() == 1)
		{
			$('#storyTabs').remove();
		}
		else{
			$('#storyItems').css("height", "265px");
		}
		
		showSelSubNav();
		$('.subNav').hide();
		$("#currentSel .subNav").css("display","inline");
		$('#tabs .topLevelLi').hover(
			function(){
				clearTimeout(timeout);
				$('#tabs .topLevelLi').attr("class", "topLevelLi");
				$("#"+currentSel).attr("class", "topLevelLi current");
				$(this).attr("class", "topLevelLi current");
				$('.subNav').hide();
				$(this).children(".subNav").css("display","inline");
			},
			function(){
				
				//if(timeout != null){ timeout = null;}
				
				timeout = setTimeout("returnNavEls()", 500 );
				/*$(this).attr("class", "topLevelLi");
				$('.subNav').hide();
				$("#"+currentSel).attr("class", "topLevelLi current");
				showSelSubNav();*/
			}
		);
		$('.storyTabLinks').click(function(event){
			var thisId = $(this).attr("id");
			$('.storyTabDiv').hide("fast");
			$("#"+thisId+'Content').show("fast");
			$('#storyTabs li').removeAttr("id","current");
			$('#'+thisId).parent().attr("id","current");
			return false;
		});
		$('.accLink').css('font-weight', 'bold');
	});
})(jQuery);

function returnNavEls()
{
	//$(el).attr("class", "topLevelLi");
	$('.subNav').hide();
	$('#tabs .topLevelLi').attr("class", "topLevelLi");
	$("#"+currentSel).attr("class", "topLevelLi current");
	//$('#tabs .topLevelLi').attr("class", "topLevelLi");
	showSelSubNav();
	
}

function showSelSubNav()
{
	currentSel = $('.current').attr("id");
	$('#currentSel .subNav').show();	
}
function createStoryItemsNav()
{
	$('#storyItems').append("<ul id='storyTabs'></ul>");
	var index = 1;
	/*alert($('.storyTabDiv h3').size());
	*/
		$('.storyTabDiv h3').each(function(){
				
			$('#storyTabs').append("<li><a href='#' id='tab"+index+"' class='storyTabLinks'>"+$(this).text()+"</a></li>"); 
			
			$(this).parent().parent().attr("id", "tab"+index+"Content");
			index++;
		});
		$('#storyTabs li:first').attr("id", "current");
		
	/*}*/
}
function hideNavEls(currentPage)
{
	$(".topLevelLi").removeAttr("id");
	$(".topLevelLi").attr("class", "topLevelLi");
	$(".topLevelLi .topLevelLink").each(function(){
		var href = $(this).attr('href');		
		if(href == currentPage)
		{
			$(this).parent().attr("id","currentSel");
			$(this).parent().attr("class","topLevelLi current");
		}
		$(".subNav li a").removeAttr("id");
		$($(this) + ".subNav li a").each(function(){
			var href = $(this).attr('href');		
			
			if(href == currentPage)
			{
				$(this).parent().parent().parent().attr("id","currentSel");
				$(this).parent().parent().parent().attr("class","topLevelLi current");
				$(this).attr("id","currentSub");
				
			}
		});
	});
}