$(document).ready(function() {
	var thisMenu;
	var thisMenuItem;
	var activeMenu;
	var ha = window.location.hash;
	// binding navigation elements to hover
	$("#nav_home").hover(showMenu,hideMenu);
	$("#nav_home").click(function(){
		document.location=$("#nav_home a").attr("href");
	});
	$("#nav_aboutUs").hover(showMenu,hideMenu);
	$("#nav_inspectionSvc").hover(showMenu,hideMenu);
	$("#nav_inspectionSvc").click(function(){
		if (ha!="" && ha!="product_inspections") {
			window.location.hash="product_inspections";
		}
		//document.location=$("#nav_inspectionSvc a").attr("href");

		$("#nav_inspectionSvc").addClass("nav_inspectionSvc_hover");
		$("#nav_inspectionSvc .nav_heading a").css({color:"#fff"});
		
		$("#nav_labTest").removeClass("nav_labTest_hover");
		$("#nav_labTest .nav_heading a").css({color:"#000"});
		
		$("#nav_audits").removeClass("nav_audits_hover");
		$("#nav_audits .nav_heading a").css({color:"#000"});
		
		activeCategory();
	});
	$("#nav_audits").hover(showMenu,hideMenu);
	$("#nav_audits").click(function(){
		if (ha!="" && ha!="audit") {
			window.location.hash="audit";
		}
		//document.location=$("#nav_audits a").attr("href");

		$("#nav_audits").addClass("nav_audits_hover");
		$("#nav_audits .nav_heading a").css({color:"#fff"});
		
		$("#nav_inspectionSvc").removeClass("nav_inspectionSvc_hover");
		$("#nav_inspectionSvc .nav_heading a").css({color:"#000"});
		
		$("#nav_labTest").removeClass("nav_labTest_hover");
		$("#nav_labTest .nav_heading a").css({color:"#000"});		
		
		activeCategory();
	});
	$("#nav_labTest").hover(showMenu,hideMenu);
	$("#nav_labTest").click(function(){
		if (ha!="" && ha!="laboratory_testing") {
			window.location.hash="laboratory_testing";
		}
		//document.location=$("#nav_labTest a").attr("href");

		$("#nav_labTest").addClass("nav_labTest_hover");
		$("#nav_labTest .nav_heading a").css({color:"#fff"});
		
		$("#nav_inspectionSvc").removeClass("nav_inspectionSvc_hover");
		$("#nav_inspectionSvc .nav_heading a").css({color:"#000"});
		
		$("#nav_audits").removeClass("nav_audits_hover");
		$("#nav_audits .nav_heading a").css({color:"#000"});		
		
		activeCategory();
	});
	$("#nav_news").hover(showMenu,hideMenu);
	$("#nav_news").click(function(){
		document.location=$("#nav_news a").attr("href");
	});
	$("#nav_createAcct").hover(showMenu,hideMenu);
	$("#nav_createAcct").click(function(){
		document.location=$("#nav_createAcct a").attr("href");
	});
	$("#nav_contactUs").hover(showMenu,hideMenu);
	$("#nav_contactUs").click(function(){
		document.location=$("#nav_contactUs a").attr("href");
	});
	$(".nav_menu_item").hover(highlightOn,highlightOff);
	
	/*$(".nav_element").bind("mouseout",function(){
		//alert($(this).attr("id"));
		console.log($("div>a",this).attr("href"));
	});*/
	
	// keeps current category highlighted on navigation bar
	activeCategory();
 });

function showMenu(){
	thisMenu = $(this).attr("id");
	$("#" + thisMenu + "_menu").removeClass("hide");
	$("#" + thisMenu).addClass(thisMenu + "_hover");
	$("#" + thisMenu + " .nav_heading a").css({color:"#fff"});
	$("#" + thisMenu + " .topMenuLink").css({color:"#fff"});
	
	if(thisMenu == "nav_contactUs"){
		//$("#" + thisMenu + " .topMenuLink").css({color:"#fff"});
		$(".arrowRed").addClass("arrowWhite");
	}
		
};

function hideMenu(){
	//console.log(activeMenu+"/"+thisMenu);
	//console.log(thisMenu.indexOf(activeMenu));
	$("#" + thisMenu + "_menu").addClass("hide");
	if(thisMenu.indexOf(activeMenu)!=-1){return false;}
	$("#" + thisMenu).removeClass(thisMenu + "_hover");
	$("#" + thisMenu + " .nav_heading a").css({color:"#000"});
	$("#" + thisMenu + " .topMenuLink").css({color:"#000"});
	
	if (thisMenu == "nav_contactUs") {
	$("#" + thisMenu + " .topMenuLink").css({color:"#b31820"});
	$(".arrowRed").removeClass("arrowWhite");
	}
};

