(function($){
	
	$.paginateMe = function(el, options){
	
		var p = this; // Avoid scope issues
		p.$p = $(el); // Provide us with a DOM version of the element
		p.settings = $.extend({}, $.paginateMe.defaults, options);
		
		p.init = function(){
			p.item_count = p.$p.children().size(); // Counts the child elements within the object	
			p.page_count = Math.ceil(p.item_count/p.settings.show); // Calculates the number of pages we will have
			p.landmark = 0; // The landmark tells the script what page we're on
			if (p.page_count >= 2){
				p.build_navigation(); // Build and insert navigation if there are at least two pages
			}
			p.$p.children().hide(); // Hide all child elements within the element
			p.$p.children().slice(0, p.settings.show).show(); // Show the first "show_count" set of child elements
		};
		
		p.build_navigation = function(){
			p.current_link = 0;
			p.$page_nav = $('<div id="'+p.settings.nav_id+'"></div>')
			if (p.settings.top) p.$page_nav.insertBefore(p.$p); // Set the containing div
			if (!p.settings.top && p.settings.bottom) p.$page_nav.insertAfter(p.$p);
			p.$prev_link = $('<a class="prev_link">'+p.settings.prev_text+'</a>').css({'cursor':'pointer'}).appendTo(p.$page_nav); // Start with the 'prev' link
			while(p.page_count > p.current_link){ // Make a link for each page until the total page count has been reached
				p.$page_links += $('<a class="page_link" name="'+p.current_link+'">'+(p.current_link + 1)+'</a>').css({'cursor':'pointer'}).appendTo(p.$page_nav);
				p.current_link++;
			}
			p.$next_link = $('<a class="next_link">'+p.settings.next_text+'</a>').css({'cursor':'pointer'}).appendTo(p.$page_nav); // End with the 'next' link
			p.$page_nav.find('.page_link:first').addClass('active_page'); // Add active page to the first page link
			p.$page_link = p.$p.parent().find('.page_link'); // Grabs the set of page links for easy and unique reference
			p.$prev_link.addClass('inactive'); // There are no previous pages on the first page
			
			if (p.settings.top && p.settings.bottom) p.$cloned_nav = p.$page_nav.clone().insertAfter(p.$p); // CLONE
			
			$('.prev_link').click(function(){
				var new_page = p.landmark - 1;
				if (new_page >= 0) p.goto_page(new_page);
			});
			
			$('.next_link').click(function(){
				var new_page = p.landmark + 1;
				if(new_page < p.page_count) p.goto_page(new_page);
			});
			
			$('.page_link').click(function(){
				var new_page = parseInt($(this).attr('name')); // The value hidden in the anchor under 'name' is what we link to
				p.goto_page(new_page);
			});	
			
			p.goto_page = function(page_num){
				var start_from = page_num * p.settings.show; // The beginning of the current set of elements to display
				var end_on = start_from + p.settings.show; // The end of the current set of elements to display
				p.$p.children().hide().slice(start_from, end_on).show(); // Hide all the child elements for safety, then show the set of elements on the current page
				p.$p.parent().find('.page_link[name='+page_num+']').addClass('active_page').siblings('.active_page').removeClass('active_page'); // Unset and reset the 'active_page' class for css style control
				p.landmark = page_num;	// Sets the landmark value to the page number for navigation
				if (p.landmark+1 == p.page_count){
					p.$next_link.addClass('inactive').siblings('.inactive').removeClass('inactive'); // Set class for css stlying of the 'next' link if we're on the last page
				} else if (p.landmark == 0){
					p.$prev_link.addClass('inactive').siblings('.inactive').removeClass('inactive'); // Set class for css stlying of the 'prev' link if we're on the first page
				} else {
					p.$p.parent().find('.inactive').removeClass('inactive'); // Ensure that all inactive classes have been removed if we are not on the first or last page
				}
			}
		}
		p.init(); // Run the pagination setup
	};
	
	$.paginateMe.defaults = {
		show: 10, 						// How many items to show per page
		nav_id: 'page_navigation',		// Sets the <div> id to this value
		prev_text: 'prev',				// Sets the text on the previous link
		next_text: 'next',				// Sets the text on the next link
		top: true,						// Set the nav at the top (true or false)
		bottom: true					// Set the nav at the bottom (true or false)
	};
		
	$.fn.paginateMe = function(options){
		return this.each(function(){
			new $.paginateMe(this, options);	// paginateMe supports multiple instances on a single page
		});
	};
	
})(jQuery);
