(function($){

	$.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 = {
			items_per_page : 10,
			item_container_id : '.content',
			nav_panel_id : '.page_navigation'
		};
		var options = $.extend(defaults,options);
		var $item_container;
		var $page_container;
		var $items;
	
		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;
			
			// Store each item's display type so that they can be restored on each page
			$items.each(function(){
				$(this).data('display',$(this).css('display'));
			});
			
			// 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);
			
			// Construct the nav bar
			var navigation_html = '<a class="previous_link" href="">Previous Page</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=""> Next Page</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
			$items.css('display', 'none');
			// Show the first page			
			$items.slice(0, meta.data(items_per_page)).each(function(){
						$(this).css('display',$(this).data('display'));
					});			
		});
		
		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	
			//$items.data('display', $(this).css('display'));
			$items.css('display', 'none')
					.slice(start_from, end_on)
					.each(function(){
						$(this).css('display',$(this).data('display'));
					});
			
			// 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);
			
			// Reload our sifr heads
			sIFR.replace(univers_thin_uc, {
			  selector: 'h6',
			  css: '.sIFR-root {color: #fa7b13; font-size: 58px; font-weight: normal; padding: 0 0 0 0; margin: 0 0 0 0;}'
			});
			sIFR.replace(univers_light_uc, {
			  selector: 'h3',
			  css: '.sIFR-root {color: #333333;}'
			});
		};	
		
	};
	
})(jQuery);	

