(function($){

	$.fn.pajination = 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 = {
			items_per_page : 5,
			item_container_id : 'ul',
			nav_panel_id : '.page_navigation',
			fadeTransition : true
		};
		var options = $.extend(defaults,options);
		var $item_container;
		var $page_container;
	
		return this.each(function(){
			$page_container = $(this);
			$item_container = $(this).find(options.item_container_id);
			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();
			
			if(total_items>options.items_per_page){
				
				// Calculate the number of pages needed
				var number_of_pages = Math.ceil(total_items/options.items_per_page);
				
				var init_page = 0;

				// Construct the nav bar
				var navigation_html = '<a class="previous_link" href="">&lt; Earlier</a>';
				var current_link = 0;
				while(number_of_pages > current_link){
					navigation_html += '<a class="page_link" href="" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
					current_link++;

				}
				navigation_html += '<a class="next_link" href="">Later &gt;</a>';
				
				// And add it to the appropriate area of the DOM			
				$page_container.find(options.nav_panel_id).html(navigation_html);
				
				// Bind the actions to their respective links
				$page_container.find('.previous_link').click(function(e){
					e.preventDefault();
					showPrevItem($(this));
				});
				
				$page_container.find('.next_link').click(function(e){
					e.preventDefault();				
					showNextItem($(this));
				});
				
				$page_container.find('.page_link').click(function(e){
					e.preventDefault();
					goto($(this).attr('longdesc'));
				});
				
				// Set the active page link styling
				$page_container.find(options.nav_panel_id + ' .previous_link').next().addClass('active_page');
				
				// And hide all pages
				$item_container.children().css('display', 'none');
				// Show the first page			
				$item_container.children().slice(0, meta.data(items_per_page)).css('display', 'block');	
			}
		});
		
		function showPrevItem(e){
			new_page = parseInt(meta.data(current_page)) - 1;						
			
			// Check that we aren't on a boundary link
			if($(e).siblings('.active_page').prev('.page_link').length==true){
				goto(new_page);
			}
				
		};
			
		function showNextItem(e){
			new_page = parseInt(meta.data(current_page)) + 1;
			
			// Check that we aren't on a boundary link
			if($(e).siblings('.active_page').next('.page_link').length==true){				
				goto(new_page);
			}
				
		};
			
		function goto(page_num){
			var ipp = meta.data(items_per_page);

			// 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	
			
			if(options.fadeTransition){
				$("#shows ul").fadeOut(function() {
				  $page_container.find(options.item_container_id).children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
				}).fadeIn();
			}else{
				$page_container.find(options.item_container_id).children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
			}
		
			// 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);
		};	
		
	};
	
})(jQuery);	
