/*
 *  PagingControl
 *  A class to create a control to handle data paging
 *  See documentation for usager and properties
 *
 *  The classes rely on the prototype.js ( http://prototype.conio.net )
 *  and is released under the same terms as the prototype library.
 *
 *  @version 0.1
 *  @license GPL
 *
 *	TODO:
 *      - convert form creation from html string to object creation+DOM insertion
 * 		- massive english spellchecking ;)
*/

var PagingControl=Class.create();

PagingControl.prototype=
{
	initialize: function(container,sourceForm,options)
	{
	    this.loading=false;
		this.container=$(container);

		options=options || {};

		if (options.maxHeight===undefined)
		{
		    var heightStyle="";
		}
		else
		{
			this.maxHeight=options.maxHeight;
			
			// IE/FF hack for control height
			var heightStyle=(Global.EXPLORER ? "height:"+this.maxHeight : "max-height:"+this.maxHeight)+"px;";
		}
		
		this.onquery=options.onquery || function(){};
        this.sourceForm=sourceForm;
        this.formName=options.formName || (container+"_form");
        
		var html=
		"<form id=\""+this.formName+"\" name=\""+this.formName+"\" action=\"\" method=\"post\">"+
			"<center>"+
			"<table width=\"98%\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"+
			    "<tr>"+
				    "<td align=\"left\" width=\"10%\"><a id=\""+this.formName+"_firstpage"+"\" href=\"#\" title=\"vai alla prima pagina\">&lt;&lt;</a></td>"+
				    "<td align=\"left\" width=\"10%\"><a id=\""+this.formName+"_prevpage"+"\" href=\"#\" title=\"vai alla pagina precedente\">&lt;</a></td>"+
				    "<td align=\"center\" width=\"60%\"><div id=\""+this.formName+"_thispage"+"\"> pagina <input id=\""+this.formName+"_requestedPage"+"\" name=\"requestedPage\" type=\"text\" size=\"5\" style=\"text-align:right\"> di <span id=\""+this.formName+"_totalPages"+"\" style=\"font-weight: bold;\"></span></div></td>"+
				    "<td align=\"right\" width=\"10%\"><a id=\""+this.formName+"_nextpage"+"\" href=\"#\" title=\"vai alla pagina successiva\">&gt;</a></td>"+
				    "<td align=\"right\" width=\"10%\"><a id=\""+this.formName+"_lastpage"+"\" href=\"#\" title=\"vai all'ultima pagina\">&gt;&gt;</a></td>"+
				"</tr>"+
				"<tr>"+
				    "<td colspan=\"5\">"+
					    "<div id=\""+this.formName+"_searchresults"+"\" style=\"width:100%; "+heightStyle+" border: 0px; margin: 0px; padding:0px; overflow-y: visible; overflow-y:auto;\">"+
		                "</div>"+
	                "</td>"+
				"</tr>"+
	            "<tr>"+
				    "<td colspan=\"5\" align=\"center\"><div id=\""+this.formName+"_footerMessage"+"\">"+
						"<b>Numero di elementi trovati: <span id=\""+this.formName+"_rowCount"+"\"></span></b>"+
					"</div></td>"+
				"</tr>"+
	        "</table>"+
			"</center>"+
			"<input type=\"hidden\" id=\""+this.formName+"_currentPage"+"\" name=\"currentPage\" value=\"\">"+
			"<input type=\"hidden\" id=\""+this.formName+"_rowPerPage"+"\" name=\"rowPerPage\" value=\"\">";

		// insert hidden tag in this form that are copies of any input tags of original form
		this.inputs=new Object();
		var inputs=Form.getElements(sourceForm);

		for (var i=0; i<inputs.length; i++)
		{
		    html+="<input type=\"hidden\" id=\""+this.formName+"_"+inputs[i].name+"\" name=\""+inputs[i].name+"\" value=\""+inputs[i].value+"\">";
            this.inputs[inputs[i].name]=this.formName+"_"+inputs[i].name;
		}
        
		html+="</form>";

		// insert the conrol inside the container
		//this.container.appendChild(document.createTextNode(html));
		this.container.innerHTML=html;

		this.form=$(this.formName);

		this.dataContainer=$(this.formName+"_searchresults");
		this.currentPageInput=$(this.formName+"_currentPage");
		this.requestedPageInput=$(this.formName+"_requestedPage");
		this.rowCountInput=$(this.formName+"_rowCount");
		this.rowPerPageInput=$(this.formName+"_rowPerPage");
		this.totalPagesInput=$(this.formName+"_totalPages");
		
		// attach this object to controls
		this.form.pagingControl=this;
		$(this.formName+"_firstpage").pagingControl=this;
		$(this.formName+"_prevpage").pagingControl=this;
		$(this.formName+"_nextpage").pagingControl=this;
		$(this.formName+"_lastpage").pagingControl=this;

		// attach avents to controls
		this.form.onsubmit=function()
		{
		    this.pagingControl.onquery(this.pagingControl.formName,"");
		    return false;
		};

		$(this.formName+"_firstpage").onclick=function()
		{
		    if (!this.loading) this.pagingControl.onquery(this.pagingControl.formName,"first");
		    
		    return false;
		};

		$(this.formName+"_prevpage").onclick=function()
		{
		    if (!this.loading) this.pagingControl.onquery(this.pagingControl.formName,"prev");
		    
		    return false;
		};

		$(this.formName+"_nextpage").onclick=function()
		{
		    if (!this.loading) this.pagingControl.onquery(this.pagingControl.formName,"next");
		    
		    return false;
		};

		$(this.formName+"_lastpage").onclick=function()
		{
		    if (!this.loading) this.pagingControl.onquery(this.pagingControl.formName,"last");
		    
		    return false;
		};
	},
	
	updateControl:function (newValues)
	{
	    // update controls
	    this.currentPageInput.value=newValues.currentPage;
		this.requestedPageInput.value=newValues.currentPage;
		this.rowPerPageInput.value=newValues.rowPerPage;
		this.rowCountInput.innerHTML=newValues.rowCount;
        this.totalPagesInput.innerHTML=newValues.pageCount;
        
		if (newValues.pageCount == 1)
		{
			Element.hide(this.formName+"_firstpage");
			Element.hide(this.formName+"_prevpage");
			Element.hide(this.formName+"_thispage");
			Element.hide(this.formName+"_nextpage");
			Element.hide(this.formName+"_lastpage");
		}
		else
		{
			Element.show(this.formName+"_thispage");

			if(newValues.currentPage == 1)
			{
				Element.hide(this.formName+"_firstpage");
				Element.hide(this.formName+"_prevpage");
				Element.show(this.formName+"_nextpage");
				Element.show(this.formName+"_lastpage");
			}
			else if( newValues.currentPage == newValues.pageCount)
			{
				Element.show(this.formName+"_firstpage");
				Element.show(this.formName+"_prevpage");
				Element.hide(this.formName+"_nextpage");
				Element.hide(this.formName+"_lastpage");
			}
			else
			{
				Element.show(this.formName+"_firstpage");
				Element.show(this.formName+"_prevpage");
				Element.show(this.formName+"_nextpage");
				Element.show(this.formName+"_lastpage");
			}
		}

		// update query parameters
		if (newValues.parameters)
		{
			for (var i in this.inputs)
			{
			    if (newValues.parameters[i]!=undefined) $(this.inputs[i]).value=newValues.parameters[i];
			}
		}
	},
	
	setFooterString: function(messageString)
	{
		$(this.formName+"_footerMessage").innerHTML = messageString;
	},

	showLoading: function(show)
	{
	    this.loading=show;
	    if (show)
	    {
	        this.savedCursorStyle=this.container.style.cursor;
	    	this.container.style.cursor="wait";
	    	Form.disable(this.formName);
		}
		else
		{
		    Form.enable(this.formName);
		    this.container.style.cursor=this.savedCursorStyle;
		}
	}
} // PagingControl
