/* 	Copyright (c) WebPurity Limited
*	Author: Andrew Larkin (alarkin@webpurity.co.uk)
*	Description: JQuery code file for crofton-design.com
*/
$(document).ready(function() {
	highlightNav(); // Set current nav item		
	$("div.client a").each(function(index) { // Correct UI for client listings page
		//alert($(this).html());
		if($(this).html().indexOf("No project to display") >= 0)
		{
			$(this).remove();
		}
		else
		{
			$(this).empty();		
			$(this).addClass("client_more");
			$(this).text("");
			$(this).attr("title","View project details");	
		}
	});
	$("#project-item-list a.category-item").hover( function() {  // Correcy UI for project listings
			$(this).parent().parent().find("ul.current").removeClass("current");											 
	}, function () {
    		$(this).parent().parent().find("ul.current").addClass("current");	
  	});
});

/* Name: highlightNav
*  Description: Function to set the current nav item in the top and sub-level menus.
*/
function highlightNav()  {
	var isSubItem = false;
	var isTopLevel = false;
	
	var path = location.pathname.substring(1);	
        if ( path ) {
		// Find the navigation item for the page
		var node = $('.navigation-menu a[href$="' + path + '"]');
		if(node != null) {	    				
			isTopLevel = node.parent().parent().hasClass("top-level");
			//alert('isTopLevel: ' + isTopLevel);
			isSubItem = node.parent().hasClass("sub-item");
			//alert('isSubItem: ' + isSubItem);
			
			node.addClass('current');
			node.closest("li").addClass('current');
			if(isSubItem) {
				// sub-items need to set current on parent lists and list items
				node.closest("ul").addClass('current');		
				node.parent().parent().parent().find("a").addClass('current');
			}
		}	
        }
	
	if(isTopLevel) {
            // record the current top level navigation item
	    $.cookie('navigation.path2', path);
	    //alert('write topLevel: ' + path);
	} else {
		// read the last top level item and set it
		var topPath = $.cookie('navigation.path2');
                //var hiddenHint = $("#top-level-item");
                //if(hiddenHint) topPath = hiddenHint .val();
		//alert('read topLevel: ' + topPath);
		topNode =  $('.navigation ul li a[href$="' + topPath + '"]'); 
		if(topNode) topNode.addClass('current');	
	}
}
