//2011-04-29 Jordan

$(document).ready(function(){

	//how much items per page to show
	show_per_page = 15;
	//how many page numbers before hyphenation
	page_numbers = 10;
	
	//setting vars in case of failure to set later in script
	left_elipses = '';
	right_elipses = '';
	pages_to_right = '';
	pages_to_left = '';
	
	//getting the amount of elements inside content div
	number_of_items = $('.postlist').children().size();
	//calculate the number of pages we are going to have
	number_of_pages = Math.ceil(number_of_items/show_per_page);
	
	pages_to_right = number_of_pages-1;
	
	//adding variables for hash tags
	re = new RegExp('#.+','g')
	current_page = document.URL;
	hash_number = String(current_page.match(re));
	hash_number = parseInt(hash_number.replace('#',''));
	hash_number = (hash_number - 1);

	if (hash_number != parseInt(hash_number)) {
	//this means a number was not supplied
	hash_number = 0;
	}
	
	//page base url, no hash tags
	current_page = current_page.replace(re, '');
	
	//set the value of our hidden input fields
	$('#current_page').val(0);
	$('#show_per_page').val(show_per_page);

	//now when we got all we need for the navigation let's make it
	navigation();
	
	//add active_page class to the first page link
	$('#page_navigation .page_link:first').addClass('active_page');

	//hide all the elements inside content div
	$('.postlist').children().css('display', 'none');

	//and show the first n (show_per_page) elements
	$('.postlist').children().slice(0, show_per_page).css('display', 'block');
	
	if (typeof hash_number != 'undefined') {
	//url supplies a number after # sign, go to that page
	go_to_page(hash_number);
	}
	
});

function navigation(){
	/*
	what are we going to have in the navigation?
		- link to previous page
		- links to specific pages
		- link to next page
	*/
	new_page = parseInt($('#current_page').val()) - 1;
	//if there is an item before the current active link run the function
	
	if(new_page >= 0){
		var navigation_html = '<a class="previous_link" href="'+current_page+'#'+parseInt($('#current_page').val())+'" onclick="javascript:go_to_page('+(parseInt($('#current_page').val()) - 1)+');">Prev</a>';
	} else {
		var navigation_html = '';
	}
	
	var current_link = 0;
	var first_link = '<a class="page_link" href="' + current_page + '#1" onclick="javascript:go_to_page(0)" longdesc="0">1</a>';
	var last_link = '<a class="page_link" href="' + current_page + '#' + number_of_pages + '" onclick="javascript:go_to_page(' + (number_of_pages - 1) + ')" longdesc="' + (number_of_pages - 1) + '">' + number_of_pages + '</a>';

	if (number_of_pages > page_numbers) {
	//page_numbers is defined at top
	//This means we need elipses and marching navigation
		current_link = parseInt(($('#current_page').val()));
		var pages_to_left = parseInt((page_numbers)/2);
		//console.log("pages to left is " + pages_to_left);
		//console.log("current link before manipulation is " + current_link);
		current_link = (current_link - pages_to_left);
		//console.log("current link - pages to left is " + current_link);
		if (current_link <= 0) {
			//current page is at the start of the set
			current_link = 0;
			pages_to_right = (page_numbers - 1);
			//-1 for the current page, so the total is the number the user wants displayed
			//console.log("current link was less than 0, pages_to_right is now" + pages_to_right);
			left_elipses = '';
			right_elipses = '...' + last_link;
		} else if ((current_link + pages_to_left) >= (number_of_pages - 1)) {
			//current page is at end of the set
			//console.log("End of set. Current link + pages to left is " + (current_link + pages_to_left) + " out of " + (number_of_pages - 1));
			current_link = (current_link + pages_to_left) - (page_numbers - 1);
			pages_to_right = (number_of_pages - 1);
			right_elipses = '';
			left_elipses = first_link + '...';
		} else {
		//page is in the middle of set
			pages_to_right = ((current_link + pages_to_left) + parseInt((page_numbers)/2));
			if (pages_to_right >= (number_of_pages - 1)) {
				pages_to_right = (number_of_pages - 1);
				right_elipses = '';
			}
			else {
				right_elipses = '...' + last_link;
			}
			//console.log("current link is " + current_link + ", pages to right is " + pages_to_right);
			if (current_link > 0) {
				left_elipses = first_link + '...';
			} else {
				left_elipses = '';
			}
		}
	}

	navigation_html += left_elipses;
	//console.log("while current_link ("+current_link+") <= pages_to_right ("+pages_to_right+") && number_of_pages ("+number_of_pages+") > 1");
	while(current_link <= pages_to_right && number_of_pages > 1){
		//console.log("current link is " + current_link);
		navigation_html += '<a class="page_link" href="' + current_page + '#' + (current_link + 1) + '" onclick="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
		current_link++;
	}
	navigation_html += right_elipses;
	
	new_page = parseInt($('#current_page').val()) + 1;
	//if there is an item after the current active link run the function
	if(new_page < number_of_pages){
		navigation_html += '<a class="next_link" href="' + current_page + '#' + (parseInt($('#current_page').val()) + 2) + '" onclick="javascript:go_to_page('+(parseInt($('#current_page').val()) + 1)+');">Next</a>';
	} else {
		navigation_html += '';
	}

	$('#page_navigation').html(navigation_html);

}

function go_to_page(page_num){
	//get the number of items shown per page
	var show_per_page = parseInt($('#show_per_page').val());

	//get the element number where to start the slice from
	start_from = page_num * show_per_page;

	//get the element number where to end the slice
	end_on = start_from + show_per_page;
	
	//hide all children elements of content div, get specific items and show them
	$('.postlist').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

	//update the current page input field
	$('#current_page').val(page_num);
	
	navigation();
	
	/*get the page link that has longdesc attribute of the current page and add active_page class to it
	and remove that class from previously active page link*/
	$('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
	
}