/*function resetMenu() {
	$('#navigation div').each(function(i, e) {
		$(e).removeClass('nav_inspectionSvc_hover');
		$(e).removeClass('nav_audits_hover');
		$(e).removeClass('nav_labTest_hover');
		$(e + ' a').css({color:"#000"});
		$(e + " .nav_heading a").css({color:"#000"});
		$(e + " .topMenuLink").css({color:"#000"});
	});
}*/


function highlightOn(){
	thisMenuItem = "#" + $(this).children("a").attr("id");
	$(thisMenuItem).parent("div").css({background:"#e8e8e8"});
};

function highlightOff(){
	$(thisMenuItem).parent("div").css({background:"#fff"});
};

function activeCategory() {
	currentUrl = document.location.href;
	
	// gets sub-directory
	activeMenu = currentUrl.replace("http://aitest.asiainspection.com/","");

	//	assigns category to sub-directory pages
	if (activeMenu == "") {
		activeMenu = "home";
	}
	else {
		if (activeMenu.indexOf("inspections-audits-tests-pricing")>=0 || activeMenu.indexOf("inspectors-network-asia")>=0 || activeMenu.indexOf("who-are-we")>=0 || activeMenu.indexOf("Our8Principles")>=0 || activeMenu.indexOf("code-of-ethics")>=0 || activeMenu.indexOf("current-opportunities")>=0 || activeMenu.indexOf("job-application")>=0){
			activeMenu = "aboutUs";
		}
		else if (activeMenu.indexOf("c-tpat-compliance")>=0 || activeMenu.indexOf("audit")>=0 ){
			activeMenu = "audits";
		}
		else if (activeMenu.indexOf("testing")>=0 || activeMenu.indexOf("laboratory_testing")>=0 || activeMenu.indexOf("en71-toy-safety")>=0){
			activeMenu = "labTest";  
		}
		else if (activeMenu.indexOf("pre-shipment-inspection")>=0 || activeMenu.indexOf("production-monitoring")>=0 || activeMenu.indexOf("during-production-inspection")>=0 || activeMenu.indexOf("initial-production-check")>=0 || activeMenu.indexOf("container-loading-check")>=0 || activeMenu.indexOf("quality-control-services-china-india-asia")>=0 || activeMenu.indexOf("product_inspections")>=0){
			activeMenu = "inspectionSvc";
		}
		else if (activeMenu.indexOf("asia-inspection-news")>=0) {
			activeMenu = "news";
		}
		else if (activeMenu.indexOf("register")>=0){
			activeMenu = "register";
		}
		else if (activeMenu.indexOf("contact-us")>=0){
			activeMenu = "contact-us";
		}
	}

	switch (activeMenu) {
	
	case "home":
		$("#nav_home").addClass("nav_home_hover");
		$("#nav_home .nav_heading a").css({color:"#fff"});
		$("#nav_home .topMenuLink").css({color:"#fff"});
		break;
	
	case "aboutUs":
		$("#nav_aboutUs").addClass("nav_aboutUs_hover");
		$("#nav_aboutUs .nav_heading a").css({color:"#fff"});
		break;
		
	case "inspectionSvc":
		$("#nav_inspectionSvc").addClass("nav_inspectionSvc_hover");
		$("#nav_inspectionSvc .nav_heading a").css({color:"#fff"});
		break;	
		
	case "audits":
		$("#nav_audits").addClass("nav_audits_hover");
		$("#nav_audits .nav_heading a").css({color:"#fff"});
		break;
		
	case "labTest":
		$("#nav_labTest").addClass("nav_labTest_hover");
		$("#nav_labTest .nav_heading a").css({color:"#fff"});
		break;
		
	case "news":
		$("#nav_news").addClass("nav_news_hover");
		$("#nav_news .nav_heading a").css({color:"#fff"});
		$("#nav_news .topMenuLink").css({color:"#fff"});
		break;
		
	case "register":
		$("#nav_createAcct").addClass("nav_createAcct_hover");
		$("#nav_createAcct .topMenuLink").css({color:"#fff"});		
		break;
		
	case "contact-us":
		$("#nav_contactUs").addClass("nav_contactUs_hover");
		$("#nav_contactUs .nav_heading a").css({color:"#fff"});
		$("#nav_contactUs .topMenuLink").css({color:"#fff"});
		$(".arrowRed").addClass("arrowWhite");
		break;
		
	default:
		break;	
	}
};
