
$(document).ready(function(){   
	oContNav.init();
});

oContNav =
{
	pItemsPerPage : 9
	,
	pData : new Array()
	,
	pCurrLinkIndex : -1
	,
	init : function()
	{
		//Get list of html to write to contact nav.
		oContNav.pData = oContNav.importList();
		
		//Empty content nav div.
		$( 'div#contentNav').empty();
		
		//Write loading elements
		$( 'div#contentNav').append( '<div id="contNavList"></div>' ); //holds content nav list
		$( 'div#contentNav').append( '<div id="contNavPagination"></div>' ); //holds pagination interface
		
		//Get start page for pagination by index of link within list
		var myStartPage =  Math.floor( oContNav.pCurrLinkIndex / oContNav.pItemsPerPage );
		myStartPage = Math.max( 0, myStartPage );
		myStartPage = Math.min( oContNav.pData.length, myStartPage );
		if( isNaN( myStartPage ) ){ myStartPage = 0; };
		
		//Use pagination plugin to setup pagination interface
		
		$("div#contNavPagination").pagination( oContNav.pData.length, { items_per_page: oContNav.pItemsPerPage, current_page: myStartPage, callback: oContNav.setContent, link_to: 'javascript:;' });
		if( oContNav.pItemsPerPage >= oContNav.pData.length ){ $( 'div#contNavPagination' ).hide(); };
	}
	,
	//Set content nav based on pagination index
	//Pagination index sent from pagination plugin
	setContent : function( pmPageIndex, jq )
	{
		$( 'div#contNavList' ).html( '<ul></ul>' );
		var myStart = ( pmPageIndex ) * oContNav.pItemsPerPage;
		var myEnd = myStart + oContNav.pItemsPerPage;
		for( var i=myStart; i < myEnd; i++ )
		{
			$( 'div#contNavList ul' ).append( oContNav.pData[i] );
		};//end for
	}
	,
	//Returns a list of html links to write to the content nav
	importList : function()
	{
		//Track return list.
		var myReturn = new Array();
		//Iterate over the top content ul
		$( 'ul#contentList > li' ).each( function( pmIndex ){
			var myLiElement = $( this ).children( 'a:first' );
			var myClass = $( this ).attr( 'class' );
			
			var my_current_filename = oUrlUtil.getFileName();
			var my_filename = oUrlUtil.getFileName( myLiElement.attr( 'href' ) );
			var mySet = my_current_filename == my_filename;
			//console.log(my_current_filename,' ; ',my_filename,' ; ',mySet);
			
			//Also track current index of page link against content nav
			if( mySet )
			{
				oContNav.pCurrLinkIndex = pmIndex;
				myClass += ' active'; 
			};
			
			var myHtml = '<li class="' + myClass + '"><a href="' + myLiElement.attr( 'href' ) + '">' + myLiElement.text() + '<span></span></a></li>';
			myReturn.push( myHtml );
			
			//Check for sub navigation elements
			//Page within the sub set must be open to make the sub menu open
			
			var myArrSub = new Array();
			$( this ).children( 'ul' ).children().each( function( pmSubIndex ){
				var mySubLiElement = $( this ).children( 'a:first' );
				var myClass = $( this ).attr( 'class' ) + " sub";
				
				if( !mySet ){
					mySet = oUrlUtil.getFileName() == oUrlUtil.getFileName( mySubLiElement.attr( 'href' ) );
					//Also track current index of page link against content nav
					if( mySet )
					{ 
						oContNav.pCurrLinkIndex = pmIndex + pmSubIndex + 1;
						myClass += ' activeSub';
					};
				};//end if
				
				var myHtmlSub = '<li class="' + myClass + '"><a href="' + mySubLiElement.attr( 'href' ) + '">' + mySubLiElement.text() + '<span></span></a></li>';
				myArrSub.push( myHtmlSub );
			} );
			if( mySet ){ myReturn = myReturn.concat( myArrSub ); };
		} );
		
		//$.each(myReturn, function(index, value) { console.log(index + ': ' + value); });
		
		return( myReturn );
	}
	
};//end object literal oContNav

oUrlUtil =
{
	/*Returns the filename component of the URL sent.
	Defaults to the current URL if no parameter sent.*/
	getFileName : function( pmUrl )
	{
		if( pmUrl == null ){ pmUrl = window.location.href };
		return( pmUrl.replace(/.+[\/]([^\/]+)$/,'$1') );
	}//end function  getFileName

};//end object literal oUrl
