/**
* File: navigation.js
* Author: Brian Schemp
* 
* History:
*
*/

 //called when the page is ready
$(document).ready(function() {
	//cache the top navigation list items.
	var topNavigationList = $("#top_navigation_list li");
			
	//set the hover event.
	topNavigationList.hover(
		function(){
			$(this).css("background-image", "none");
			$(this).find("a").css("color", "#6699CC");
			$(this).find("a").css("text-decoration", "underline");
		},
		function() {
			if(isLinkActive(($(this).attr("id")))) {
				$(this).css("background-image", "none");				
			}
			
			else {
				$(this).css("background-image", "url('/images/kauai-web-design/top-navigation-background.png')");
			}
						
			$(this).find("a").css("color", "#003366");
			$(this).find("a").css("text-decoration", "none");					
		}
	);
	
	//set the onClick event.
	topNavigationList.click(function() {
		window.location = $(this).find("a").attr("href");return false;		
	});
	
	//set the current active link.
	setCurrentLink();		
});

/**
 * Set the current active link for the top navigation.
 */
function setCurrentLink() {
	var mainURLLink = window.location.href;
	
	var servicesReqExp = /services/;
	var portfolioReqExp = /portfolio/;
	var contactUsRegExp = /contactus/;
	
	if(servicesReqExp.test(mainURLLink)) {		
		$("#top_navigation_services").css("background-image", "none");
	}
	
	else if(portfolioReqExp.test(mainURLLink)) {
		$("#top_navigation_portfolio").css("background-image", "none");
	}
	
	else if(contactUsRegExp.test(mainURLLink)) {
		$("#top_navigation_contactus").css("background-image", "none");
	}
	
	else {
		$("#top_navigation_home").css("background-image", "none");
	}		
}

/**
 * Checks if a current link is active or currently being viewed.
 * 
 * @param {Object} elementId
 * @return true/false
 */
function isLinkActive(elementId) {
	var mainURLLink = window.location.href;
	
	var homeRegExp = /home/;
	var servicesReqExp = /services/;
	var portfolioReqExp = /portfolio/;
	var contactUsRegExp = /contactus/;	
	
	if(servicesReqExp.test(mainURLLink) && servicesReqExp.test(elementId)) {
		return true;
	}
	
	else if(portfolioReqExp.test(mainURLLink) && portfolioReqExp.test(elementId)) {
		return true;
	}
	
	else if(contactUsRegExp.test(mainURLLink) && contactUsRegExp.test(elementId)) {
		return true;
	}
	
	else if(homeRegExp.test(mainURLLink) && homeRegExp.test(elementId))	{
		return true;
	}
	
	else {
		return false;
	}		
}

