//////////////////////////////////////////////
// Italcom Spa
// Browser: IE 6.0, Mozilla 1.7
// Library: CPager
// Version 1.0
// Require:
//
//
// Objects:
//////////////////////////////////////////////


// Costructor
function CPager( id, maxPagesLink, pageNumberBody , leftButton, rightButton )
{
	this.disabled = false;
	this.Table = null;
	this.id = id;
	this.maxPagesLink = maxPagesLink;
	this.Initialized = false;
	this.CurrentPage = 1;
	this.TotalPageCount = 0;
	this.cellArray = new Array(maxPagesLink);
	this.tableCells = [];
	
	
	this.pageNumberBody = pageNumberBody == null ? "$PAGE" : pageNumberBody;
	this.leftButtons = leftButton == null ? [{HTML:"|&lt;", Type:"F"}, {HTML:"&lt;", Type:"P"}] : leftButton;
	this.rightButtons = rightButton == null ? [{HTML:"&gt;", Type:"N"}, {HTML:"&gt;|", Type:"L"}] : rightButton;
	
}

CPager.prototype.ObjectKey = null;
CPager.prototype.ButtonOverItemStyle = 'CPager-clsTD-Over';
CPager.prototype.ButtonItemStyle = 'CPager-clsTD-Normal';


CPager.prototype.Disable = 
// =================================
function()
{
	this.disabled = true;
}

CPager.prototype.Enable = 
// =================================
function()
{
	this.disabled = false;
}


CPager.prototype.getItemStyle 
= // =================================
function (item, over)
{
    if (over) 
	return this.ButtonOverItemStyle;
    else
	return this.ButtonItemStyle;
	
}



CPager.prototype.createTable 
= // ================================
function ()
{
    this.Table = document.createElement('TABLE');
    this.Table.id = "Table_" + this.id;
}

CPager.prototype.Render
= // ================================
function(objectParent)
{



	// Se non e' ancora stato definito il layout della tabella , invochiamo il metodo per definire il layout
	if (this.Initialized == false)
	{
		
		if ( this.Table != null )
		{
	    		this.Table.parentNode.removeChild(this.Table);	
		}	
		
		//
		// creiamo la tabella.
		//
		
		this.createTable();
		
		// creiamo l 'unica riga TR

		var tableRow = this.Table.insertRow(-1);
		var cell;
		var nCellIndex = 0;
		
		// bottoni sulla sinistra.
		
		for (var col=0;col<this.leftButtons.length;col++)
		{
		    	cell = tableRow.insertCell(nCellIndex++);
		    	cell.onclick = this.GoToPage;
		    	cell.onmouseover = this.mouseEnter;
		    	cell.onmouseout = this.mouseExit;
		    	cell.objParent = this;
		    	cell.className = this.getItemStyle(cell, false);
				cell.pageNo = null;
	        	cell.cmdType = this.leftButtons[col].Type;
	        	cell.innerHTML = this.leftButtons[col].HTML;
		}
		
		
		
		for (var col=0;col<this.maxPagesLink;col++)
		{
		    cell = tableRow.insertCell(nCellIndex++);
		    this.tableCells[col] = cell;


		    if (this.CurrentPage + col <= this.TotalPageCount)
		    {
			cell.innerHTML = "" + this.pageNumberBody.replace(/\$PAGE/gi, "" + (this.CurrentPage + col));
			cell.pageNo = this.CurrentPage + col;
			cell.cmdType = "G";
		    }
		    else
		    {
			cell.innerHTML = "&nbsp;";
			cell.pageNo = null;
			cell.cmdType = "G";
		    }
			    	
		    cell.onclick = this.GoToPage;
		    cell.onmouseover = this.mouseEnter;
		    cell.onmouseout = this.mouseExit;
		    cell.objParent = this;
		    cell.className = this.getItemStyle(cell, false);
		    
		}
		
		for (var col=0;col<this.rightButtons.length;col++)
		{
		    	cell = tableRow.insertCell(nCellIndex++);
		    	cell.onclick = this.GoToPage;
		    	cell.onmouseover = this.mouseEnter;
		    	cell.onmouseout = this.mouseExit;
		    	cell.objParent = this;
		    	cell.className = this.getItemStyle(cell, false);
				cell.pageNo = null;
	        	cell.cmdType = this.rightButtons[col].Type;
	        	cell.innerHTML = this.rightButtons[col].HTML;
		}
		
		
		objectParent.appendChild(this.Table);
		this.Initialized = true;
	}


}

CPager.prototype.mouseEnter 
= // =====================================
function ()
{
    if ( this.objParent.disabled ) return;
    this.className = this.objParent.getItemStyle(this, true);
}


CPager.prototype.mouseExit
= // =====================================
function ()
{
	if ( this.objParent.disabled ) return;
    this.className = this.objParent.getItemStyle(this, false);
}


CPager.prototype.updatePager
= // ======================================
function ()
{
    
    
    if (this.Table == null) return;
    
    for (var i = 0; i< this.maxPagesLink; i++)
    {
		if ((i + this.CurrentPage ) <= this.TotalPageCount)
		{
			this.tableCells[i].innerHTML =  this.pageNumberBody.replace(/\$PAGE/gi, "" + (i + this.CurrentPage));
			this.tableCells[i].pageNo =  i + this.CurrentPage;
		}
		else
		{
			this.tableCells[i].innerHTML =  "&nbsp;"
			this.tableCells[i].pageNo = null;
		}
    }


}

CPager.prototype.Refresh
= // =======================================
function ()
{
    this.updatePager();	
}


CPager.prototype.SetCurrentPage
= // =======================================
function (page)
{
    if (page > 0 && page <= this.TotalPageCount)
    {
    	this.CurrentPage  = page;
    	this.updatePager();
    }
}


CPager.prototype.SetTotalPageCount
= // =======================================
function (pageCount)
{
	this.TotalPageCount = pageCount;
	this.CurrentPage = 1;
	this.updatePager();
}

CPager.prototype.GoToPage
= // =====================================
function ()
{

	if ( this.objParent.disabled ) return;
	var newPage = 0;
	var obj = this.objParent;

	switch(this.cmdType)
	{
	case "F":
		newPage = 1;
		break;
	case "N":
		newPage = obj.CurrentPage + 1;
		if ( newPage > obj.TotalPageCount ) return;
		break;
	case "P":
		newPage = obj.CurrentPage - 1;
		if ( newPage < 1 ) return;
		break;
	case "L":
		newPage = obj.TotalPageCount;
		if (newPage < 1) newPage = 1;
		break;
	case "G":
		newPage = this.pageNo;
		if (newPage == null) return;
		break;
	}

	var result = false;
	

	if (obj.OnGotoPage != null)  result = obj.OnGotoPage(this, newPage);
	if ( result )
	{
	     obj.CurrentPage = newPage;
	     obj.updatePager();
	}
}



