/**
 * Pager Class
 * @author Siolk
 * @date 2011.08.24
 * */
function Pager() {
	 this.Class = "Pager";
	 this.currentPage = 1; //選擇的頁碼
	 this.countOfPage = 20;//幾筆一頁(預設 需要與Action|Dao中的設定相同)
	 this.recordCount = 0; //資料總筆數
	 this.totalPage   = 1; //總頁數
	 this.pageItem    = 10;//幾個分頁item不含上一頁、下一頁
	 this.pagerId	  = "#pagenation";	//pager's ID
	 this.totalPage   = 1;				//此次查詢共幾頁
	 this.callBackName = "gotoPage4Json";//Pager點擊頁碼時，呼叫的方法名稱
	 this.init = function(currentPage,recordCount,pagerId,countOfPage,callBackName){
		 if(pagerId){this.pagerId 	  = pagerId;}
		 if(callBackName){this.callBackName = callBackName;}
		 if(!isNaN(currentPage)){this.currentPage = parseInt(currentPage);}
		 if(!isNaN(recordCount)){this.recordCount = parseInt(recordCount);}
		 if(!isNaN(countOfPage)){this.countOfPage = parseInt(countOfPage);}
		 if(countOfPage!=0) {
			 this.totalPage = parseInt(this.recordCount/this.countOfPage) + (this.recordCount%this.countOfPage != 0 ? 1 : 0);
		 }
	 };
	 this.reset = function(currentPage,recordCount){
		 if(!isNaN(currentPage)){this.currentPage = parseInt(currentPage);}
		 if(!isNaN(recordCount)){this.recordCount = parseInt(recordCount);}
		 this.totalPage = parseInt(this.recordCount/this.countOfPage) + (this.recordCount%this.countOfPage != 0 ? 1 : 0);
	 };
	 this.scrollTo = function(selector){
		 //TODO
	 };
	 this.toString = function(){
		var begin   = this.currentPage; 
		var end     = begin + (this.pageItem - 1);
		var count   = this.pageItem;
		var html    = "";
		var preHtml = "";
		if(this.totalPage > 1){
			if(this.recordCount<=0) {
				html = "<font color='black'>&nbsp;上一頁</font><a class='activeSlide' href='javascript:" + this.callBackName + "(1)'>1</a><font color='black'>&nbsp;上一頁</font>";
				$(this.pagerId).html(html);
				return html;
			}
			
			for(var i = begin; i <= end; i++){
				if(i <= this.totalPage){
					if(i == this.currentPage){
						html += "<a class='activeSlide' href='javascript:" + this.callBackName + "("+ i +")'>" + i + "</a>";
					}else{
						html += "<a href='javascript:" + this.callBackName + "("+ i +")' >" + i + "</a>";
					}
					count--;
				}
			}
			
			for(var i = count;i >= 1;i--){
				--begin;
				if(begin < 1)break;
				
				preHtml = "<a href='javascript:" + this.callBackName + "("+ (begin) +")'>" + (begin) + "</a>" + preHtml;
			}	
			html = preHtml + html;
			if(this.currentPage - 1 > 0){
				html = "<a href='javascript:" + this.callBackName + "("+ (this.currentPage -1) +")'>&nbsp;上一頁</a>" + html;
			}else{
				html = "<font color='black'>&nbsp;上一頁</font>" + html;
			}
			
			if(this.currentPage + 1 > this.totalPage){
				html += "<font color='black'>&nbsp;下一頁</font>";
			}else{
				html += "<a href='javascript:" + this.callBackName + "("+ (this.currentPage + 1) +")'>&nbsp;下一頁</a>";
			}
			$(this.pagerId).html(html);
		}
		
		 return html;
	 };
 };
