var ProtoGrid=Class.create ({
	initialize:function(elem,opt) {
		opt = opt || {};
		this.options = {
			width: 500,
			height: 300,
			rows: 10,
			rowsSelectorOpt: [10,15,20,25,40],  
			titleBar: true,
			title: "ProtoGrid",
			buttonBar1: true,
			buttonBar2: true,
			controlBar: true,
			dataUrl: "",
			parameters: "",
			queryMethod: "get",
			columnsTitles: ["Column1","Column2","Column3"],
			columnsFields: ["col1","col2","col3"],
			columnsVisibility: [true,true,true],
			columnsAlign: ["left","center","right"],
			columnsWidth: [100,100,100],
			columnsDraggables: [true,true,true],
			pageText: "Pagina",
			ofText: "de",
			displayingText: "Mostrando",
			toText: "a",
			itemText: "elementos",
			processingText: "Procesando, por favor espere...",
			searchText: "Buscar en",
			searchButtonText: "Buscar",
			clearButtonText: "Limpiar",
			noItems: "No se han encontrado datos",
			multiSelect: false,
			imageFolder: 'js/protoGrid/images',
			searchButton: true,
			onclick: null
		};
		
		Object.extend(this.options, opt);
		
		this.elem = $(elem);
		if (!this.elem) return;
		
		this.elem.className='protoGrid';
		
		this.searchParams=""; //aquesta variable contendra els paramtres de recerca a l'hora de fer la peticio ajax
		this.searchBarVisible=false;
		
		
		this.arrRows=new Array();
		this.selectedData=new Array();
		this.currentRow=null;
		this.currentColumn=null;
		
		this.elem.setStyle({
			overflow: "hidden",
			width: this.options.width+"px",
			height: this.options.height+"px"
		});
		
		if (this.options.titleBar) {
			this.dTitle=new Element('div', { 'class': 'protoTitle' });
			var tTitle=document.createTextNode(this.options.title);
			this.dTitle.appendChild(tTitle);
			this.elem.appendChild(this.dTitle);
		}
		
		if ((this.options.buttonBar1) || (this.options.buttonBar2)) {
			this.dButtonBarContainer=new Element('div', { 'class': 'contButtonBars' });
			this.elem.appendChild(this.dButtonBarContainer);
		}
		
		if (this.options.buttonBar1) {
			this.dButtonBar1=new Element('div', { 'class': 'buttonBar' });
			this.dButtonBarContainer.appendChild(this.dButtonBar1);
			
			var dBut=new Element('div', { 'class':'pButton', 'style':'cursor:default; width:0px;' });
			this.dButtonBar1.appendChild(dBut);
		}
		
		if (this.options.buttonBar2) {
			this.dButtonBar2=new Element('div', { 'class':'buttonBar' });
			this.dButtonBarContainer.appendChild(this.dButtonBar2);
			var dBut=new Element('div', { 'class':'pButton', 'style':'cursor:default; width:0px;' });
			this.dButtonBar2.appendChild(dBut);
		}
		
		//en aquest punt cream el div contenidor de la taula
		this.dGridCont=new Element('div', { 'class':'contTable' });
		this.dGridCont.style.width=(this.options.width-2)+"px";
		this.dGridCont.style.height=this.options.height+"px";
		this.elem.appendChild(this.dGridCont);
		
		//ara la barra de botons
		if (this.options.controlBar) {
			this.dControlBar=new Element('div', { 'class':'protoTitle' });
			this.dControlBar.style.height="23px";
			this.elem.appendChild(this.dControlBar);
			
			this.dSearch=new Element('div', { 'class':'pButton' });
			this.dSearch.style.width="19px";
			this.dSearch.style.paddingLeft="3px";
			this.dSearch.style.paddingTop="2px";
			this.dSearch.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dSearch.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dSearch.onclick=function() {
				if (this.searchBarVisible==false) {
					this.mostraCerca();
					this.searchBarVisible=true;
				} else {
					this.amagaCerca();
					this.searchBarVisible=false;
				}
				
			}.bind(this);
			
			var imgSearch=new Element('img');
			imgSearch.src=this.options.imageFolder+"/magnifier.png";
			
			this.dSearch.appendChild(imgSearch);		 
			
			//afegim el botó a la barra de botons
			if(this.options.searchButton==true){
				this.dControlBar.appendChild(this.dSearch);
			}
			
			this.addSeparator(this.dControlBar);
			
			this.dRowSelector=new Element('div', { 'class':'rowSelectorCont' });
			this.dControlBar.appendChild(this.dRowSelector);
			this.rowSelector=new Element('select');
			this.dRowSelector.appendChild(this.rowSelector)
			this.loadRowSelectorOptions();
			
			//this.rowSelector.observe('change', this.request.bind(this));
			
			this.rowSelector.onchange=function() {
				this.currentPage=1;
				this.request();
			}.bind(this);
			
			//separador
			this.addSeparator(this.dControlBar);
			
			//botó primera pagina
			this.dFirstPage=new Element('div', { 'class':'pButton' });
			this.dFirstPage.style.width="19px";
			this.dFirstPage.style.paddingLeft="3px";
			this.dFirstPage.style.paddingTop="3px";
			this.dFirstPage.style.marginRight="1px";
			this.dFirstPage.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dFirstPage.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dFirstPage.onclick=function() {
				this.currentPage=1;
				this.request();
			}.bind(this);
			
			var imgFirstPage=new Element('img');
			imgFirstPage.src=this.options.imageFolder+"/first.gif";
			
			this.dFirstPage.appendChild(imgFirstPage);
			this.dControlBar.appendChild(this.dFirstPage);	
			
			//botó pagina anterior
 			this.dPreviousPage=new Element('div', { 'class':'pButton' });
			this.dPreviousPage.style.width="19px";
			this.dPreviousPage.style.paddingLeft="3px";
			this.dPreviousPage.style.paddingTop="3px";
			this.dPreviousPage.style.marginRight="1px";
			this.dPreviousPage.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dPreviousPage.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dPreviousPage.onclick=function() {
				if (this.currentPage>1) {
					this.currentPage=this.currentPage-1;
					this.request();
				}
			}.bind(this);
			
			var imgPreviousPage=new Element('img');
			imgPreviousPage.src=this.options.imageFolder+"/prev.gif";
			
			this.dPreviousPage.appendChild(imgPreviousPage);
			this.dControlBar.appendChild(this.dPreviousPage);
			
			//separador
			this.addSeparator(this.dControlBar);
			
			//el texte Pagina
			var dPage=new Element('div', { 'class':'dPage' }).update(this.options.pageText+" ");
			this.dControlBar.appendChild(dPage);
			
			//el textbox de la pagina
			this.dTextboxPage=new Element('div', { 'class':'dPage' });
			this.iTextboxPage=new Element('input');
			this.iTextboxPage.setAttribute("type","text");
			this.iTextboxPage.style.width="30px";
			this.iTextboxPage.style.fontSize="8pt";
			this.iTextboxPage.style.marginTop="-1px";
			this.dTextboxPage.appendChild(this.iTextboxPage);
			this.dControlBar.appendChild(this.dTextboxPage);
			
			//el texte de
			var dDe=new Element('div', { 'class':'dPage' }).update(this.options.ofText+" ");
			this.dControlBar.appendChild(dDe);
			
			//el texte de total de pagines
			this.dTotPage=new Element('div', { 'class':'dPage' });
			this.dTotPage.style.marginRight="3px";
			this.dControlBar.appendChild(this.dTotPage);
			
			//separador
			this.addSeparator(this.dControlBar);
			
			//botó pagina seguent
			this.dNextPage=new Element('div', { 'class':'pButton' });
			this.dNextPage.style.width="19px";
			this.dNextPage.style.paddingLeft="3px";
			this.dNextPage.style.paddingTop="3px";
			this.dNextPage.style.marginRight="1px";
			this.dNextPage.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dNextPage.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dNextPage.onclick=function() {
				if (this.currentPage<this.totalPagines) {
					this.currentPage++;
					this.request();
				}
			}.bind(this);
			
			var imgNextPage=new Element('img');
			imgNextPage.src=this.options.imageFolder+"/next.gif";
			
			this.dNextPage.appendChild(imgNextPage);
			this.dControlBar.appendChild(this.dNextPage);	
			
			//botó darrera página
 			this.dLastPage=new Element('div', { 'class':'pButton' });
			this.dLastPage.style.width="19px";
			this.dLastPage.style.paddingLeft="3px";
			this.dLastPage.style.paddingTop="3px";
			this.dLastPage.style.marginRight="1px";
			this.dLastPage.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dLastPage.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dLastPage.onclick=function() {
				this.currentPage=this.totalPagines;
				this.request();
			}.bind(this);
			
			var imgLastPage=new Element('img');
			imgLastPage.src=this.options.imageFolder+"/last.gif";
			
			this.dLastPage.appendChild(imgLastPage);
			this.dControlBar.appendChild(this.dLastPage);
			
			//separador
			this.addSeparator(this.dControlBar);
			
			//botó recarrega
 			this.dReloadPage=new Element ('div', {'class':'pButton'});
			this.dReloadPage.style.width="19px";
			this.dReloadPage.style.paddingLeft="3px";
			this.dReloadPage.style.paddingTop="3px";
			this.dReloadPage.style.marginRight="1px";
			this.dReloadPage.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dReloadPage.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dReloadPage.onclick=function() {
				this.request();
			}.bind(this);
			
			this.imgReloadPage=new Element('img');
			this.imgReloadPage.src="images/load.png";
			
			this.dReloadPage.appendChild(this.imgReloadPage);
			this.dControlBar.appendChild(this.dReloadPage);	
			
			//separador
			this.addSeparator(this.dControlBar);
			
			//el texte de estat
			this.dStatus=new Element('div', { 'class':'dPage' });
			this.dControlBar.appendChild(this.dStatus);
			
			
		}
		
		//ara la barra de recerca
		if (this.options.controlBar) {
			this.dSearchBar=new Element("div", { 'class':'protoTitle' });
			this.dSearchBar.style.height="23px";
			this.dSearchBar.style.display="none";
			//this.dSearchBar.style.position="absolute";
			this.elem.appendChild(this.dSearchBar);
			
			var ds=new Element("div", { 'class':'dPage' }).update(this.options.searchText+" ");
			this.dSearchBar.appendChild(ds);
			
			this.dSearchCombo=new Element("div", { 'class':'rowSelectorCont'});
			this.dSearchBar.appendChild(this.dSearchCombo);
			this.searchCombo=new Element('select');
			this.dSearchCombo.appendChild(this.searchCombo);
			this.dSearchBar.appendChild(this.dSearchCombo);
			this.loadSearchCombo(this.dSearchCombo);
			
			var ds2=new Element("div", { 'class':'rowSelectorCont'});
			this.iSearch=new Element("input");
			this.iSearch.style.width="150px";
			ds2.appendChild(this.iSearch);
			this.dSearchBar.appendChild(ds2);
			
			this.addSeparator(this.dSearchBar);
			
			
			//Boton buscar
			this.dSearchButton=new Element('div', { 'class':'pButton' }).update(this.options.searchButtonText);
			this.dSearchButton.style.padding="4px 4px 0px";
			this.dSearchButton.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dSearchButton.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dSearchButton.onclick=function() {
				this.cerca();
			}.bind(this);
			
			this.dSearchBar.appendChild(this.dSearchButton);
			
			//boton limpiar
			this.dCleanButton=new Element('div', {'class':'pButton' }).update(this.options.clearButtonText);
			this.dCleanButton.style.padding="4px 4px 0px";
			this.dCleanButton.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			this.dCleanButton.onmouseout=function() {
				this.className="pButton";
			}
			
			this.dCleanButton.onclick=function() {
				this.limpia();
			}.bind(this);
			
			this.dSearchBar.appendChild(this.dCleanButton);
			
		}
		
		//ara calculam la altura de la taula de dades
		if (this.options.titleBar) {
			var hTitle=this.dTitle.getHeight();
		} else {
			var hTitle=0;
		}
		
		if (this.options.buttonBar1) {
			var hButtonBar1=this.dButtonBar1.getHeight();
		} else {
			var hButtonBar1=0;
		}
		
		if (this.options.buttonBar2) {
			var hButtonBar2=this.dButtonBar2.getHeight();
		} else {
			var hButtonBar2=0;
		}
		
		if (this.options.controlBar) {
			var hControlBar=this.dControlBar.getHeight();
		} else {
			var hControlBar=0;
		}
		
		var tableHeight=this.options.height-hTitle-hButtonBar1-hButtonBar2-hControlBar;
		
		this.dGridCont.style.height=tableHeight+"px";
		
		//això ens permetra saber el numero de pagina en el que estam
		this.currentPage=1;
		this.orderCol=-1;
		this.orderDir="ASC";
		
		//en aquest punt ja podem fer la peticio ajax
		this.request();
		
	},
	
	loadSearchCombo: function() {
		//aquesta funció s'encarrega de carregar el combo de les columnes amb les dades que pertoquin.
		
		for (var k=0;k<this.options.columnsTitles.length;k++) {
			if (this.options.columnsVisibility[k]) {
				if (this.options.columnsTitles[k]!="") {
					var op=new Element('option', { 'value': this.options.columnsFields[k]}).update(this.options.columnsTitles[k]);
					this.searchCombo.appendChild(op);
				}
			}
		}
	},
	
	request: function() {
		if (this.options.controlBar) {
			this.imgReloadPage.src=this.options.imageFolder+"/load.gif";
			this.setText(this.options.processingText,this.dStatus);
		
			params="limit1="+(this.rowSelector.value*(this.currentPage-1))+"&limit2="+this.rowSelector.value+"&page="+this.currentPage;
		} else {
			params="limit1="+(this.options.rowsSelectorOpt[0]*(this.currentPage-1))+"&limit2="+this.options.rowsSelectorOpt[0]+"&page="+this.currentPage;
		}
		
		if (this.orderCol!=-1) {
			params+="&ordercol="+this.orderCol+"&orderDir="+this.orderDir;
		}
		
		if (this.options.parameters!="") {
			params+="&"+this.options.parameters;
		}
		
		if (this.searchParams!="") {
			params+=this.searchParams;
		}
		
		var method=this.options.queryMethod;
		
		if (this.options.dataUrl!="") {
			var ajax=new Ajax.Request( this.options.dataUrl, {
				method: this.options.queryMethod,
				parameters: params,
				onComplete: function(reply) {
					this.paintGrid(reply);
					
				}.bind(this)
			});
		}
	},
	
	paintGrid: function(reply) {
		
		if (reply.responseXML) {
			var rows=reply.responseXML.selectSingleNode("/rows");
			
			this.currentPage=rows.getAttribute("page");
			this.totalItems=rows.getAttribute("total");
			
			//ja tenim els items totals i la pagina actual
			if (this.options.controlBar) {
			this.iTextboxPage.value=this.currentPage;
			
			//ara calculam el numero de pagines
			this.totalPagines=parseInt(this.totalItems/this.rowSelector.value)+1;
			if (this.totalItems%this.rowSelector.value==0) {
				this.totalPagines--;
			}
			
			if (this.totalPagines==0) {
				this.totalPagines=1;
			}
			
			this.setText(this.totalPagines,this.dTotPage);
			
			if ((this.currentPage*this.rowSelector.value)>this.totalItems) {
				var texto=this.totalItems;
			} else {
				var texto=this.currentPage*this.rowSelector.value;
			}
			
			this.setText(this.options.displayingText+
						" "+(((this.currentPage-1)*this.rowSelector.value)+1)+
						" "+this.options.toText+
						" "+texto+
						" "+this.options.ofText+
						" "+this.totalItems+
						" "+this.options.itemText,this.dStatus);
			}
			this.clearContainer(this.dGridCont);
			
			//abans de pintar calculam l'amplaria total de la taula sumant les dimensions de cada columna
			var amplariaTotal=0;
			
			//comensam a pintar capcaleres
			this.dTableHead=new Element('div', { 'class':'tableHeaderCont' });
			this.dGridCont.appendChild(this.dTableHead);
			this.tableHeader=new Element("table");
			this.tableHeader.setAttribute("cellpadding","0");
			this.tableHeader.setAttribute("cellspacing","0");
			var tbody=new Element('tbody');
			this.rowHeader=new Element("tr");
			this.dTableHead.appendChild(this.tableHeader);
			this.tableHeader.appendChild(tbody);
			tbody.appendChild(this.rowHeader);
			
			//si hi ha borders a la dreta o a l'esquerra de cada celda,
			//he d'afegir l'amplaria del borde a l'amplaria total
			var addBorder;
			
			for (var k=0;k<this.options.columnsTitles.length;k++) {
				
				var td=new Element("td", { 'class':'tableHeader' });
				
				if (this.options.columnsVisibility[k]) {
					td.style.width=this.options.columnsWidth[k]+"px";
				} else {
					td.style.display="none";
					this.options.columnsWidth[k]=0;
					this.options.columnsDraggables[k]=false;
				}
				td.setAttribute("sort","0");
				
				td.onmouseover=function() {
					this.className="tableHeaderOver";
					
					if (this.getAttribute("sort")=="0") {
						this.firstChild.className="cellDivUp";
					}
					
					if (this.getAttribute("sort")=="1") {
						this.firstChild.className="cellDivDown";
					}
					
					if (this.getAttribute("sort")=="2") {
						this.firstChild.className="cellDivUp";
					}
				}
				
				td.onmouseout=function() {
					this.className="tableHeader";
					
					if (this.getAttribute("sort")=="0") {
						this.firstChild.className="cellDiv";
					}
					
					if (this.getAttribute("sort")=="1") {
						this.firstChild.className="cellDivUp";
					}
					
					if (this.getAttribute("sort")=="2") {
						this.firstChild.className="cellDivDown";
					}
				}
				
				var dummy=this;
				
				td.onclick=function() {
					
					if (this.getAttribute("sort")=="0") {
						this.setAttribute("sort","1");
						this.firstChild.className="cellDivUp";
						dummy.orderDir="ASC";
					} else {
						if (this.getAttribute("sort")=="1") {
							this.setAttribute("sort","2");
							this.firstChild.className="cellDivDown";
							dummy.orderDir="DESC";
						} else {
							if (this.getAttribute("sort")=="2") {
								this.setAttribute("sort","1");
								this.firstChild.className="cellDivUp";
								dummy.orderDir="ASC";
							}
						}
					}
					
					var row=this.parentNode;
					
					for (var k=0;k<row.childNodes.length;k++) {
						if (this!=row.childNodes[k]) {
							row.childNodes[k].firstChild.className="cellDiv";
							row.childNodes[k].setAttribute("sort","0");
						} else {
							dummy.orderCol=dummy.options.columnsFields[k];
						}
					}
					
					dummy.request();
				}
				
				
				var d=new Element('div');
				d.style.textAlign=this.options.columnsAlign[k];
				d.style.width=this.options.columnsWidth[k];
				d.className="cellDiv";
				
				if (this.options.columnsTitles[k]=="") {
					d.innerHTML="&nbsp;";
				} else {
					var t=document.createTextNode(this.options.columnsTitles[k]);
					d.appendChild(t);
				}
				
				td.appendChild(d);
			
				if (this.orderCol==this.options.columnsFields[k]) {
					if (this.orderDir=="ASC") {
						td.setAttribute("sort","1");
						d.className="cellDivUp";
					}
					
					if (this.orderDir=="DESC") {
						td.setAttribute("sort","2");
						d.className="cellDivDown";
					}
				}
				
				
				this.rowHeader.appendChild(td);
			}
			
			for (var k=0;k<this.options.columnsWidth.length;k++) {
					amplariaTotal=amplariaTotal+this.options.columnsWidth[k];
			}
			
			this.tableHeader.style.width=amplariaTotal+"px";
			this.dTableHead.style.width=this.tableHeader.getWidth()+"px";
			
			//en aquest punt feim el div que contendrá els altres divs per a poder canviar l'amplaria
			//de les columnes
			
			this.dDragCont=new Element('div', { 'class':'dragCont'});
			this.dDragCont.style.top=this.dTableHead.cumulativeOffset()[1]+"px";
			this.dGridCont.appendChild(this.dDragCont);
			
			var culumativeLeft=0;
				
			for (var k=0;k<this.tableHeader.firstChild.firstChild.childNodes.length;k++) {
				culumativeLeft=culumativeLeft+(this.tableHeader.firstChild.firstChild.childNodes[k].getWidth());
				
				var d=new Element('div');
				
				if (this.options.columnsDraggables[k]) {
					d.className="dragger";
				} else {
					d.className="noDrag";
				}
				d.id="colDrag"+(k);
				d.style.left=(culumativeLeft-5)+"px";
				
				d.style.height=this.dGridCont.getHeight()+"px";
				
				this.dDragCont.appendChild(d);
				
				if (this.options.columnsDraggables[k]) {
					d.onmouseover=function() {
						this.className="draggerOver";
					}
				
					d.onmouseout=function() {
						this.className="dragger";
					}
				}
				
				if (this.options.columnsDraggables[k]) {
					new Draggable( d, { constraint: "horizontal",
								onEnd: function(draggable, event) {
	        						var k=draggable.element.id.substring(7);
	        						var colsDreta=0;
	        						for (var i=0; i<k; i++) {
	        							colsDreta=colsDreta+this.tableHeader.firstChild.firstChild.childNodes[i].getWidth();
	        						}
	        						
	        						this.options.columnsWidth[k]=parseInt(draggable.element.style.left)-colsDreta+5;
	        						this.resizeCols();
								}.bind(this),
								onDrag: function(draggable,event) {
									if (draggable.element==draggable.element.parentNode.firstChild) {
										if (parseInt(draggable.element.style.left)<=0) {
											draggable.element.style.left="8px";
											draggable.endDrag();
										}
									} else {
										if (parseInt(draggable.element.style.left)<=parseInt(draggable.element.previousSibling.style.left)) {
											draggable.element.style.left=(parseInt(draggable.element.previousSibling.style.left)+8)+"px";
											draggable.endDrag();
										}
									}
								}.bind(this)
					});
				}
			}
			
			//a continuació cream el div que contendrá la taula amb les dades:
			this.dContDataTable=new Element("div");
			this.dContDataTable.style.width=this.options.width+"px";
			this.dContDataTable.style.overflow="auto";
			this.dContDataTable.style.height=(this.dGridCont.getHeight()-this.dTableHead.getHeight())+"px";
			this.dContDataTable.onscroll=function() {
				//aixo fara que les capcaleres es desplacin amb l'scroll
				this.dTableHead.style.marginLeft="-"+this.dContDataTable.scrollLeft+"px";
				
				//are repintam els draggables
				for (var k=0; k<this.dDragCont.childNodes.length;k++) {
					this.dDragCont.childNodes[k].style.marginLeft="-"+this.dContDataTable.scrollLeft+"px";
				}
			}.bind(this);
			this.dGridCont.appendChild(this.dContDataTable);
			
			if (rows.childNodes.length==0) {
				var tNoData=document.createTextNode(this.options.noItems);
				this.dContDataTable.appendChild(tNoData);
				this.imgReloadPage.src=this.options.imageFolder+"/load.png";
				return;
			}
			
			this.dataTable=new Element('table');
			this.dataTable.style.width=amplariaTotal+"px";
			this.dataTable.setAttribute("cellpadding","0");
			this.dataTable.setAttribute("cellspacing","0");
			this.dContDataTable.appendChild(this.dataTable);
			var tbody2=new Element('tbody');
			this.dataTable.appendChild(tbody2);
			var primera=true;
			
			for (var k=0;k<rows.childNodes.length;k++) {
				var tr=new Element("tr");
				
				if (k%2==0) {
					tr.className="tr1";
				} else {
					tr.className="tr2";
				}
				
				tr.setAttribute("oldClass",tr.className);
				tr.setAttribute("selected","0");
				
				tr.onmouseover=function() {
					this.className="trOver";
				}
				
				tr.onmouseout=function() {
					if (this.getAttribute("selected")=="0") {
						this.className=this.getAttribute("oldClass");
					}
				}
				
				tbody2.appendChild(tr);
				
				for (var j=0;j<this.options.columnsTitles.length;j++) {
					var td=new Element("td");
					
					if (k%2==0) {
						td.className="td1";
					} else {
						td.className="td2";
					}
					
					td.style.width=this.options.columnsWidth[j]+"px";
					
					var d=new Element('div');
					td.appendChild(d);
					tr.appendChild(td);
					
					td.setAttribute("col",j);
					td.setAttribute("row",k);
					
					if (this.options.columnsVisibility[j]==false) {
						td.style.display="none";
					} else {
						td.style.width=this.options.columnsWidth[j]+"px";
						td.firstChild.style.width=(this.options.columnsWidth[j])+"px";
					}
					
					var dummy=this;
					
					td.onclick=function() {
					
						if (dummy.options.multiSelect==false) {
							for (var k=0;k<dummy.dataTable.firstChild.childNodes.length;k++) {
								if (k%2==0) {
									dummy.dataTable.firstChild.childNodes[k].className="tr1";
								} else {
									dummy.dataTable.firstChild.childNodes[k].className="tr2";
								}
								
								dummy.dataTable.firstChild.childNodes[k].setAttribute("selected","0");
							}
							
							//buidam l'array
							while (dummy.arrRows.length>0) {
								dummy.arrRows.pop();
							}
						}					
						
						for (var k=0;k<this.parentNode.parentNode.childNodes.length;k++) {
							if (dummy.dataTable.firstChild.childNodes[k].getAttribute("selected")=="0") {
								dummy.arrRows[k]=null;
							}
							if (this.parentNode.parentNode.childNodes[k]==this.parentNode) {
								var fila=k;
							}
						}
						
						for (var k=0;k<this.parentNode.childNodes.length;k++) {
							if (this.parentNode.childNodes[k]==this) {
								var columna=k;
							}
						}
						
						
						if (this.parentNode.getAttribute("selected")=="0") {
							this.parentNode.className="trOver";
							this.parentNode.setAttribute("selected","1");
							
							var arrData=new Array();
							
							for (var k=0;k<this.parentNode.childNodes.length;k++) {
								if (this.parentNode.childNodes[k].firstChild.firstChild) {
									arrData[k]=this.parentNode.childNodes[k].firstChild.firstChild.nodeValue;
								} else {
									arrData[k]="";
								}
							}
							
							dummy.arrRows[fila]=arrData;
							
						} else {
							this.parentNode.className=this.parentNode.getAttribute("oldClass");
							this.parentNode.setAttribute("selected","0");
							dummy.arrRows[fila]=null;
						}
						
						dummy.cellClick(dummy.arrRows,fila,columna);	
					}.bind(td);
					
					d.className="divData";
					d.style.textAlign=this.options.columnsAlign[j];
					
					if (rows.childNodes[k].childNodes[j].firstChild) {
						d.innerHTML=rows.childNodes[k].childNodes[j].firstChild.nodeValue;
					} else {
						d.innerHTML="&nbsp;";
					}
				}
				
				primera=false;
				
			}
		}
		
		this.imgReloadPage.src=this.options.imageFolder+"/load.png";
	},
	
	cellClick: function(arrData,row,column) {
		var obj=new Object();
		obj.row=row;
		obj.column=column;
		
		var arrSelected=new Array();
		var arrCurrentRow=new Array();
		
		for (var k=0;k<arrData.length;k++) {
			if (arrData[k]) {
				arrSelected.push(arrData[k]);
			}
		}
		
		obj.arrData=arrSelected;
		obj.arrCurrentRow=arrData[row];
		
		this.selectedData=arrSelected;
		this.currentRow=row;
		this.currentColumn=column;
		
		if (this.options.onclick) {
			this.options.onclick.call(obj);
		}
	},
	
	getSelectedData: function() {
		return this.selectedData;
	},
	
	getCurrentRow: function() {
		return this.currentRow;
	},
	
	getCurrentColumn: function() {
		return this.currentColumn;
	},
	
	resizeCols: function() {
	
		//aquesta funció s'encarrrega de redimensionar les columnes
		var colHeaders=this.rowHeader.childNodes;
		var offsetLeft=0;
		
		//primer recalculam l'amplaria de la taula dels encapsalament
		for (var k=0;k<this.options.columnsWidth.length;k++) {
			offsetLeft=offsetLeft+this.options.columnsWidth[k];
		}
		
		this.tableHeader.style.width=offsetLeft+"px";
		
		
		for (var k=0;k<colHeaders.length;k++) {
			colHeaders[k].style.width=this.options.columnsWidth[k]+"px";
			colHeaders[k].firstChild.style.width=this.options.columnsWidth[k]+"px";
		}
		
		this.dTableHead.style.width=this.tableHeader.getWidth()+"px";
		
		var offsetLeft=0;
		
		for (var k=0;k<colHeaders.length;k++) {
			offsetLeft=offsetLeft+colHeaders[k].getWidth();
			this.dDragCont.childNodes[k].style.left=(offsetLeft-5)+"px";
		}
		
		if (this.dataTable) {
			this.dataTable.style.width=offsetLeft+"px";
			
			
			for (var j=0;j<this.dataTable.firstChild.childNodes.length;j++) {
				var tr=this.dataTable.firstChild.childNodes[j];
				for (var k=0;k<colHeaders.length;k++) {
					this.dataTable.firstChild.childNodes[j].childNodes[k].style.width=this.options.columnsWidth[k]+"px";
					this.dataTable.firstChild.childNodes[j].childNodes[k].firstChild.style.width=(this.options.columnsWidth[k])+"px";
				}
			}
		}
	},
	
	setText: function(text,container) {
		this.clearContainer(container);
		var tText=document.createTextNode(text);
		container.appendChild(tText);
	},
	
	clearContainer: function(container) {
		if (container.childNodes) {
			for(var i=container.childNodes.length-1;i>=0;i--){
                var childNode=container.childNodes[i];
                container.removeChild(childNode);
            }
		}
	},
	
	addButton: function(barra,text,image,click) {
		if (barra==1) {
			var dBarra=this.dButtonBar1;
		}
		
		if (barra==2) {
			var dBarra=this.dButtonBar2;
		}
		
		var dBut=document.createElement('div');
			dBut.className="pButton";
			dBut.style.paddingTop="2px";
			dBut.style.marginRight="1px";
			dBut.style.height="19px";
			dBut.onmouseover=function() {
				this.className="pButtonOver";	
			}
			
			dBut.onmouseout=function() {
				this.className="pButton";
			}
			
			if (text) {
				dBut.style.paddingLeft="2px";
				dBut.style.paddingRight="2px";
				var t=document.createTextNode(text);
				dBut.appendChild(t);
				
			}
			
			if (image) {
				var img=document.createElement('img');
				img.src=image;
				img.align="absmiddle";
				img.style.marginLeft="4px";
				dBut.appendChild(img);
			}
			
			if (click) {
				dBut.onclick=function() { 
					click.call(this); 
				}.bind(this);
			}
			
			dBarra.appendChild(dBut);
	},
	
	addSeparator: function(bar) {
		var d=document.createElement('div');
		d.className="pButtonSeparator";
		bar.appendChild(d);
	},
	
	loadRowSelectorOptions: function() {
		var options=this.options.rowsSelectorOpt;
		for (var k=0;k<options.length;k++) {
			var op=document.createElement('option');
			op.value=options[k];
			var tOp=document.createTextNode(options[k]);
			op.appendChild(tOp);
			this.rowSelector.appendChild(op);
		}
	},
	
	setMultiselect: function (bol) {
		this.options.multiSelect=bol;
	},
	
	getMultiselect: function() {
		return this.options.multiSelect;
	},
	
	setParameters: function(params) {
		this.options.parameters=params;
	},
	
	getParameters: function() {
		return this.options.paramters;
	},
	
	getDataUrl: function() {
		return this.options.dataUrl;
	},
	
	setDataUrl: function(nDataUrl) {
		this.options.dataUrl=nDataUrl;
	},
	
	mostraCerca: function() {
		//this.elem.style.height="auto";
		var h1=this.dGridCont.getHeight();
		var h2=this.dSearchBar.getHeight();
		var h3=this.dGridCont.lastChild.getHeight();
		
		new Effect.Morph(this.dGridCont, {
			style: {
				height: (h1-h2)+"px"
			},
			duration: 1.0
		});
		
		new Effect.Morph(this.dGridCont.lastChild, {
			style: {
				height: (h3-h2)+"px"
			},
			duration: 1.0
		});
		
		this.dSearchBar.appear({ duration: 1.0 });
	},
	
	amagaCerca: function() {
		var h1=this.dGridCont.getHeight();
		var h2=this.dSearchBar.getHeight();
		var h3=this.dGridCont.lastChild.getHeight();
		
		new Effect.Morph(this.dGridCont, {
			style: {
				height: (h1+h2)+"px"
			},
			duration: 1.0
		});
		
		new Effect.Morph(this.dGridCont.lastChild, {
			style: {
				height: (h3+h2)+"px"
			},
			duration: 1.0
		});
	
		this.dSearchBar.fade({ duration: 1.0 });
	},
	
	limpia: function() {
		this.iSearch.value="";
		this.searchParams="";
		this.request();
	},
	
	cerca: function() {
		if (this.iSearch.value!="") {
			this.searchParams="&searchField="+this.searchCombo.value+"&searchValue="+this.iSearch.value;
			this.currentPage=1;
		} else {
			this.searchParams="";
		}
		
		this.request();
	}
});
