// JavaScript Document

$(document).ready(function(){	
	
	// Code pour marquer la section courante dans le menu primaire
	var currentPageHREF = window.location.pathname;
	//alert(currentPageHREF);
	$("#main-nav a").each(function() {			
		//alert("CurrentPageHref: " + currentPageHREF + " * This: " + $(this).attr("href"));		
		if(currentPageHREF == "/" || currentPageHREF == "/a-propos/nos-valeurs/" || currentPageHREF == "/a-propos/notre-clientele/") {
			//alert("1");
			imgsrc = $(this).children("img").attr("src");
			// Fail if no image found or if user is mousing over the current page button
			if (typeof(imgsrc) != 'undefined') {
				// Replace .gif with _over.gif
				imgsrcON = imgsrc.replace('.png', '_over.png');
				// Now change the src to the 'ON' value
				$(this).children("img").attr("src", imgsrcON);
			}
			$(this).attr("class", "current");
			return false;
		}		
		if(currentPageHREF.indexOf($(this).attr("href")) != -1) {
			//alert("2");
			if($(this).attr("href") != "/") {
				imgsrc = $(this).children("img").attr("src");
				// Fail if no image found or if user is mousing over the current page button
				if (typeof(imgsrc) != 'undefined') {
					// Replace .gif with _over.gif
					imgsrcON = imgsrc.replace('.png', '_over.png');
					// Now change the src to the 'ON' value
					$(this).children("img").attr("src", imgsrcON);
				}
				$(this).attr("class", "current");
			}
		}
		if(currentPageHREF.indexOf("/espace-clients/") != -1 || currentPageHREF.indexOf("Default.aspx") != -1) {
			//alert("3");			
			if($(this).attr("href") != "http://www.btca.qc.ca/") {
				imgsrc = $("#espace-client").children("img").attr("src");
				// Fail if no image found or if user is mousing over the current page button
				if (typeof(imgsrc) != 'undefined') {
					// Replace .gif with _over.gif
					imgsrcON = imgsrc.replace('.png', '_over.png');
					// Now change the src to the 'ON' value
					$("#espace-client").children("img").attr("src", imgsrcON);
				}
				$("#espace-client").attr("class", "current");
				return false;
			}
		}
	});
	
	// Main navigation image rollovers
	$("#main-nav a").mouseover(function() {
		imgsrc = $(this).children("img").attr("src");
		// Fail if no image found or if user is mousing over the current page button
		if (typeof(imgsrc) != 'undefined' && $(this).attr("class") != "current") {
			// Replace .gif with _over.gif
			imgsrcON = imgsrc.replace('.png', '_over.png');
			// Now change the src to the 'ON' value
			$(this).children("img").attr("src", imgsrcON);
		}
	});
	
	$("#main-nav a").mouseout(function(){
	  // trap for 'undefined' errors again
	  if (typeof(imgsrc) != 'undefined') {
		  $(this).children("img").attr("src", imgsrc);
	  }
	});		
	
	// Code pour marquer la section courante dans le menu secondaire
	$("#secondary-nav a").each(function() {				
		if(currentPageHREF == $(this).attr("href")) {			
			$(this).attr("class", "current");			
			$(this).parent("li").parent("ul").parent("li").children('a:first-child').attr("class", "current");	
		}
		// Show sub if on current page or parent page
		if($(this).attr("class") == "current") {
			//alert($(this).attr("href"));
			$(this).parent("li").children("ul").show();
			$(this).parent("li").parent("ul").parent("li").children("ul").show();
		}		
		/*if(currentPageHREF.indexOf($(this).attr("href")) != -1) {
			if($(this).attr("href") != "/") {
				$(this).attr("class", "current");
			}
			if(currentPageHREF == "/") {
				$(this).attr("class", "current");
			}
		}*/
	});
	
	// Secondary menu sub show-hide
	$("#secondary-nav ul li").mouseover(function() {
		//alert("Hey!");
		$(this).children("ul").show(); 
		//$("#secondary-nav ul li > ul").css("display", "visible");
	});
	
	$("#secondary-nav ul li").mouseout(function() {
		//alert($(this).children("a").attr("class"));
		if($(this).children("a").attr("class") != "current") {
			$(this).children("ul").hide(); 
		}
		//$("#secondary-nav ul li > ul").css("display", "none");
	});
	
});
