$(document).ready(function(){
	//generic implementation
	var perPage = 6;
	var nextButton = $('div.Navigation a#navnext');
	var prevButton = $('div.Navigation a#navprev');
	var ulSelector = $('ul.PressList');
	var liSelector = $('ul.PressList li');
	var shownSelector = $(ulSelector).children('.show');
	var navContainer = $('div.Navigation.shortNav');
	var currentPage = $('span.CurrentPage');
	var numberPages = $('span.NumPages');
	var disabledClass = "disabled";
	
	//non-generic
	var initSelector = $('li.showInitial');
	
		
	pg = document.location.hash.replace('#p/','');
	if(pg==="") {
 		curPage = 1;
	} else if(isNaN(parseInt(pg,10))) {
		curPage = 1;
	} else {
 		curPage = parseInt(pg,10);
	}
	
	
	
	if($(liSelector).length > perPage){
		//show paging
		$(navContainer).show();
		
		$(numberPages).text(Math.ceil($(liSelector).length / perPage));
		$(currentPage).text(curPage);
		
		$(prevButton).addClass(disabledClass);
		$(nextButton).attr('href','#p/' + (curPage + 1));
		
		if (curPage == 1) {
			//NOTE: Custom addition for Camco. Probably not useful in a generic solution.
			
			if ($('.showInitial').length < 6) {
				var it = 6 - $(initSelector).length;
				
				for (i = 0; i < it; i++) {
					$(liSelector).eq(i).show().addClass('show').before($(liSelector).eq(it + i)); // show it and place it logically after the last .showInitial 
				}
			}
			else {
				$('.showInitial').addClass('show');
			}
			//end of custom addition
		}
		else
		{
			switchPages(curPage);
		}
	}
	
	
	$(prevButton).click(function(){
		if (curPage - 1  > 0) {
			window.location = $(this).attr('href');
			curPage--;
			switchPages(curPage);
			return false;
		}
	});
	$(nextButton).click(function(){
		if (curPage < ($(liSelector).length / perPage)) {
			curPage++;
			window.location = $(this).attr('href');
			switchPages(curPage);
			return false;
		}
	});
	
	
	// There is probably a better way of doing this
	function callBackOut(collection) {
		$(collection).eq(0).removeClass('show').addClass('transout').animate({opacity:0, left:'20px'},200,function(){
			callBackOut($('.show'));
		});
		if($(collection).length == 0)
		{
			$('.transout').removeClass('transout').css('display','none').css('opacity','1').css('left',0);
			
			var goTo = (window.startingPage + perPage + 1) > $(liSelector).length ? $(liSelector).length : (window.startingPage + perPage);
			
			for(i = window.startingPage; i < goTo; i++) {
				$(liSelector).eq(i).fadeIn('fast').addClass('show');	
			}
		}
	}
	
	function switchPages (page) {
		window.startingPage = ((page - 1) *6);
		
		$(currentPage).text(page);
		
		callBackOut($('.show'));
		
		if((page+1) > Math.ceil($(liSelector).length / perPage))
		{
			$(nextButton).addClass(disabledClass);
		}
		else
		{
			$(nextButton).removeClass(disabledClass);
			$(nextButton).attr('href',"#p/" + (page + 1));
		}
		
		if(((page)-1) < 1)
		{
			$(prevButton).addClass(disabledClass);
			$(prevButton).attr('href',"#p/1");
		}
		else
		{
			$(prevButton).removeClass(disabledClass);
			$(prevButton).attr('href',"#p/" + (page - 1));
		}
	}
})

