$(document).ready(function() {	
	$(".global_nav li a.global_anchor").css( {backgroundPosition: "0 0"} );
	$("#homeNav a").css( {backgroundPosition: "0 0"} );
	
	var cc = document.getElementById("cc");
	var sp = document.getElementById("sp");
	var dr = document.getElementById("dr");
	var ps = document.getElementById("ps");
				
	
	$(".global_nav").hover(openNav, closeNav);
	
	// mouseover animations
    var subnavOver = false;
    var subnavOpen = false;
    var homeOver = false;

    for (var i = 1; i<5; i++){
		
		$("#homeNav").hoverIntent(
			function(){
				if($("#homeNav a").attr("class") != "active"){
					$("#homeNav a").animate({backgroundPosition:"(0px -34px)"},{duration:300});
				}
				
				if(subnavOpen){
					subnavOpen = false;
					homeOver = true;
					closeNav();
				}
			},
			function(){	
				$("#homeNav a").animate({backgroundPosition:"(0px 0px)"},{duration:300});
			}
		);
		
		var selectedNav;
		var selectedNavNo;
		
		var config = {
			//interval:10,
			//sensitivity:10,
			over:navItemCheck,
			out:navItemClose
		};
		
		$("#globalNav_" + i).hoverIntent(config);
	 }
	 
	 function navItemCheck(){
	 	navItemOver($(this));
	 }
	 
	 function navItemOver(hoveredNav){
		$(".subnav").hide().css({height: "0"});
		
		if(hoveredNav){selectedNav = hoveredNav.attr("id");}
		else{selectedNav = $(this).attr("id");}
		selectedNavNo = selectedNav.split("_");
			
		if($("#globalNav_" + selectedNavNo[1] + " a").attr("class") != "global_anchor active"){
			$("#globalNav_" + selectedNavNo[1] + " a.global_anchor").animate({backgroundPosition:"(0px -34px)"},{duration:300});
		}
			
		if(!subnavOpen && (selectedNav != "homeNav")){
			displayOverlay();
			
			$("#placeholder_nav").animate({height:"140px"}, {duration:300});
			$("#subnav" +  selectedNavNo[1]).animate({height:"140px"}, {duration:300});
			
			subnavOpen = true;
			homeOver = true;
		}
			
		if(subnavOpen){
			$(".subnav").hide().css({height: "0px"});
			$("#subnav" +  selectedNavNo[1]).css({height: "140px"}).fadeIn("800");
		}			
	}
			
	function navItemClose(){
		test = true;
		selectedNav = $(this).attr("id");
		selectedNavNo = selectedNav.split("_");
		$("#globalNav_" + selectedNavNo[1] + " a.global_anchor").animate({backgroundPosition:"(0px 0px)"},{duration:300});
	}

	function openNav(){
	
	}
		
	function closeNav(){
		setTimeout(hideOverlay, "10");
		subnavOpen = false;
		setTimeout(function(){
			if(selectedNav && homeOver){
				$("#placeholder_nav").animate({height:"0px"}, 300, function(){$("#placeholder_nav").hide();});
				//$("#globalNav_" + selectedNavNo[1] + " div").animate({height: "0"}, 400, function(){$("#globalNav_" + selectedNavNo[1] + " div").hide();});
				$("#globalNav_" + selectedNavNo[1] + " div").fadeOut("200");
				setTimeout(function(){$("#globalNav_" + selectedNavNo[1] + " div").hide()}, "200");
				homeOver = false;
			}
			
		}, "200");
	}
});