/*--------------------------------------------------------------------------------------------

	File:		jquery.pageme.js
	Author:		David Robinson/Sumo Design
	Date:		2010-03-11

	Purpose:	Provides dynamic inline paging to arbitraty lists

	Usage example:
	
	$(document).ready(function(){

		// Instance 1
		pageme.init({
			className:'.p1',			// Class of the UL with the list items
			perPage:4,					// Items per page
			pagingElement:'.pagenums1a'	// Element in which to place page numbers
		});
		
		// Instance 2
		pageme.init({
			name:'list2',				// If using multiple instances, must add a unique name
			className:'.p2',
			perPage:3,
			pagingElement:'.pagenums2a'
		});
	});
--------------------------------------------------------------------------------------------*/

pageme={
	// Default settings
	name:'pageme',		// Unique name
	className:'',		// Class of list to paginate
	perPage:5,			// List items per page
	pagingElement:'',	// Element(s) to add paging to
	reset:false,

	init:function(o){

		// Override defaults
		if(o.name)this.name=o.name;
		if(o.className)this.className=o.className;
		if(o.pagingElement)this.pagingElement=o.pagingElement;
		if(o.perPage)this.perPage=o.perPage;
		if(o.before)this.before=o.before;
		if(o.after)this.after=o.after;
		if(o.reset)this.reset=o.reset;

		// Go
		this.page=this.getcookie(this.name);
		if(this.reset)this.page=1;
		this.splitItems();

		// Jump to last selected page on load
		if(this.page)this.changePage(this.name,this.className,this.page);
	},
	splitItems:function(){
		if(this.reset)for(var _int=0;_int<20;_int++)$('li').removeClass('pageme'+_int);
		if(this.reset)for(var _int=0;_int<20;_int++)document.cookie='pageme'+_int+'=';

		_li=$(this.className+' li');
		if(_li.length>0){
			for(var i=0,j=1,p=1;i<_li.length;i++,j++){
				if(j==(this.perPage+1)){j=1;p++;}
				$(_li[i]).addClass(this.name+p);
			}
			// Create page numbers
			this.paging(p);
		}
	},
	paging:function(num){

		// Build the page numbering
		var paging=$('<ul class="paginator ns '+this.name+'"><li class="first">Page:</li></ul>');
		for(var i=0;i<num;i++)paging.append('<li class="p'+this.name+(i+1)+'"><a href="#" onclick="return pageme.changePage(\''+this.name+'\',\''+this.className+'\',\''+(i+1)+'\')">'+(i+1)+'</a></li>');

		// Append page numbers to document
		pagingElement=(this.pagingElement)?this.pagingElement:this.className;
		$(pagingElement).html(paging);

		// Set current page to 1
		this.changePage(this.name,this.className,'1');
	},
	changePage:function(a,b,c){
		//alert(a);
		var _a=$('.p'+(a+c)).parent().find('li').find('a');
		if(_a.length){
			_a.removeClass('selected');
			if($('.p'+(a+c)).length)$('.p'+(a+c)).find('a').addClass('selected');
			if($(b+' li').length)$(b+' li').hide();
			if($(b+' li').parent().find('.'+(a+c)).length)$(b+' li').parent().find('.'+(a+c)).show();
			var expires=1000 * 60 * 60 * 24;
			document.cookie=a+'='+c;
		}
		return false;
	},
	getcookie:function(name){
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++){
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==name){
				return unescape(y);
			}
		}
	}
}

