//*-------------------*//  
//*  PagerControl	*//
//*-------------------*//

function PagerControl(pageSize, step)
{
    this.PageSize = pageSize;
    this.Step = step;
    this.PagesCount = 0;
    this.CurrentPage = 1;
    this.HeaderTemplate = "<div style=\"display: inline; float: left; margin-right: 10px;\">Select a page:</div><div class=\"list\" style=\"display: inline; width: 35px;\"><a href=\"javascript:{#pagerFunction}(1)\">First</a></div>";
    this.PrevTemplate = "<div class=\"list\" style=\"display: inline; width: 35px;\"><a href=\"javascript:{#pagerFunction}({#prevpage})\">Prev</a></div>";
    this.NextTemplate = "<div class=\"list\" style=\"display: inline; width: 35px;\"><a href=\"javascript:{#pagerFunction}({#nextpage})\">Next</a></div>";
    this.ItemTemplate = "<div class=\"list\" style=\"display: inline; \"><a href=\"javascript:{#pagerFunction}({#page})\">{#title}</a></div>";
    this.SelectedItemTemplate = "<div class=\"list\" id=\"current\" style=\"display: inline;\">{#title}</div>";
    this.FooterTemplate = "<div class=\"list\" style=\"display: inline; width: 35px;\"><a href=\"javascript:{#pagerFunction}({#lastpage})\">Last</a></div><div style=\"display: inline;\">Total pages: ";
    this.TopHostControlID = null;
	this.BottomHostControlID = null;
	this.ShowHeader = true;
	this.ShowFooter = true;
	this.ShowItem = true;

	PagerControl.prototype.RenderPager = function(pagerFunction)
	{
		var result = "";

		if(this.PagesCount > 1)
		{
			if (this.ShowHeader) result += this.HeaderTemplate.replace("{#pagerFunction}", pagerFunction);
			
			if (this.CurrentPage > 1)
				result += this.PrevTemplate.replace("{#pagerFunction}", pagerFunction).replace("{#prevpage}", this.CurrentPage - 1);
				
			var startPoint = Math.floor((this.CurrentPage/this.Step)) * this.Step;
			if((this.CurrentPage % this.Step) == 0) 
			{
				startPoint -= this.Step;
			}
	        
	        if (this.ShowItem)
	        {
				if(startPoint >= this.Step)
				{
					result += this.ItemTemplate.replace("{#page}",startPoint).replace("{#title}","...").replace("{#pagerFunction}",pagerFunction);
				}
		        
				for(var i=startPoint +1;i<=this.PagesCount && i<=(startPoint + this.Step);i++)
				{			
					if(i != this.CurrentPage)
					{
						result += this.ItemTemplate.replace("{#page}",i).replace("{#title}",i).replace("{#pagerFunction}",pagerFunction);
					}
					else
					{
						result += this.SelectedItemTemplate.replace("{#title}",i);
					}
				}
		        
				if(startPoint < (this.PagesCount - this.Step))
				{
					result += this.ItemTemplate.replace("{#page}",(startPoint + this.Step +1)).replace("{#title}","...").replace("{#pagerFunction}",pagerFunction);
				}
			}
			
			if (this.CurrentPage < this.PagesCount)
				result += this.NextTemplate.replace("{#pagerFunction}", pagerFunction).replace("{#nextpage}", this.CurrentPage + 1);
	        
			if (this.ShowFooter) result += this.FooterTemplate.replace("{#pagerFunction}",pagerFunction).replace("{#lastpage}", this.PagesCount) + "<b>" + this.PagesCount + "</b></div>" ;
		}
	    
		$get(this.TopHostControlID).innerHTML = result;
		
		if (this.BottomHostControlID != null) $get(this.BottomHostControlID).innerHTML = result;
	}
}
