/*---------------------------------------------------  
 *   PagerControl for AJAX
 *  (c) 2005 Jigar Desai <desaijm@hotmail.com>
 *  Date Change: 12/31/2005
 *  requires: prototype.js http://prototype.conio.net/
 *--------------------------------------------------*/

function PagerControl(pageSize,step){
    this.PageSize = pageSize;
    this.Step = step;
    this.PageCount = 0;
    this.CurrentPage = 1;
    this.HeaderTemplate = "<td align='right'><b>";
    this.ItemTemplate = "<a href=\"javascript:{#reloadfunction}({#page},'{#vars}')\" >{#title}</a> &nbsp; ";
    this.SelectedItemTemplate = "{#title} &nbsp; ";
    this.FooterTemplate = "</b></td>";
    this.HostControlIDTop = null;
    this.HostControlIDBottom = null;
    this.TotalItems = 0;
    this.Vars = "";
}

PagerControl.prototype.RenderPager = function(reloadFunction){
    var result = "";
    
    result += "<table width='100%'><tr><td>Showing Results " + (parseInt(this.CurrentPage-1) * parseInt(this.PageSize) + 1);
    if((parseInt(this.CurrentPage) * parseInt(this.PageSize)) < this.TotalItems){
	    result += " to " + (parseInt(this.CurrentPage) * parseInt(this.PageSize));
	  }
	  else{
	    result += " to " + parseInt(this.TotalItems);
	  }
    result += " of " + parseInt(this.TotalItems) + "</td>";
    
    if(parseInt(this.PageCount) > 1){
        result += this.HeaderTemplate;
				
        var startPoint = Math.floor((parseInt(this.CurrentPage)/parseInt(this.Step))) * parseInt(this.Step);
		
        if((parseInt(this.CurrentPage) % parseInt(this.Step)) == 0) {
            startPoint -= parseInt(this.Step);
        }
        
        var midStep = Math.ceil(parseInt(this.Step) / 2);

        if(parseInt(this.CurrentPage) > parseInt(midStep)){
        		if((parseInt(this.CurrentPage) + (parseInt(this.Step) - parseInt(midStep))) <= parseInt(this.PageCount)){
		        		startPoint = parseInt(this.CurrentPage) - midStep;
		        }
		        else{
		        	  startPoint = parseInt(this.PageCount) - parseInt(this.Step);
		        }
        }
       
        if(parseInt(this.CurrentPage) > parseInt(midStep)){
		        result += this.ItemTemplate.replace("{#page}",1).replace("{#vars}",this.Vars).replace("{#title}","<<").replace("{#reloadfunction}",reloadFunction);        		
        }
        else if(parseInt(this.PageCount) > parseInt(this.Step)){
            result += this.SelectedItemTemplate.replace("{#title}","<<");
        }        	
        if(parseInt(this.CurrentPage) > 1){
            result += this.ItemTemplate.replace("{#page}",parseInt(this.CurrentPage)-1).replace("{#vars}",this.Vars).replace("{#title}","Prev").replace("{#reloadfunction}",reloadFunction);
        }
        else{
            result += this.SelectedItemTemplate.replace("{#title}","Prev");
        }        	
        
        for(var i=parseInt(startPoint)+1;i<=parseInt(this.PageCount) && i<=(parseInt(startPoint) + parseInt(this.Step));i++){
            if(i != parseInt(this.CurrentPage)){
                result += this.ItemTemplate.replace("{#page}",i).replace("{#vars}",this.Vars).replace("{#title}",i).replace("{#reloadfunction}",reloadFunction);
            }else{
                result += this.SelectedItemTemplate.replace("{#title}","<span class='selectedItem'>"+i+"</span>");
            }
        }

        if(parseInt(this.CurrentPage) < parseInt(this.PageCount)){
            result += this.ItemTemplate.replace("{#page}",parseInt(this.CurrentPage)+1).replace("{#vars}",this.Vars).replace("{#title}","Next").replace("{#reloadfunction}",reloadFunction);
        }
        else{
            result += this.SelectedItemTemplate.replace("{#title}","Next");
        }        	
        if(parseInt(this.CurrentPage) <= (parseInt(this.PageCount) - parseInt(midStep))){
            result += this.ItemTemplate.replace("{#page}",parseInt(this.PageCount)).replace("{#vars}",this.Vars).replace("{#title}",">>").replace("{#reloadfunction}",reloadFunction);
        }
        else if(parseInt(this.PageCount) > parseInt(this.Step)){
            result += this.SelectedItemTemplate.replace("{#title}",">>");
        }        	
        result += this.FooterTemplate;
    }
    
    result += "</tr></table>"
    
		if(this.HostControlIDTop != null){
	    $(this.HostControlIDTop).innerHTML = result;
	  }
		if(this.HostControlIDBottom != null){
	    $(this.HostControlIDBottom).innerHTML = result;
	  }
}

