jQuery(document).ready(function(){
	var show_per_page = 14;
	
	render(0,false);
	//add active_page class to the first page link
	jQuery('#page_navigation .page_link:first').addClass('active_page');

	//hide all the elements inside content div
	jQuery('#pagination').children().css('display', 'none');

	//and show the first n (show_per_page) elements
	jQuery('#pagination').children().slice(0, show_per_page).css('display', 'inline-block');

});

function previous(){

	new_page = parseInt(jQuery('#current_page').val()) - 1;
	//if there is an item before the current active link run the function
	if(jQuery('.active_page').prev('.page_link').length==true){
		go_to_page(new_page);
	}

}

function next(){
	new_page = parseInt(jQuery('#current_page').val()) + 1;
	//if there is an item after the current active link run the function
	if(jQuery('.active_page').next('.page_link').length==true){
		go_to_page(new_page);
	}

}
function go_to_page(page_num){
	//get the number of items shown per page
	var show_per_page = 14;
	
	//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
	jQuery('#pagination').children('div').css('display', 'none').slice(start_from, end_on).css('display', 'inline-block');

	/*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*/

	//update the current page input field
	jQuery('#current_page').val(page_num);
	
	render( page_num, true );
}

function resizeImgPage() {
var w = jQuery("#vprw").val();
var h = jQuery(this).children().first().height();

jQuery(this).children().last().height(h);
if ( h > w ) {
	jQuery(this).children().last().children().first().width(w+"px");
}else {
	jQuery(this).children().last().children().first().width(h+"px");
}

}

function render( cp, first ) {
	var show_per_page = 14;

	var number_of_items = jQuery('#pagination').children().size();

	var number_of_pages = Math.ceil(number_of_items/show_per_page);

	if ( number_of_pages < 2 )
		return false;
	jQuery('#current_page').val(cp);
	jQuery('#show_per_page').val(show_per_page);
	
	var ap = cp;
	cp++;
	var navigation_html = '';
	
	if ( ap != 0 ) {
		navigation_html += '<a class="previous_link" href="javascript:go_to_page(0);">&lt&lt</a>';
		navigation_html += '<a class="previous_link" href="javascript:previous();">&lt</a>';
	}
	
	var i = 0;
	
	var start = cp - 4;
	
	if ( start < 1 ) {
		start = 0;
	} 
	
	if ( cp < 5 ) {
		cp = 4;
	}

	if ( number_of_pages > 7 ) {
		if ( cp >= number_of_pages - 4 ) {
			start = number_of_pages - 5;
			
			navigation_html += '<a class="page_link" href="javascript:go_to_page(' + (1) +')" longdesc="' + (1) +'">'+ (1) +'</a>';
			navigation_html += '<a class="page_link" href="javascript:go_to_page(' + (1) +')" longdesc="' + (2) +'">'+ (2)+'</a>';
			navigation_html += '...'; 
		
			for ( i = start; i < number_of_pages; i++ ) {
				if (i == ap) {
					navigation_html += '<a class="page_link active_page" href="javascript:go_to_page(' + i +')" longdesc="' + i +'">'+ (i + 1) +'</a>';
				} else {				
					navigation_html += '<a class="page_link" href="javascript:go_to_page(' + i +')" longdesc="' + i +'">'+ (i + 1) +'</a>';
				}
			}
	
		} else {
			for ( i = start; i < cp+1; i++ ) {
				if ( i == ap ) {
					navigation_html += '<a class="page_link active_page" href="javascript:go_to_page(' + i +')" longdesc="' + i +'">'+ (i + 1) +'</a>';
				} else {
					navigation_html += '<a class="page_link" href="javascript:go_to_page(' + i +')" longdesc="' + i +'">'+ (i + 1) +'</a>';
				}
				
				if ( i + 1 == number_of_pages ) {
					break;
				}
			}
			navigation_html += '...';
			
			navigation_html += '<a class="page_link" href="javascript:go_to_page(' + (number_of_pages - 2) +')" longdesc="' + (number_of_pages - 2) +'">'+ (number_of_pages-1) +'</a>';
			navigation_html += '<a class="page_link" href="javascript:go_to_page(' + (number_of_pages - 1) +')" longdesc="' + (number_of_pages - 1) +'">'+ (number_of_pages )+'</a>';
		}
	} else {
		for ( i = start; i < number_of_pages; i++ ) {
			if (i == ap) {
				navigation_html += '<a class="page_link active_page" href="javascript:go_to_page(' + i +')" longdesc="' + i +'">'+ (i + 1) +'</a>';
			} else {				
				navigation_html += '<a class="page_link" href="javascript:go_to_page(' + i +')" longdesc="' + i +'">'+ (i + 1) +'</a>';
			}
		}
	}

	if ( ap != number_of_pages-1 ) {
		navigation_html += '<a class="next_link" href="javascript:next();">&gt</a>';
		navigation_html += '<a class="next_link" href="javascript:go_to_page('+(number_of_pages-1)+');">&gt&gt</a>';
	}
	
	jQuery('#page_navigation').html(navigation_html);

	if ( first ) {
		jQuery("#pagination").children(":visible").each(resizeImgPage);
	}
}

