



/**
 * Paginations associated array [divId,pagination class]
 */
paginationsArray = new Array();
/**
 * Pagination class, encapsulates the pagination details
 */
function Pagination(divId,methodName,ids){
	// Div Id
	this.divId=divId;
	// the pagation div is divId+"Pagination"
	this.paginationDivId=divId+"Pagination";
	// Method name (the methodo to invoke)
	this.methodName=methodName;
	// the ids of the pagination
	this.ids=ids;
	// Cache this pagination class associated with the divId
	paginationsArray[divId]=this;
	// Pagination page size
	this.pageSize=10;
	this.id=0;
	// After loading function (to execute after loading script)
	this.afterLoading=function(){}
	
	//Get paginated content of url and set it in the divId provided
	this.paginate = function (id,url){
		this.id=id;
		
		if(url.indexOf("?")==-1){
			url+="?r="+Math.random();
		}else{
			url+="&r="+Math.random();
		}
		
		xmlhttp.open("GET",url ,true);
		xmlhttp.onreadystatechange=this.renderServerSideResponse;
		xmlhttp.send(null);
	}

	this.renderServerSideResponse=function(){
 		if (xmlhttp.readyState==4) {
 	 		// Get the pagination from the associative array
 	 		pagination=paginationsArray[divId];
 	 		// Draw the content
 	 		document.getElementById(pagination.divId).innerHTML=xmlhttp.responseText;
 	 		// Draw the pagination
 	 		pagination.drawPagination(pagination.divId,pagination.id);
 	 		// Execute
 	 		pagination.afterLoading();
 		}
	}

	/**
	 * Draw the Pagination in the provided divId, with the currentId
	 */
	this.drawPagination=function(divId,currentId){
		html="<table class='ajax-pagination'><tr>";
		startIndex=this.getCurrentIndex(currentId)-this.pageSize/2;
		//startIndex=Math.max(startIndex,0);
		startIndex=0;
		//endIndex=Math.min(startIndex+this.pageSize,this.ids.length);
		endIndex=this.ids.length;
		startIndex=Math.max(startIndex,endIndex-this.pageSize);
		if(this.getPreviousId(currentId)!=null){
			html+="<td><a href=\"javascript:"+methodName+"("+this.getPreviousId(currentId)+")\"><img src='../icons/previous_yellow.gif' border='0'/></a></td>";
		}
		
		for(i=startIndex;i<endIndex;i++){
			if(currentId==this.ids[i]){
				html+="<td class='selected'>"+(i+1)+"</td>";
			}else{
				html+="<td><a href=\"javascript:"+methodName+"("+this.ids[i]+")\">"+(i+1)+"</a></td>";
			}
		}
		if(this.getNextId(currentId)!=null){
			html+="<td><a href=\"javascript:"+methodName+"("+this.getNextId(currentId)+")\"><img src='../icons/next_yellow.gif' border='0'/></a></td>";
		}
		
		
		html+="</tr></table>";
		document.getElementById(this.paginationDivId).innerHTML="<div id='"+divId+"'>"+html+"</div>";
	}


	this.getCurrentIndex=function (currentId){
		for(i=0;i<this.ids.length;i++){
			if(this.ids[i]==currentId){
				return i;
			}
		}
		return 0;
	}


	this.getPreviousId=function (currentId){
		if(this.ids==null){
			return null;
		}
		if(this.getCurrentIndex(currentId)>0){
			return ids[this.getCurrentIndex(currentId)-1];
		}
		return null;
	}

	this.getNextId=function (currentId){
		if(this.ids==null){
			return null;
		}
		return this.ids[this.getCurrentIndex(currentId)+1];
	}
	
			
	if(ids!==null && ids.length>0){
		this.drawPagination(this.divId,ids[0]);
	}
	
}


