;(function($){
/*******************************************************************************************/	
// jquery.pajinate.js - version 0.2
// A jQuery plugin for paginating through any number of DOM elements
// 
// Copyright (c) 2010, Wes Nolte (http://wesnolte.com)
// Liscensed under the MIT License (MIT-LICENSE.txt)
// http://www.opensource.org/licenses/mit-license.php
// Created: 2010-04-16 | Updated: 2010-04-26
/*******************************************************************************************/

	$.fn.pajinate = function(options){
		// Set some state information
		var current_page = 'current_page';
		var items_per_page = 'items_per_page';
		
		var meta;
	
		// Setup default option values
		var defaults = {
			item_container_id : '.content',
			items_per_page : 10,			
			nav_panel_id : '.page_navigation',
			num_page_links_to_display : 20,			
			start_page : 0,
			nav_label_first : 'First',
			nav_label_prev : 'Prev',
			nav_label_next : 'Next',
			nav_label_last : 'Last'
		};
		var options = $.extend(defaults,options);
		var $item_container;
		var $page_container;
		var $items;
		var $nav_panels;
	
		return this.each(function(){
			$page_container = $(this);
			$item_container = $(this).find(options.item_container_id);
			$items = $page_container.find(options.item_container_id).children();
			meta = $page_container;
			
			// Initialise meta data
			meta.data(current_page,0);
			meta.data(items_per_page, options.items_per_page);
					
			// Get the total number of items
			var total_items = $item_container.children().size();
			
			// Calculate the number of pages needed
			var number_of_pages = Math.ceil(total_items/options.items_per_page);
			$('div.prev').html('<a class="previous_link" href=""><img src="img/prev.png" /></a>');
			$('div.next').html('<a class="next_link" href=""><img src="img/next.png" /></a>');
			
			if (total_items<=options.items_per_page) {
				$(".next_link").css("display","none");
			}
			// Hide the more/less indicators
			$('.ellipse').hide();
			
			// Set the active page link styling
			$('.previous_link').next().next().addClass('active_page');
			
			/* Setup Page Display */
			// And hide all pages
			$items.hide();
			// Show the first page			
			$items.slice(0, meta.data(items_per_page)).show();

			/* Setup Nav Menu Display */
			// Page number slices
			
			var total_page_no_links = $page_container.children(options.nav_panel_id+':first').children('.page_link').size();
			options.num_page_links_to_display = Math.min(options.num_page_links_to_display,total_page_no_links);
			
			// Event handler for 'Prev' link
			$('.previous_link').click(function(e){
				e.preventDefault();
				showPrevPage($(this));
			});

			// Event handler for 'Next' link
			$('.next_link').click(function(e){
				e.preventDefault();				
				showNextPage($(this));
			});
			
			// Goto the required page
			goto(parseInt(options.start_page));
		});
		
		function showPrevPage(e){
			new_page = parseInt(meta.data(current_page)) - 1;						
			tagNextPrev(parseInt(meta.data(current_page)),new_page);
			goto(new_page);
			
		};
			
		function showNextPage(e){
			new_page = parseInt(meta.data(current_page)) + 1;
			tagNextPrev(parseInt(meta.data(current_page)),new_page);
			goto(new_page);	
			
		};
			
		function goto(page_num){
			
			var ipp = meta.data(items_per_page);
			
			var isLastPage = false;
			
			// Find the start of the next slice
			start_from = page_num * ipp;
			
			// Find the end of the next slice
			end_on = start_from + ipp;
			// Hide the current page	
			$items.hide()
					.slice(start_from, end_on)
					.show();
			
			// Reassign the active class
			$page_container.find(options.nav_panel_id).children('.page_link[longdesc=' + page_num +']').addClass('active_page')
													 .siblings('.active_page')
													 .removeClass('active_page');										 
			
			// Set the current page meta data							
			meta.data(current_page,page_num);
			
						
			
};	
		
		function tagNextPrev(page_current,new_page) {
						
			if(page_current<new_page){				
				$('.next_link').css('display','none');
			} else {
				$('.next_link').css('display','block');
			}

			if(new_page==0){
				$('.previous_link').css('display','none');
			} else {
				$('.previous_link').css('display','block');
			}

		}		
				
		// Show or remove the ellipses that indicate that more page numbers exist in the page index than are currently shown
	};
	
})(jQuery);
