// ::::: JQuery to handle directory data :::::

$(document).ready(function(){

	// Select the first directory list item
	$("#clientlist").children(":first").attr("class", "current");

	// Show the first info block
	$("#dir_navInfo_contentbox").children(":first").show();

	// Show the prev/next pagination links
	$("#dir_navInfo_nav a").hide();

	// If not last element then get next
	if( $("#clientlist li:first a").attr("id") != $("#clientlist li:last a").attr("id") ) {
		var nextElement = $("#clientlist li:eq(1) a");
		var nextElementID = nextElement.attr("id").substring( nextElement.attr("id").indexOf("_"), nextElement.attr("id").length );
		$("#dir_navInfo_nav a:last").attr("id", "next" + nextElementID);
		$("#dir_navInfo_nav a:last").show();
	}
			
	
	// ::::: Do this when a DIRECTORY LINK is clicked on ::::::
	$("#clientlist a").click(function(e){
									  
		
//alert( "(this).parent.attr(id): " + $(this).parent().attr("id") );
		
		if( $(this).parent().attr("id") != "dir_navList_nav" ) {
		
			// stop normal link click
			e.preventDefault();
	
			// Reset all directory links
			$("#clientlist > li").attr("class", "");
					
			// hide all directory details
			$("#dir_navInfo_contentbox > div[id]").hide();
				
			// Set/highlight current link to current
			$(this).parent().attr("class", "current");
	
	
			var showinfo = "#clientinfo" + getID( $(this) );
			// Show directory info 		
			$(showinfo).show();
	
			// ::: Set the bottom prev | next navigation links :::
			showHidePrev( getID( $(this) ) );
			showHideNext( getID( $(this) ) );
		}
		
	});
	
	

	// ::::: Do this when a CONTENT PREV | NEXT NAV is clicked on ::::::
	$("#dir_navInfo_nav a").click(function(e){

		// stop normal link click
		e.preventDefault();

		// hide all
		// Reset all directory links
		$("#clientlist > li").attr("class", "");
				
		// hide all directory details
		$("#dir_navInfo_contentbox > div[id]").hide();
		
		// Get directory ID
		var clientid = getID( $(this) );

		var highlightlink = "#listid_" + clientid;
		$(highlightlink).parent().attr("class", "current");
		
		// highlight directory link
		var dirLink = "#listid" + clientid;
		$(dirLink).parent().attr("class", "current");
		
		var showinfo = "#clientinfo" + clientid;
		// Show directory info 		
		$(showinfo).show();
	
	
		// ::: Set the bottom prev | next navigation links :::
		showHidePrev(clientid);
		showHideNext(clientid);
		
	});


	function getID( element ) {
		return element.attr("id").substring( element.attr("id").indexOf("_"), element.attr("id").length );	
	}
	
	function getFirstElementID () {
		var firstElement = $("#clientlist li:first a");
		var firstElementID = firstElement.attr("id").substring( firstElement.attr("id").indexOf("_"), firstElement.attr("id").length );	
		return firstElementID;
	}
	function getLastElementID () {
		var lastElement = $("#clientlist li:last a");
		var lastElementID = lastElement.attr("id").substring( lastElement.attr("id").indexOf("_"), lastElement.attr("id").length );
		return lastElementID;
	}
	
	function showHidePrev ( id ) {
		var dirLink = "#listid" + id;
		var firstElement = "#listid" + getFirstElementID();
		
		if( $(dirLink).attr("id") == $(firstElement).attr("id") ) {
			$("#dir_navInfo_nav a:first").hide();
		}else {
			var prevElement = $(dirLink).parent().prev().children(":first");
			var prevElementID = prevElement.attr("id").substring( prevElement.attr("id").indexOf("_"), prevElement.attr("id").length );
			$("#dir_navInfo_nav a:first").attr("id", "prev" + prevElementID);			
			$("#dir_navInfo_nav a:first").show();
		}
	}
	
	function showHideNext ( id ) {
		var dirLink = "#listid" + id;
		var lastElement = "#listid" + getLastElementID();
			
		if( $(dirLink).attr("id") == $(lastElement).attr("id") ) {
			$("#dir_navInfo_nav a:last").hide();
		}else {
			var nextElement = $(dirLink).parent().next().children(":first");
			var nextElementID = nextElement.attr("id").substring( nextElement.attr("id").indexOf("_"), nextElement.attr("id").length );
			$("#dir_navInfo_nav a:last").attr("id", "next" + nextElementID);
			$("#dir_navInfo_nav a:last").show();
		}
	}


});
