//http://en.newinstance.it/2006/09/27/client-side-html-table-pagination-with-javascript/

/**********************************

JSP pages involved : starting page = pageing.jsp
ajax painting page : page_test.jsp

*****************************************/
//function Pager(pageURL,contentId, itemsPerPage,currentPage,totalRecords,pagesToDisplay) {
function Pager(pageURL,contentId, itemsPerPage,totalRecords,pagesToDisplay) {
    this.contentId = contentId;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    //this.currentPage = currentPage;
    //this.pages = 0;
    this.inited = false;
    
    this.totalRecords = totalRecords;
    this.pagesToDisplay = pagesToDisplay;
    this.pageURL = URLDecode(pageURL); //url that will be called to refresh the page
    this.buttonclicked = 'next';
    this.setbuttonclicked = '';
    
    //this.showRecords = function(from, to) {   
    this.showRecords = function(from, to,content_divId) {   
    
    	var query = fleegix.uri.getQuery(this.pageURL);
		var url_base = fleegix.uri.getBase(this.pageURL); //portion before the question mark
		query = fleegix.uri.setParam('pageroffset', from - 1, query);  
		query = fleegix.uri.setParam('curpage', this.currentPage, query);   
		query = fleegix.uri.setParam('paginate', 'Y', query);   
       
        //var params = "pageroffset="+from+"&curpage="+this.currentPage;
        //alert(url_base+"?"+query);
        //alert(content_divId);
		var opt = {
				    // Use GET
				    method: 'post',asynchronous:true,
				    //postBody: 'thisvar=true&thatvar=Howdy&theothervar=2112',
				    //postBody: params, 
				    postBody: query, 
				    onCreate: function(t) {
				    	$(content_divId).innerHTML = '<div style="padding:0; text-align:center !important; width:460px; height:369px;background:url(images/loadingAnimation.gif) center center no-repeat;"></div>';
				    	//$(content_divId).innerHTML = '<div style="padding:0 0 0 8px; text-align:center !important; width:222px; height:174px;background:url(images/loadingAnimation.gif) center center no-repeat;"></div>';
				    	
				    },
				     // Handle successful response
				    onSuccess: function(t) {
				    	//alert("contentId:"+content_divId);
				    	var resp = t.responseText; 
				    	//alert(resp);
				    	Element.update(content_divId,resp);
				    	
				    },
				   
				     // On Complete acts as a postfunction
				    onComplete: function(t) {
				    	
				    	
				    }
				}
		
		new Ajax.Request(url_base, opt);
		//new Ajax.Request('page_test.jsp', opt);
		
		this.setCurrentPage('pager',this.currentPage);
		//alert("currentpage before pageMod:"+this.currentPage);
		
		//if you want the last page number as the first page number in the next page, use the commented line below
		//var pageMod = (this.currentPage) % (this.pagesToDisplay);
		
		
		// this showPageNav is only called outside for Virgin as for each page the numbers are repainted
		//For other projects , where the page numbers are painted after say 10 pages , follow the convention below
		//where showPageNav is again called only at the 11th page
		//Also , in Virgin , no ptrevset or nextset
		//Original code with everything is in Nextel Mexico
		this.showPageNav('pager', 'pageNavPosition',0,this.setbuttonclicked); 
		
		/*if (this.setbuttonclicked == 'prevset' || this.setbuttonclicked == 'nextset') {
			
			//alert('in prevset for showRecords');
			this.showPageNav('pager', 'pageNavPosition',1,this.setbuttonclicked); 
			
			//pass 0 so that the ajax call is not made , else will fall into infinite loop
			this.showPage(this.currentPage,0);
		
			
		}*/
		
		
		/*if (this.setbuttonclicked == '') {
			alert('enetered in pageMod');
			var pageMod = 1;
			if (this.buttonclicked == 'next')
				pageMod = (this.currentPage) % (this.pagesToDisplay+1);
			if (this.buttonclicked == 'prev')
				pageMod = (this.currentPage) % (this.pagesToDisplay);
				
			//Redraw the pagination bar with the new numbers
			if (pageMod == 0) {
				//this.showPageNav('pager', 'pageNavPosition'); 
				//alert('Entered in pageMod = 0');
				this.showPageNav('pager', 'pageNavPosition',1,this.buttonclicked); 
				
				//pass 0 so that the ajax call is not made , else will fall into infinite loop
				this.showPage(this.currentPage,0);
			}
		}*/
		
		//Reinitialize the variables
		this.setbuttonclicked = '';
		this.buttonclicked = '';
    }
    
    this.setCurrentPage = function(pagerName,pageNumber) {
    	pagerName.currentPage = pageNumber;
    }
    
    //Controlling the current page number clicked display 
    //not required in Virgin , so commented above after ajax call
    this.showPage = function(pageNumber,makeajaxcall) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
		
        //var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        //if (oldPageAnchor)
        	//oldPageAnchor.className = 'pg-normal';
        
        this.currentPage = pageNumber;
        //var newPageAnchor = document.getElementById('pg'+this.currentPage);
        //if (newPageAnchor)
        	//newPageAnchor.className = 'pg-selected';
        
        var from = (pageNumber - 1) * (this.itemsPerPage) + 1;
        var to = from + (this.itemsPerPage) - 1;
        if (makeajaxcall == 1)
        	this.showRecords(from, to,this.contentId);
    }   
    
    this.prev = function() {
    	
    	this.buttonclicked = 'prev';  
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1,1);
        
       
       
    }
    
    this.next = function() {
    	this.buttonclicked = 'next';
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1,1);
        }
    } 
    
    this.nextSet = function(startPage) {
    
    	if (startPage + 1 < this.pages) {
	    	this.setbuttonclicked = 'nextset';
	    	this.currentPage = startPage + 1; //startpage is set to current page
	        //if (this.currentPage < this.pages) {
	        this.showPage(this.currentPage,1);
	       // }
	    }
        else 
        	this.setbuttonclicked = '';
        	
    } 
    
    this.prevSet = function(endPage) {
    	
    	if (endPage > this.pagesToDisplay) {
	    	this.setbuttonclicked = 'prevset';
	    	//if (endPage > this.pagesToDisplay) {
    		this.currentPage = endPage - this.pagesToDisplay; //startpage is set to current page
	        if (this.currentPage < this.pages) {
	            this.showPage(this.currentPage,1);
	        }
		    //}
		}
	    else
	    	this.setbuttonclicked = '';
    }                                                        
    
    this.init = function() {
      
        var records = totalRecords;
       
        this.pages = Math.ceil(records / this.itemsPerPage);
        //alert("pages:"+this.pages);
        this.inited = true;
    }

    this.showPageNav = function(pagerName, positionId,redrawpagenumbers,prevornext) {
    	//alert('in showPageNav');
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
    	var element = document.getElementById(positionId);
    	//var curPage = pagerName.currentPage;
    	
    	
    	//alert("currentPage:"+this.currentPage);
    	//var startPage = parseInt((this.currentPage / this.pagesToDisplay)) * this.pagesToDisplay;
    	
    	var startPage;
    	var endPage;

    	if (redrawpagenumbers == 1) {
    	
    		if (prevornext == 'next') {
    			startPage = this.currentPage;
    			endPage = startPage + this.pagesToDisplay - 1;
    			
    		}
    		
    		if (prevornext == 'prev') {
    			startPage = this.currentPage -  this.pagesToDisplay + 1;
    			endPage = this.currentPage;
    			
    		}
    		
    		if (prevornext == 'prevset'|| prevornext == 'nextset') {
    			startPage = this.currentPage;
    			endPage = startPage + this.pagesToDisplay - 1;
    			if (endPage > this.pages)
    				endPage = this.pages;
    			
    		}
    	
    	}
    	else {
	    	startPage = this.currentPage;
	    	if (startPage == 0)
	    		startPage = 1;
	    	endPage = startPage + this.pagesToDisplay - 1;
	    }
    	//alert("startPage:"+startPage);
    	//alert("endPage:"+endPage);
    	if (endPage > this.pages)
    		endPage = this.pages;
    	
    	//Draw the innerhtml
    	//var pagerHtml = '<span onclick="'+pagerName+'.prevSet('+startPage+');" class="pg-normal"> PrevSet;</span>';    
    	//pagerHtml += '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> | ';
        //for (var page = 1; page <= this.pages; page++) 
        
        //for (var page = startPage; page <= endPage; page++) 
           // pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ',1);">' + page + '</span> | ';
        //pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>'; 
        
        //pagerHtml += '<span onclick="'+pagerName+'.nextSet('+endPage+');" class="pg-normal"> NextSet;</span>';  
        
        var pagerHtml = "";
        var upperHTML = "";//to display on the upperside as well
        var start_index = ((this.currentPage -1) * this.itemsPerPage) + 1;
        var end_index = start_index + this.itemsPerPage - 1;;
        if (end_index >= this.totalRecords)
        	end_index = this.totalRecords;
        
        //Html for pagination in Virgin	
        //<div class="c7_pagination"> 
			//21-40 of 53 games 
			//<span class="c7_pagination_link">&lt; <a href="#">Previous&nbsp;</a></span>
			//<span class="c7_pagination_sep"> | </span> 
			//<span class="c7_pagination_link"> <a href="#">&nbsp;Next</a> &gt;</span>
		//</div>
 
   		//pagerHtml = '1-9 of 13 games <span class="c7_pagination_link"> ';
   		var itemType = "Games";
   		if ((this.pageURL.indexOf("itemtype%3Dapps")) != -1 || (this.pageURL.indexOf("itemtype=apps") != -1))
   			itemType = "Apps & Info";
   		if ((this.pageURL.indexOf("itemtype%3Dgames")) != -1 || (this.pageURL.indexOf("itemtype=games") != -1))
   			itemType = "Games";
   		if ((this.pageURL.indexOf("itemtype%3Dwp")) != -1 || (this.pageURL.indexOf("itemtype=wp") != -1))
   			itemType = "Graphics";
   		if ((this.pageURL.indexOf("itemtype%3Drt")) != -1 || (this.pageURL.indexOf("itemtype=rt") != -1))
   			itemType = "Ringtones";
   		
   		//For Global Search
   		if (this.pageURL.indexOf("globalsearch.do") != -1) {
   			/*if ((this.pageURL.indexOf("found_item_type=games") != -1))
   				itemType = "Games";
   			if ((this.pageURL.indexOf("found_item_type=apps") != -1))
   				itemType = "Apps & Info";
   			if ((this.pageURL.indexOf("found_item_type=wp") != -1))
   				itemType = "Graphics";
   			if ((this.pageURL.indexOf("found_item_type=rt") != -1))
   				itemType = "Ringtones";*/
   				
   			itemType = "";
   			
   		}
   		
   		pagerHtml = '<div class="pagination"> ';
   		//pagerHtml = '';
   		if (this.currentPage > 1)
   			pagerHtml += '<a id="pg_prev" href="javascript:void 0" onclick="'+pagerName+'.prev();">&lt;Previous</a> &nbsp;';
   		pagerHtml += '<span>'+start_index+'-'+ end_index + ' of '+this.totalRecords+' &nbsp;'+itemType+' </span> ';
   		//pagerHtml += '<span onclick="'+pagerName+'.nextSet('+endPage+');"> NextSet;</span>'; 
   		
   		if (end_index < this.totalRecords) {
   			//if (this.currentPage > 1)
   				//pagerHtml += '<span class="c7_pagination_sep"> | </span> ';
    		pagerHtml += '<span><a id="pg_next" href="javascript:void 0" onclick="'+pagerName+'.next();">Next &gt;</a> </span>';
    	}
    	pagerHtml += '</div>';
       
		//upperHTML = '<strong>'+start_index+'-'+ end_index + '</strong> of <span class="copyRed">'+this.totalRecords+'&nbsp;'+itemType+'</span>'; 
        
        //alert(pagerHtml);
        Element.update(element,pagerHtml);
        if ($('upperNavindex'))
        	Element.update('upperNavindex',pagerHtml);
    }
}

