/**
 * @author Nivaria Innova Team
 */

(function($){
	// Default configuration properties.
    var defaults = {
        current: 0,							//Current page
        total: 1,							//Number of pages in total
        groupBy: 3,							//Groups pages, maximal number of links is (groupBy*3+2) 
		style: "pagination-flickr",			//Style of pager
		prevText: "Previous",				//Text for prev element
		nextText: "Next",					//Text for next element
		prevImage: null,					//Image for prev element
		nextImage: null,					//Image for next element
		hidePages: false,					//Hide the pages and show only prev and next elements 
		allowChangePage: false,				//Is used for global pager (allow or prohibid the global pager navigation), normally is not used
		globalPage: 0,						//Global page number, normally is not used
		globalPagesCount: 1,				//Global pages total number, normally is not used
		alignWidth: false,					//Recalculate the width of pager and apply it to parent div
		linkMargin: 4,                      //Margin used in links, this parameter allows calculate correctly the width of pager
		object: null,                       //Parent object for the pager element
		method: null,                       //Method of parent object to apply for each link,
		withitpp: false,					//Generate items per page combobox
		dsprefix: "",						//Datasource prefix
		styleitpp: "list-items-per-page",	//Style of items per page combobox
		methoditpp: null,					//Method of parent object to apply for items per page combobox
		optionsitpp: [1,2,3,4,5,6,7,8,9,10,12,15,18,20,24,30,40,50,999999],
		titleitpp: "Show",					//Title for items per page combobox
		allText: "All",						//Text for "all" option
		ofText: "of",						//Text for "of" element
		itpp: 0,							//Items per page
		totalItems: 0						//Total of items								
    };
	
	$.fn.jpager = function(o) {
        return this.each(function() {
            new $jp(this, o);
        });
    };
	
	$.jpager = function(e, o){
		var self = this;
		this.options = $.extend({}, defaults, o || {});
		var arrPg = this.setup();
		var html = new Array();
		if(this.options.withitpp) {
			html.push("<select id=\"");
			html.push(this.options.dsprefix);
			html.push("cmbpage\" class=\"");
			html.push(this.options.styleitpp);
			html.push("\" title=\"");
			html.push(this.options.titleitpp);
			html.push("\" name=\"");
			html.push(this.options.dsprefix);
			html.push("cmbpage\">");
			$.each(this.options.optionsitpp,function(i,opt){
				html.push("<option value=\"");
				html.push(opt);
				html.push("\"");
				html.push(opt==self.options.itpp?" selected=\"selected\"":"");
				html.push(">");
				html.push(self.options.titleitpp);
				html.push(" ");
				html.push(opt==999999?self.options.allText:opt);
				html.push("<\/option>");
			});
			html.push("<\/select>");
			if(this.options.totalItems>0) {
				html.push("<span>");
				html.push(this.options.ofText);
				html.push("<em>");
				html.push(this.options.totalItems);
				html.push("<\/em><\/span>");	
			}
		}
		if(this.options.total>1 || this.options.globalPagesCount>1) {
			html.push("<ul class=\"");
			html.push(this.options.style);
			html.push(this.options.hidePages?" hidden-pages":"");
			html.push("\">");
			html.push("<li class=\"");
			html.push(this.options.current>0 || (this.options.allowChangePage && this.options.globalPage>0)?"previous":"previous-off");
			html.push("\">");
			html.push(this.options.current>0 || (this.options.allowChangePage && this.options.globalPage>0)?"<a href=\"noscript.html\" title=\"\">":"");
			if(this.options.prevImage!=null) {
				html.push("<img src=\"");
				html.push(this.options.prevImage);
				html.push("\" alt=\"");
				html.push(this.options.prevText);
				html.push("\" title=\"");
				html.push(this.options.prevText);
				html.push("\" \/>");
			} else {
				html.push("«");
				html.push(this.options.prevText);			
			}
			html.push(this.options.current>0 || (this.options.allowChangePage && this.options.globalPage>0)?"</a>":"");
			html.push("</li>");
			if (!this.options.hidePages) {
				for (var i = 0; arrPg != null && i < arrPg.length; i++) {
					html.push("<li");
					html.push(arrPg[i]==""+this.options.current?" class=\"active\"":"");
					html.push(arrPg[i]==""?" class=\"empty\"":"");
					html.push(">");
					html.push(arrPg[i]!=""+this.options.current && arrPg[i]!=""?"<a href=\"noscript.html\" title=\"\">":"");
					html.push(arrPg[i]==""?"...":""+(parseInt(arrPg[i])+1));
				      html.push(arrPg[i]!=""+this.options.current && arrPg[i]!=""?"</a>":"");
					html.push("</li>");
				}
			}	
			html.push("<li class=\"");
			html.push(this.options.current<this.options.total-1 || (this.options.allowChangePage && this.options.globalPage<this.options.globalPagesCount-1)?"next":"next-off");
			html.push("\">");
			html.push(this.options.current<this.options.total-1 || (this.options.allowChangePage && this.options.globalPage<this.options.globalPagesCount-1)?"<a href=\"noscript.html\" title=\"\">":"");
			if(this.options.nextImage!=null) {
				html.push("<img src=\"");
				html.push(this.options.nextImage);
				html.push("\" alt=\"");
				html.push(this.options.nextText);
				html.push("\" title=\"");
				html.push(this.options.nextText);
				html.push("\" \/>");
			} else {
				html.push(this.options.nextText);
				html.push(" »");
			}
			html.push(this.options.current<this.options.total-1 || (this.options.allowChangePage && this.options.globalPage<this.options.globalPagesCount-1)?"</a>":"");
			html.push("</li>");
			html.push("</ul>");
		}
		$(e).html(html.join(""));
		if($.browser.msie && this.options.prevImage==null && this.options.nextImage==null && !this.options.hidePages) {
			$("ul li",e).each(function(i) {
				var w = "20px";
				if($("a",this).length>0) {
					w = ($("a",this).width()+16)+"px";
				} else {
					w = $(this).text().length*8+"px";
				}
				$(this).css("width",w).css("float","left");
			});
		}
		/* Calculates real width of pager and apply this width to parent container */
		if(this.options.alignWidth) {
	        var total_width=48;
	        $("ul li",e).children().each(function (i){
	            total_width=total_width+$(this).outerWidth()+self.options.linkMargin;
	        });
	        $(e).css("width",total_width+"px").find("ul").css("width",total_width+"px");
		}
		//Defines the events for link elements
		if(this.options.object && this.options.method) {
			if(this.options.object[this.options.method]) {
				if($.isFunction(this.options.object[this.options.method])){
					var jqLis = $("ul li",e);
					jqLis.each(function(index){
						if(index==0 && self.options.current>0) {
							$(this).find("a").click(function(){
								self.options.object[self.options.method](self.options.current-1);
								return false;
							}).keypress(function(){
								self.options.object[self.options.method](self.options.current-1);
								return false;
							});
						} 
						if(index==jqLis.length-1 && self.options.current<self.options.total-1) {
							$(this).find("a").click(function(){
								self.options.object[self.options.method](self.options.current+1);
								return false;
							}).keypress(function(){
								self.options.object[self.options.method](self.options.current+1);
								return false;
							});
						}
						if(index>0 && index<jqLis.length-1) {
							$(this).find("a").click(function(){
								var pg = parseInt($.trim($(this).html()))-1;
								self.options.object[self.options.method](pg);
								return false;
							}).keypress(function(){
								var pg = parseInt($.trim($(this).html()))-1;
								self.options.object[self.options.method](pg);
								return false;
							});
						}
					});
				}
			}
		}
		//Define events for item per page element
		if(this.options.withitpp) {
			if(this.options.object && this.options.methoditpp) {
				if (this.options.object[this.options.methoditpp]) {
					if ($.isFunction(this.options.object[this.options.methoditpp])) {
						$("select",e).change(function(){
							self.options.object[self.options.methoditpp](this.value);
						});
					}
				}	
			}	
		}	
	};
	
	// Create shortcut for internal use
    var $jp = $.jpager;

    $jp.fn = $jp.prototype = {
        jpager: '1.0.0'
    };

    $jp.fn.extend = $jp.extend = $.extend;

    $jp.fn.extend({
		setup: function() {
			var pg_max=this.options.groupBy*3+2;
    		var maxSize=(this.options.total<pg_max)?this.options.total:pg_max;
    		var arrPg = new Array(maxSize);
    		if(this.options.total<=pg_max)
        		for(var i=0;i<this.options.total;i++) arrPg[i] = ""+i;
    		else {
        		if(this.options.current<this.options.groupBy*2 || (this.options.current>=this.options.total-this.options.groupBy*2 && this.options.current<this.options.total)) {
            		if(this.options.current<this.options.groupBy*2) {
                		for(var k=0;k<pg_max;k++) {
                    		if(k<=this.options.groupBy*2) arrPg[k]=""+k;
                    		if(k==this.options.groupBy*2+1) arrPg[k]="";
                    		if(k>this.options.groupBy*2+1) arrPg[k]=""+(this.options.total-(pg_max-k));
                		}
            		}
            		if(this.options.current>=this.options.total-this.options.groupBy*2 && this.options.current<this.options.total) {
                		for(var k=0;k<pg_max;k++) {
                    		if(k<this.options.groupBy) arrPg[k]=""+k;
                    		if(k==this.options.groupBy) arrPg[k]="";
                    		if(k>this.options.groupBy) arrPg[k]=""+(this.options.total-(pg_max-k));
                		}
            		}
        		} else {
            		for(var k=0;k<pg_max;k++) {
                		if(k<this.options.groupBy) arrPg[k]=""+k;
                		if(k==this.options.groupBy) arrPg[k]="";
                		if(k>this.options.groupBy*2+1) arrPg[k]=""+(this.options.total-(pg_max-k));
                		if(k==this.options.groupBy*2+1)arrPg[k]="";
                		if(k>this.options.groupBy && k<this.options.groupBy*2+1) arrPg[k]=""+(this.options.current-(this.options.groupBy*2+1-k)+2);
            		}
            		if(parseInt(arrPg[this.options.groupBy+1])-parseInt(arrPg[this.options.groupBy-1])==2)
                		arrPg[this.options.groupBy]=""+(parseInt(arrPg[this.options.groupBy-1])+1);
            		if(parseInt(arrPg[this.options.groupBy*2+2])-parseInt(arrPg[this.options.groupBy*2])==2)
                		arrPg[this.options.groupBy*2+1]= ""+(parseInt(arrPg[this.options.groupBy*2])+1);
        		}
    		}
    		return arrPg;
		}		
	});	
})(jQuery); 
