dojo.declare("Miolo.Grid", null, {
    constructor: function() {
	},
	check : function (chkRow, checkboxId) {
       var tr = miolo.getElementById('row' + checkboxId);
       var chkRow = miolo.getElementById('select'+ checkboxId);
       if (chkRow.checked) {
          if (tr.className=='row1')
             tr.className='row1checked';
          else if (tr.className=='row2')
             tr.className='row2checked';
          else if (tr.className=='row0')
             tr.className='row0checked';
       }
       else {
          if (tr.className=='row1checked')
             tr.className='row1';
          else if (tr.className=='row2checked')
             tr.className='row2';
          else if (tr.className=='row0checked')
             tr.className='row0';
       }
    },
	checkAll: function (chkAll, n, gridname) {
       for( var i=0; i < n; i++ ) {
          var chkRow = miolo.getElementById('select'+ gridname +'[' + i + ']');
          var chkAll = miolo.getElementById(gridname + 'chkAll');
          if (chkAll.checked != chkRow.checked) {
             chkRow.checked = chkAll.checked;
             miolo.grid.check(chkRow, gridname +'[' + i + ']');
          }
       } 
    },
	checkEachRow: function (n, gridname) {
       for ( var i=0; i < n; i++ ) {
          var chkRow = miolo.getElementById('select'+ gridname +'[' + i + ']');
          miolo.grid.check(chkRow, gridname +'[' + i + ']');
       } 
    },
	ajustSelect: function (className) {
//	   var nodes = document.getElementsByTagName('span');
	   var nodes = dojo.query("tbody > tr > td.btn > span." + className);
       var h = 15;
       for(i = 0; i < nodes.length; i++) {
           if ((nodes[i].parentNode.clientHeight) && (nodes[i].className == className)) {
    	      nodes[i].style.height = h + 'px';
	       }
	   }
       for(var i = 0; i < nodes.length; i++) {
           if ((nodes[i].parentNode.clientHeight) && (nodes[i].className == className)) {
			   var c = nodes[i].parentNode.clientHeight;
			   var m = c > h ? c : h;
    	       nodes[i].style.height = m + 'px';
	       }
	   }
       /*
       for(i = 0; i < nodes.length; i++) {
           if ((nodes[i].parentNode.clientHeight) && (nodes[i].className == className)) {
    	      nodes[i].style.height = h + 'px';
	       }
	   }
       */
    },
	ajustTHead: function () {
       var h = 15;
	   var nodes = document.getElementsByTagName('th');
/*
       for(var i = 0; i < nodes.length; i++) {
    	   var children = nodes[i].getElementsByTagName('span');
           for(j = 0; j < children.length; j++) {
              children[j].style.height = h + 'px';
		   }
	   }
*/
       for(var i = 0; i < nodes.length; i++) {
		   var c = nodes[i].clientHeight;
		   h = c > h ? c : h;
	   }
       for(var i = 0; i < nodes.length; i++) {
    	   var children = nodes[i].getElementsByTagName('span');
           for(j = 0; j < children.length; j++) {
              children[j].style.height = h + 'px';
		   }
	   }
    },
    scroll: function (tableEl, tableHeight, tableWidth) {
 
		this.initIEengine = function () {
			this.containerEl.style.overflowY = 'auto';
			if (this.tableEl.parentElement.clientHeight - this.tableEl.offsetHeight < 0) {
				this.tableEl.style.width = this.newWidth - this.scrollWidth +'px';
			} else {
				this.containerEl.style.overflowY = 'hidden';
				this.tableEl.style.width = this.newWidth +'px';
			}
 
			if (this.thead) {
				var trs = this.thead.getElementsByTagName('tr');
				for (x=0; x<trs.length; x++) {
					trs[x].style.position ='relative';
					trs[x].style.setExpression("top",  "this.parentElement.parentElement.parentElement.scrollTop + 'px'");
				}
			}
 
			if (this.tfoot) {
				var trs = this.tfoot.getElementsByTagName('tr');
				for (x=0; x<trs.length; x++) {
					trs[x].style.position ='relative';
					trs[x].style.setExpression("bottom",  "(this.parentElement.parentElement.offsetHeight - this.parentElement.parentElement.parentElement.clientHeight - this.parentElement.parentElement.parentElement.scrollTop) + 'px'");
				}
			}
 
			eval("window.attachEvent('onresize', function () { document.getElementById('" + this.tableEl.id + "').style.visibility = 'hidden'; document.getElementById('" + this.tableEl.id + "').style.visibility = 'visible'; } )");
		};
 
		this.initFFengine = function () {
			this.containerEl.style.overflow = 'hidden';
			this.tableEl.style.width = this.newWidth + 'px';
 
			var headHeight = (this.thead) ? this.thead.clientHeight : 0;
			var footHeight = (this.tfoot) ? this.tfoot.clientHeight : 0;
			var bodyHeight = this.tbody.clientHeight;
			var trs = this.tbody.getElementsByTagName('tr');
			if (bodyHeight >= (this.newHeight - (headHeight + footHeight))) {
				this.tbody.style.overflow = '-moz-scrollbars-vertical';
				for (x=0; x<trs.length; x++) {
					var tds = trs[x].getElementsByTagName('td');
					tds[tds.length-1].style.paddingRight += this.scrollWidth + 'px';
				}
			} else {
				this.tbody.style.overflow = '-moz-scrollbars-none';
			}
 
			var cellSpacing = (this.tableEl.offsetHeight - (this.tbody.clientHeight + headHeight + footHeight)) / 4;
			this.tbody.style.height = (this.newHeight - (headHeight + cellSpacing * 2) - (footHeight + cellSpacing * 2)) + 'px';
 
		};
 
		var x = miolo.getElementById(tableEl);
		this.tableEl = miolo.getElementById(tableEl);
		this.scrollWidth = 16;
 
		this.originalHeight = this.tableEl.clientHeight;
		this.originalWidth = this.tableEl.clientWidth;
 
		this.newHeight = parseInt(tableHeight);
		this.newWidth = tableWidth ? parseInt(tableWidth) : this.originalWidth;
 
		this.tableEl.style.height = 'auto';
		this.tableEl.removeAttribute('height');
 
		this.containerEl = this.tableEl.parentNode.insertBefore(document.createElement('div'), this.tableEl);
		this.containerEl.appendChild(this.tableEl);
		this.containerEl.style.height = this.newHeight + 'px';
		this.containerEl.style.width = this.newWidth + 'px';
 
 
		var thead = this.tableEl.getElementsByTagName('thead');
		this.thead = (thead[0]) ? thead[0] : null;
 
		var tfoot = this.tableEl.getElementsByTagName('tfoot');
		this.tfoot = (tfoot[0]) ? tfoot[0] : null;
 
		var tbody = this.tableEl.getElementsByTagName('tbody');
		this.tbody = (tbody[0]) ? tbody[0] : null;
 
		if (!this.tbody) return;
 
		if (document.all && document.getElementById && !window.opera) this.initIEengine();
		if (!document.all && document.getElementById && !window.opera) this.initFFengine();

     }
});

miolo.grid = new Miolo.Grid();

