$(document).ready(function(){
	
	// First paragraph
	//$("<div class='page_break_container'>").insertBefore("p.pagestart");
	
	// Last paragraph
	//$("</div>").insertAfter("p.pageend");
	
	// Middle paragraphs
	//$("p.pagebreak").before("</div>").after("<div class='page_break_container'>");
	
	// Wrap all the P's in a div to do the page break with
	//$("p.pagebreak").nextUntil("p.pagebreak").css('background-color', 'green');
		
	for (i=0;i<=20;i++) {
		$("p.pagebreak:contains('"+i+"')").nextUntil("p.pagebreak:contains('"+(i+1)+"')").wrapAll("<div class='page_break_container'></div>");
		//alert(i);
	}
	
	/*
	
	$("p.pagebreak:contains('1')").nextUntil("p.pagebreak:contains('2')").wrapAll("<div class='page_break_container'></div>");
	$("p.pagebreak:contains('2')").nextUntil("p.pagebreak:contains('3')").wrapAll("<div class='page_break_container'></div>");
	$("p.pagebreak:contains('3')").nextUntil("p.pagebreak:contains('4')").wrapAll("<div class='page_break_container'></div>");

	*/
	
	
	//how much items per page to show  
    var show_per_page = 1;  
    //getting the amount of elements inside content div  
    var number_of_items = $('.page_break_container').size();  
    //calculate the number of pages we are going to have  
    var number_of_pages = Math.ceil(number_of_items/show_per_page);  
  
    //set the value of our hidden input fields  
    $('#current_page').val(0);  
    $('#show_per_page').val(show_per_page);  
	
	//alert("Number of items"+number_of_items);
  
    //now when we got all we need for the navigation let's make it '  
  
  	// Only allow content which needs pages to have the controls displayed
  	if (number_of_items > 0) {
  
		/* 
		what are we going to have in the navigation? 
			- link to previous page 
			- links to specific pages 
			- link to next page 
		*/  
		
		var current_link = 0;
	
		var navigation_html = '<a class="previous_link" href="javascript:previous();"><span class="previous_link">PREVIOUS</span></a> <span class="break">/</span> ';    
		while(number_of_pages > current_link){  
			//navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a> ';
			navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'"></a>';    
			current_link++;  
		}  
		navigation_html += '<a class="next_link" href="javascript:next();"><span class="next_link">NEXT</span></a>';
				  
		$('#page_navigation').html(navigation_html);  
	  
	  	// Hide the previous link
		$("span.previous_link").css('display', 'none');
		$("span.break").css('display', 'none');	
	  
		//add active_page class to the first page link  
		$('#page_navigation .page_link:first').addClass('active_page');  
	  
		//hide all the elements inside content div  
		$('.page_break_container').css('display', 'none');  
	  
		//and show the first n (show_per_page) elements  
		$('.page_break_container').slice(0, show_per_page).css('display', 'block');  
		
		}
  
});  
  
function previous(){  
  
    new_page = parseInt($('#current_page').val()) - 1;  
    //if there is an item before the current active link run the function  
    if($('.active_page').prev('.page_link').length==true){  
        go_to_page(new_page);  
    }  
  
}  
  
function next(){  
    new_page = parseInt($('#current_page').val()) + 1;  
    //if there is an item after the current active link run the function  
    if($('.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 = 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  
    $('.page_break_container').css('display', 'none').slice(start_from, end_on).css('display', '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*/  
    $('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');  
  
    //update the current page input field  
    $('#current_page').val(page_num);
	
	// Fist Page
	if ($("#current_page").val() == 0) {
		$("span.previous_link").css('display', 'none');
	} else {
		$("span.previous_link").css('display', 'inline');
	}
	
	// Add 1 to the current page val
	var current_page_total = parseInt($("#current_page").val()) + 1;
	
	// Last Page
	if(current_page_total == $(".page_break_container").size()){
		$("span.next_link").css('display', 'none');
	} else {
		$("span.next_link").css('display', 'inline');
	}
	
	if((current_page_total == $(".page_break_container").size()) || ($("#current_page").val() == 0)) {
		$("span.break").css('display', 'none');	
	} else {
		$("span.break").css('display', 'inline');
	}
	
}
