var JadeGrids= {
	_initial: function(){
		document.jade.grid = {};
	},
	_default_properties: function(object) {
		object.rows = [];
		object.headers = [];
		object.columnmodel = {};
		object.parent=document.getElementById('body');
		object.style.cursor = 'default';
	},
	_default_functions: function(object) {
		object.Size = function(width,height){
			if(width!=null) { this.style.width = Jade.Objects._set_size(width); }
			if(height!=null) { this.style.height = Jade.Objects._set_size(height); }
			if(typeof(this.onresize)!="undefined" && typeof(this.onresize)!="object") { this.onresize(); }
		}
		object.Position = function (x,y){
			this.style.left = Jade.Objects._set_size(x);
			this.style.top = Jade.Objects._set_size(y);
		}
		object.Location = function(x,y,width,height) {
			this.Position(x,y);
			this.Size(width,height);

		}
		object.Add = function(parent)
		{
			if(typeof(parent)!="undefined" && parent!=null) { this.parent = parent; }
			this.style.position = 'absolute';
			this.parent.appendChild(this);
			if(typeof(this.onadd)!="undefined" && typeof(this.onadd)!="object") { this.onadd(); }
			if(typeof(this.onpaint)!="undefined" && typeof(this.onpaint)!="object") { this.onpaint(); }
		}
	},
	_check_event: function(object,event) {
		return (typeof(object[event])!="undefined" && typeof(object[event])!="object") ? true : false;

	},
	
	Grid: function(columnmodel) {
		{//Construccion
			var object = $.DOM.Add(null, 'div', 'jade_grid');
			Jade.Grids._default_properties(object);
			Jade.Grids._default_functions(object);
		
			if($.isDefined(columnmodel)){ object.columnmodel=columnmodel; }
		
			object.border = $.DOM.Add(object, 'div', 'gr_border');
			object.head = $.DOM.Add(object.border, 'div', 'gr_head');
			object.body = $.DOM.Add(object.border, 'div', 'gr_body');
		}
		
		object.onpaint = function(){
			for(var i=0; i<this.columnmodel.columns.length; i++){
				
				var column = this.columnmodel.columns[i];
				var header = document.createElement('div');
				//-----------------------------------------------------------
				Jade.Events.NoSelect(header);
				column.references = {};
				column.references.grid = this;
				column.references.header = header;
				header.references = {};
				header.references.grid = this;
				header.references.column = column;
				//-----------------------------------------------------------								
				header.className = 'gr_header';
				header.style.width = column.width +'px';
				header.style.position = 'relative';
				header.style.cssFloat = 'left';
				header.style.styleFloat = 'left';
				header.style.height = '100%';
				header.style.margin = '0 -1px 0 0';
				if(i!=0){
					header.style.borderStyle = 'solid';
					header.style.borderWidth = '0 0 0 1px';
				}
				if(column.sortable){ header.style.cursor = 'pointer'; }
				header.innerHTML = '<p class="text-size">'+column.text+'</p>';
				//-----------------------------------------------------------
				header.sortable = column.sortable;
				header.order = 0;
				//-----------------------------------------------------------
				header.onmouseover=function(){
					if(this.sortable) { this.className += ' hover'; }
				}
				header.onmouseout=function(){
					this.className = this.className.replace(/ hover/,'');
				}
				
				header.onclick = function(){
					if(this.sortable){
						switch(this.order){
							case 0:
								this.order=1;
								var arrow = $.dom.Add(this, null, 'gr_arrow gr_down');
								arrow.style.left=(this.offsetWidth - 19)+'px';
								break;
							case 1:
								this.order=2;
								this.lastChild.className='gr_arrow gr_up';
								break;
							case 2:
								this.order=1;
								this.lastChild.className='gr_arrow gr_down';
								break;
						}
						this.references.column.order();
					}
				}
				column.order=function(descending){
					function compareValues(a, b){
						var order=a.references.grid.headers[index].order;
						a = $.Convert.ToString(a.cells[index].value);
						b = $.Convert.ToString(b.cells[index].value);
							
						if(order==1){
							return a.localeCompare(b);
						}
						else{
							return b.localeCompare(a);
						}
					}
					var index = this.index;
					this.references.grid.rows.sort(compareValues);
					this.references.grid.Show();					
				}
				this.head.appendChild(header);
				this.headers.push(header);
			}
		}
		
		object.CreateRow=function (row, key, beforeRow){
			row.key=key;
			row.beforeRow=beforeRow;
			row.references={grid: object};
			row.index=this.rows.length;
			if(!$.isDefined(this.pagination) || !this.pagination){
				return this.DrawRow(row); 
			}
			else{
				this.rows.push(row);
			}
		}
		
		object.DrawRow=function (row) {
			if(row.constructor == Jade.Grids.Row && this.columnmodel!=null){
				//Paint row
				var cssRow = document.createElement('div');
				cssRow.subclassName = '';
				cssRow.style.position = 'relative';
				cssRow.style.width = '100%';
				cssRow.style.borderStyle = 'solid';
				cssRow.style.borderWidth = '0 0 1px 0';
				cssRow.style.cursor = 'pointer';
				//cssRow.style.clear = 'both';
				cssRow.onmouseover=function(){
					this.className=this._className + ' hover';
				}
				cssRow.onmouseout=function(){
					this.className=this._className;
				}

				//Make row
				if(!$.isDefined(row.key)){ row.key=''; }
				row.subitems=[];
				row.editable=true;
				row.references.css_row = cssRow;
				row.Parse(this.columnmodel);
				
				if(!$.isDefined(this.pagination) || !this.pagination){
					if(typeof(row.beforeRow)=="undefined"){
						this.body.appendChild(cssRow);
						this.rows.push(row);				
					}
					else{
						Jade.DOM.InsertBefore(cssRow, row.beforeRow.references.css_row);
						for(var i=row.beforeRow.index; i<this.rows.length; i++){
							this.rows[i].index += 1;
						}
						row.index=row.beforeRow.index-1;
						this.rows.splice(row.beforeRow.index-1,0,row);
					}
				}
				else{ this.body.appendChild(cssRow); }
				
				cssRow.className = (row.index%2==0) ? 'gr_row' : 'gr_row pair';
				cssRow._className = cssRow.className;

				
				//Paint row cells
				for(var i=0; i < this.columnmodel.columns.length; i++){
					//Make cell
					var cssCell = document.createElement('div');
					var cell = row.cells[i];
					cell.references = {};
					cell.references.column = this.columnmodel.columns[i];
					cell.references.header = this.head.childNodes[i];
					cell.references.row = row;
					cell.references.grid = this;
					cell.references.css_cell = cssCell;
					cell.references.css_row = cssRow;

					cell.editable = cell.references.column.editable;
					cell.fixedfocus = cell.references.column.fixedfocus;
					cell.clickable = (cell.references.column.clickable=="true" || cell.references.column.clickable==true)?true:false;
					cell.ChangeStyle = function (className){
						this.references.css_cell.className = 'gr_value '+className;
					}
					cell.ClearStyle=function(className){
						this.references.css_cell.className = 'gr_value';
					}

					cell.SetValue=function(value){
						this.value = value;
						this.references.css_cell.innerHTML='';
						this.references.column.render(this);
					}
					if(Jade.Grids._check_event(cell.references.column,'SetValue')){ 
						cell.SetValue=function(value){
							this.references.column.SetValue(this,value); 
						}
					}
					cell.onfixedfocus=function(){
						if(this.fixedfocus) {
							this.references.grid.ClearFocus();
							this.references.grid.selectedKey = this.references.row.key;
							this.references.grid.selectedIndex = this.references.row.index;
							this.references.row.ChangeStyle('focus');
						}
					}

					//Paint cell
					cssCell.cell = cell;
					cssCell.style.position = 'relative';
					cssCell.style.cssFloat = 'left';
					cssCell.style.styleFloat = 'left';
					cssCell.style.height = '100%';
					cssCell.style.padding = '0';
					cssCell.style.margin = '0 -1px 0 0';
					cssCell.className = 'gr_value';
					cssCell.style.width = cell.references.column.width+'px';
					if(!cell.editable) { cssCell.style.cursor = 'default'; }
					if(Jade.Grids._check_event(this,'onrowclick') || cell.fixedfocus) { cssCell.style.cursor = 'pointer'; }
					if(!cell.selectable) { Jade.Events.NoSelect(cssCell); }
					
					if(i!=0){
						cssCell.style.borderStyle = 'solid';
						cssCell.style.borderWidth = '0 0 0 1px';
					}

					//-------------------------------------------------------------------
					cell.references.column.render(cell);
					
					cssCell.onmouseover=function(){
						if(Jade.Grids._check_event(this.cell,'onmouseover')) { this.cell.onmouseover(); }
					}
					cssCell.onmouseout=function(){
						if(Jade.Grids._check_event(this.cell,'onmouseout')) { this.cell.onmouseout(); }
					}
					cssCell.onmousedown=function(ev){
						if(ev==null){ ev=event; }
						if(Jade.DetectBrowser()=="Opera" && ev.altKey==1 && Jade.Grids._check_event(this.cell,'oncontextmenu')) { this.cell.oncontextmenu(ev); }
					}
					cssCell.oncontextmenu=function(ev){
						if(ev==null){ ev=event; }
						if(Jade.DetectBrowser()!="Opera" && Jade.Grids._check_event(this.cell,'oncontextmenu')) { this.cell.oncontextmenu(ev); }
						return false;
					}
					cssCell.onclick=function(ev){
						if(this.cell.editable && this.cell.references.row.editable){
							this.cell.create_edit_area();
							this.cell.references.column.editor(this.cell);
						}
						this.cell.onfixedfocus();
						if(this.cell.clickable && Jade.Grids._check_event(this.cell.references.grid,'onrowclick')){  this.cell.references.grid.onrowclick(this.cell.references.row); }
						if(Jade.Grids._check_event(this.cell.references.column,'oncellclick')){ this.cell.references.column.oncellclick(this.cell); }
					}
					cssCell.ondblclick=function(){
						if(this.cell.clickable && Jade.Grids._check_event(this.cell,'ondblclick')) { this.cell.ondblclick(); }
					}

					
					cell.create_edit_area=function(){
						var area = document.createElement('div');
						area.className = 'edit-area';
						area.style.position = 'absolute';
						area.style.border = 'none';
						area.style.outline = 'none';
						
						var x=Jade.Css.RealPosition(this.references.header)[0]+1;
						var y=Jade.Css.RealPosition(this.references.css_cell)[1];
						var width = this.references.css_cell.offsetWidth;
						var height = this.references.css_cell.offsetHeight+2;
						
						if(this.index==0) { x--; width++; }
						if(navigator.userAgent.toLowerCase().indexOf("opera") != -1) { y--; x--; }
						area.style.left = x+'px';
						area.style.top = y+'px';
						area.style.width = width+'px';
						area.style.height = height+'px';
						area.style.zIndex = 102;
						area.tabIndex=0;
						document.body.appendChild(area);
						this.references.edit_area = area;
					}
					//-------------------------------------------------------------------
					cssRow.appendChild(cssCell);
				}
				row.ChangeStyle=function(className){
					var cssRow=this.references.css_row;
					cssRow.subclassName=className;
					cssRow.className='gr_row '+className+((row.index%2==0) ? '' : ' pair');
					cssRow._className=cssRow.className;
				}
				row.ClearStyle=function(){
					this.ChangeStyle('');
				}
				row.SetEditable=function(value){
					if(!value){ this.ChangeStyle('disabled'); }
					else{ this.ClearStyle(); }
					this.editable=value;
					for(var i=0,iLen=this.cells.length;i<iLen;i++){
						switch(this.cells[i].references.column.type){
							case 'combo':
								this.cells[i].references.object.SetEnabled(value);
								break;
						}	
					}
				}
				row.SetClickable=function(value){
					var cssRow = this.references.css_row;
					if(value){
						cssRow.style.cursor='pointer';
						if(typeof(cssRow._onmouseover)!="undefined"){
							cssRow.onmouseover=cssRow._onmouseover;
							cssRow.onmouseout=cssRow._onmouseout;
						}
					}
					else{
						cssRow.style.cursor='default';
						cssRow._onmouseover=cssRow.onmouseover;
						cssRow._onmouseout=cssRow.onmouseout;
						cssRow.onmouseover=function(){};
						cssRow.onmouseout=function(){};
					}
				}
				row.Delete=function(){
					var current_index=this.index;

					if(typeof(this.subitems)!="undefined"){
						for(var i=0;i<this.subitems.length;i++) {
							$.dom.Remove(this.subitems[i]);
						}
					}
					
					$.dom.Remove(this.references.css_row);
					this.references.grid.rows.splice(this.index,1);
					for(var i = current_index; i<this.references.grid.rows.length; i++){
						this.references.grid.rows[i].index=i;
						this.references.grid.rows[i].ChangeStyle(this.references.grid.rows[i].references.css_row.subclassName);
					}
				}
				return row;
			}
			return null;
		}
		object.AddRow=function (row,key,beforeRow){
			return this.CreateRow(row,key,beforeRow);
		}
		object.InsertRow=function(values,beforeRow){
			return this.CreateRow(new Jade.Grids.Row(values),'',beforeRow);
		}
		object.NewRow=function(values,key){
			return this.CreateRow(new Jade.Grids.Row(values),key);
		}
		
		object.AddSubItem=function(parent, item, border){
			
			var subitem = document.createElement('div');
			subitem.parent = parent;
			subitem.subitems=[];
			
			if(typeof(border)=="undefined" || border){ subitem.className = 'subitem text-size'; }
			else { subitem.className = 'subitem no-border text-size'; }
			subitem.innerHTML=item;
			parent.references.css_row.parentNode.appendChild(subitem);
			
			Jade.FX.Hide(subitem);
			parent.subitems.push(subitem);
			return subitem;
		}
		object.AddSubObject=function(parent, item){
			var subitem = document.createElement('div');
			subitem.subitems = new Array;
			subitem.className = 'subitem';
			item.Add(subitem);
			Jade.FX.Hide(subitem);
			if(parent.subitems.length==0) {	Jade.DOM.InsertAfter(subitem,parent.references.css_row); }
			else { Jade.DOM.InsertAfter(subitem,parent.subitems[parent.subitems.length-1]); }
		
			parent.subitems.push(subitem);
			subitem.parent = parent;
			return item;
		}
		object.AddSubElement=function(parent, item){
			var subitem = document.createElement('div');
			subitem.subitems = new Array;
			subitem.className = 'subitem text-size';
			subitem.appendChild(item);
			Jade.FX.Hide(subitem);
			
			if(parent.subitems.length==0) {	Jade.DOM.InsertAfter(subitem,parent.references.css_row); }
			else { Jade.DOM.InsertAfter(subitem,parent.subitems[parent.subitems.length-1]); }
		
			parent.subitems.push(subitem);
			subitem.parent = parent;
			return item;
		}		
		object.ClearRows=function(){
			this.selectedKey=null;
			this.selectedIndex=null;
			for(var i=this.rows.length-1; i>=0 ;i--){
				this.rows[i].Delete();
			}
		}
		object.ClearFocus=function(){
			for(var i in this.rows){
				this.rows[i].ClearStyle();
			}
		}
		
		{//Events
			object.onadd=function(){
				this.onresize();
			}
			object.onresize=function(){
				if(this.style.height!=''){
					var height=$.Convert.ToInt(this.style.height)-$.Css.GetHeight(this.head);
					if($.isDefined(this.navigation) && this.navigation.show){ height-=24; }
					this.body.style.height=height+'px';
				}
			}			
			
		}
		{//Functions
			object.Loading=function(value){
				if(value){
					if(!$.isDefined(this.load_layer)){
						this.load_layer=$.DOM.Add(this.border, 'div', 'gr_load_container');
						$.FX.SetOpacity(this.load_layer, 70);
						$.DOM.Add(this.load_layer, 'div', 'gr_loading');
					}
					else{ $.FX.Show(this.load_layer); }
				}
				else if($.isDefined(this.load_layer)){ $.FX.Hide(this.load_layer); }
			}
			object.Pagination=function(value){
				this.pagination=(value>0);
				this.rowsperpage=value;
				this.currentpage=0;
			}			
		}
		{//
			object.Show=function(page){
				if(!$.isDefined(page)){ page=0; }
				this.currentpage=page;
				this.totalpages=$.Convert.RoundUp(this.rows.length/this.rowsperpage,0);
				
				var from = page * this.rowsperpage;
				var to = (page+1) * this.rowsperpage;
				
				if(!this.pagination){ from = 0; }
				if(to>this.rows.length || !this.pagination){ to=this.rows.length; }
				
				if(this.rowsperpage<this.rows.length){
					if(!$.isDefined(this.navigation)){ this.ConstructNavigation(); }
					this.navigation.Show();
				}
				else{
					if($.isDefined(this.navigation)){ this.navigation.Hide(); }
				}
				
				this.ShowNavigation();

				setTimeout(function(){
					object.Refresh();
					for(var i=from; i<to; i++){
						var row = object.rows[i];
						var css = row.references.css_row;
						row.index = i;
						if($.isDefined(css)){
							object.body.appendChild(css);
							css.className = (row.index%2==0) ? 'gr_row' : 'gr_row pair';
							css._className = css.className;
						}
						else{
							object.DrawRow(object.rows[i]);
						}
					}
				}, 0);
			}
			object.ConstructNavigation=function(){
				this.navigation=$.DOM.Add(this.border, 'div', 'gr_navigation');
				this.navigation.style.display='none';
				
				var container=$.DOM.Add(this.navigation, 'div', 'container');
				
				this.navigation.first=$.Grids.NavigationButton(container, 'first');
				this.navigation.previus=$.Grids.NavigationButton(container, 'previus');
				this.navigation.text=$.DOM.Add(container, 'div', 'text-size');
				this.navigation.next=$.Grids.NavigationButton(container, 'next');
				this.navigation.last=$.Grids.NavigationButton(container, 'last');				
				

				this.navigation.first.click=function(){ object.Show(0); }
				this.navigation.previus.click=function(){ object.Show(object.currentpage-1); }
				this.navigation.next.click=function(){ object.Show(object.currentpage+1); }
				this.navigation.last.click=function(){ object.Show(object.totalpages-1); }	
				
				this.navigation.Show=function(){
					this.show=true;
					this.style.display='';
					object.onresize();
					
					this.text.innerHTML=$.Strings.Format($.Lang.Get('page_of'), object.currentpage+1, object.totalpages);
					
					this.first.enable();
					this.previus.enable();
					this.next.enable();
					this.last.enable();
						
				}
				this.navigation.Hide=function(){
					this.show=false;
					this.style.display='none';
				}
			}			
			object.Refresh=function(){
				this.selectedKey=null;
				this.selectedIndex=null;
				var start=$.isDefined(this.load_container)?1:0;
				for(var i=start, iLen=this.body.childNodes.length; i<iLen; i++){
					this.body.removeChild(this.body.childNodes[start]);
				}
			}
			object.ShowNavigation=function(){
				if($.isDefined(this.navigation)){
					this.navigation.first.enable();
					this.navigation.previus.enable();
					this.navigation.next.enable();
					this.navigation.last.enable();
				
					if(this.currentpage==0){
						this.navigation.first.disable(); 
						this.navigation.previus.disable();
					}
					else{
						this.navigation.first.enable();
						this.navigation.previus.enable();
					}
					if(this.currentpage==this.totalpages-1){
						this.navigation.next.disable() 
						this.navigation.last.disable();
					}
				}
			}			
		}
		return object;
	},
	
	NavigationButton: function(parent, className){
		var container = document.createElement('div');
		container.className='button enabled';
		container.onmouseover=function(){ this.className+=' hover'; }
		container.onmouseout=function(){ this.className = this.className.replace(/ hover/,''); }
		container.disable=function() { 
			this.onclick=null; 
			this.className='button disabled'; 
			}
		container.enable=function() {
			this.onclick=this.click;
			this.className='button enabled'; 
			}
		parent.appendChild(container);
		
		var icon = document.createElement('div');
		icon.className = 'icon '+className;
		container.appendChild(icon);
		return container;
	},
	
	ColumnModel: function(values){
		var object={};
		// Propeties
		object.columns=[];
		object.defaultValues = {};

		// Default values
		if(typeof(values)!="undefined")	{
			for(var i in Jade.Grids.ColumnProperties)
			{
				var property = Jade.Grids.ColumnProperties[i][0];
				if(typeof(values[property])!="undefined") { object.defaultValues[property] = values[property]; }
			}
		}
		
		//Function to add a column
		object.Add = function (column) {
			column.index = object.columns.length;
			for(var property in object.defaultValues)
			{
				if(typeof(object.defaultValues[property])!="undefined") { column[property] = object.defaultValues[property]; };
			}
			object.columns[column.index] = column;
			if(typeof(column.key)!="undefined" && column.key!=null) { object.columns[column.key] = object.columns[column.index] };
		}
		return object;
	},
	
	Column: function(object) {
		for(i in Jade.Grids.ColumnProperties)
		{
			var property = Jade.Grids.ColumnProperties[i][0];
			var value = Jade.Grids.ColumnProperties[i][1];
			if(typeof(object[property])=="undefined") { object[property] = value; }
		}
		if(!Jade.Grids._check_event(object,'render')) { object.render = function (cell) { cell.references.css_cell.innerHTML = '<p class="gr_simple text-size" style="text-align: '+object['align']+';">'+cell.value+'</p>'; } }
		if(!Jade.Grids._check_event(object,'editor')) { object.editor =  function (cell) { document.body.removeChild(cell.references.edit_area); } }
		return object;
	},
	
	SimpleColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.render = function(cell){
			var paragraph = document.createElement('p');
			paragraph.className='gr_simple text-size';
			paragraph.style.textAlign = object['align'];
			paragraph.innerHTML = cell.value
			Jade.Events.NoSelect(paragraph)
			cell.references.css_cell.appendChild(paragraph);
		}
		object.type='simple';
		return Jade.Grids.Column(object);
	},
	HideColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type='hide';
		object.render = function (cell) { cell.references.css_cell.innerHTML = ''; }
		object.width = 0;
		return Jade.Grids.Column(object);
	},
	IconColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type='icon';
		object.render = function(cell) {
			cell.references.css_cell.innerHTML = '<div class="gr_icon '+cell.value+'"></div>';
		}
		return Jade.Grids.Column(object);
	},
	SpecialCheckColumn: function(object) {
		if(typeof(object)=="undefined") object= {};
		object.type='specialcheck';
		object.clickable = false;
		object.render = function(cell) {
			cell.references.css_cell.className = 'gr_value special';
			var chk=Jade.Objects.CheckBox();
			chk.Location(3,1,20,18);
			chk.SetCheck(cell.value=='true'?true:false);
			cell.value = chk.GetCheck();
			chk.Add(cell.references.css_cell);
			chk.onchange=function(){
				cell.value=chk.GetCheck();
			}			
			cell.references.object=chk;
		}
		object.editor = function(cell) {
			cell.value = cell.references.css_cell.firstChild.GetCheck();
			document.body.removeChild(cell.references.edit_area);
		}
		return Jade.Grids.Column(object);
	},
	SpecialTreeColumn: function(object) {
		if(typeof(object)=="undefined") object= {};
		object.type='specialtree';
		object.clickable = false;
		object.render = function(cell){
			cell.references.css_cell.className = 'gr_value special';
			cell.references.css_cell.innerHTML = '<div class="expand"></div>';
		}
		
		object.editor = function(cell){
			if(cell.value==1){
				$.events.fire(cell.references.row, 'oncollapse');
				for(var i in cell.references.row.subitems){
					Jade.FX.Hide(cell.references.row.subitems[i]);
				}
				cell.references.css_cell.innerHTML = '<div class="expand"></div>';
				cell.value=0;
			}
			else{
				$.events.fire(cell.references.row, 'onexpand');
				for(var i in cell.references.row.subitems){
					Jade.FX.Show(cell.references.row.subitems[i]);
				}
				cell.references.css_cell.innerHTML = '<div class="collapse"></div>';
				cell.value=1;
			}
			document.body.removeChild(cell.references.edit_area);
		}
		return Jade.Grids.Column(object);
	},
	CheckColumn: function(object) {
		if(typeof(object)=="undefined") object= {};
		object.type='check';
		object.clickable = false;
		object.render = function(cell) {
			var chk = new Jade.Objects.CheckBox();
			chk.Location((parseInt(cell.references.css_cell.style.width)/2)-10,1,20,18);
			chk.SetCheck(cell.value=='true'||cell.value==1?true:false);
			cell.value = chk.GetCheck();
			chk.Add(cell.references.css_cell);

			cell.references.object = chk;
		}
		object.editor = function(cell) {
			cell.value = cell.references.css_cell.firstChild.GetCheck();
			document.body.removeChild(cell.references.edit_area);
		}
		return Jade.Grids.Column(object);
	},
	TextColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type='text';
		object.render = function(cell) {
			cell.references.css_cell.innerHTML = '<p class="gr_textbox text-size">'+(cell.value==''?'&nbsp;':cell.value)+'</p>';
		}
		
		object.editor = function(cell) {
			var input = document.createElement('input');
			input.cell = cell;
			input.type = 'text';
			input.className = 'grid_textbox text-size';
			input.value = cell.value;

			input.onblur = function() { this.end_edit(); }
			
			input.onkeydown = function(event) {
				if (window.event) { event = window.event; }
				if (event.keyCode == 13) { this.end_edit(); }
			}
			
			input.end_edit = function() {
				this.cell.value=this.value;
				this.cell.references.column.render(this.cell);
				document.body.removeChild(this.cell.references.edit_area);
			}
			
			cell.references.edit_area.appendChild(input);
			input.style.width = (cell.references.edit_area.offsetWidth-2-parseInt(Jade.Css.GetStyle(input,'padding-left'))-parseInt(Jade.Css.GetStyle(input,'padding-right'))) + 'px';
			input.style.height = (cell.references.edit_area.offsetHeight-4) + 'px';
			input.focus();
		}
		
		return Jade.Grids.Column(object);
	},
	ButtonColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type='button';
		object.clickable = false;
		object.SetValue = function(cell, value) {
			var command = cell.references.css_cell.firstChild;
			command.value = value;
			if($.isArray(value)){
				command.SetText('<div class="icon grid '+value[1]+'"></div><div class="text">'+value[0]+'</div>');
			}
			else{
				command.SetText(value);
			}
			if(value == '' || value == null) { command.style.display='none' }
			else { command.style.display='block'; }
		}
		
		object.render = function(cell) {
			var command = new Jade.Objects.Button();
			command.Location(0,0,parseInt(cell.references.css_cell.style.width)-1,17);
			command.SetStyle('grid');
			command.SetText(cell.value);
			command.Add(cell.references.css_cell);
			this.SetValue(cell,cell.value);
			command.cell=cell;
		}
		
		return Jade.Grids.Column(object);
	},
	TimeColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type='time';
		object.SetValue = function(cell,value) {
			value = Jade.Strings.PadLeft(value,'0',5);
			var hours = Jade.Strings.PadLeft(parseInt(value / 60),'0',3);
			var minutes = Jade.Strings.PadLeft(value % 60,'0',2);
			var strTime=hours+':'+minutes;
			cell.value = value;
			cell.references.css_cell.innerHTML = '<p class="gr_textbox text-size">'+strTime+'</p>';
		}
		object.render = function(cell) {
			var strTime='000:00';
			if(cell.value!=''){
				var hours = Jade.Strings.PadLeft(parseInt(cell.value / 60),'0',3);
				var minutes = Jade.Strings.PadLeft(cell.value % 60,'0',2);
				strTime=hours+':'+minutes;
			}
			cell.references.css_cell.innerHTML = '<p class="gr_textbox text-size">'+strTime+'</p>';
		}
		object.editor = function(cell) {
			var hours = Jade.Strings.PadLeft(parseInt(cell.value / 60),'0',3);
			var minutes = Jade.Strings.PadLeft(cell.value % 60,'0',2);
			
			var container = document.createElement('div');
			container.cell = cell;
			container.className = 'grid_time';
			container.style.height = (cell.references.edit_area.offsetHeight-2) + 'px';
			cell.references.edit_area.appendChild(container);
			
			var in_hour = document.createElement('input');
			in_hour.cell = cell;
			in_hour.type='text';
			in_hour.className = 'grid_time_hours text-size';
			in_hour.value=hours;
			in_hour.style.background='none';
			in_hour.style.height=(parseInt(container.style.height)-3)+'px';
			in_hour.style.width='23px';
			in_hour.style.position='absolute';
			in_hour.style.top='2px';
			in_hour.style.left='6px';
			in_hour.style.border='none';
			in_hour.style.outline='none';
			container.appendChild(in_hour);
			
			var sep = document.createElement('div');
			sep.className = 'text-size';
			sep.style.height=in_hour.style.height;
			sep.style.position = 'absolute';
			sep.style.top='1px';
			sep.style.left='28px';
			sep.style.padding='2px 0 0 0';
			sep.innerHTML = ':';
			container.appendChild(sep);

			var in_min = document.createElement('input');
			in_min.cell = cell;
			in_min.type='text';
			in_min.className = 'grid_time_minutes text-size';
			in_min.style.background='none';
			in_min.value=minutes;
			in_min.style.height=in_hour.style.height;
			in_min.style.width='18px';
			in_min.style.position='absolute';
			in_min.style.top='2px';
			in_min.style.left='32px';
			in_min.style.border='none';
			in_min.style.outline='none';
			container.appendChild(in_min);
			
			container.hours = in_hour;
			container.minutes = in_min;
			
			in_min.focus();

			in_hour.onfocus = function() { in_hour.got_focus = true; }
			in_min.onfocus = function () { in_min.got_focus = true; }
			
			in_hour.onblur = function() { 
				in_hour.got_focus=false;
				this.value=Jade.Strings.PadLeft(this.value,'0',3);
				setTimeout(function() {	if( !in_min.got_focus && !in_hour.got_focus) { container.end_edit(); } },100);
			}

			in_min.onblur = function() { 
				in_min.got_focus=false;
				this.value=Jade.Strings.PadLeft(this.value,'0',2);
				setTimeout(function() {	if( !in_min.got_focus && !in_hour.got_focus) { container.end_edit(); } },100);
			}
			
			in_hour.onkeydown = function(event) {
				if (window.event) { event = window.event; }
				if (event.keyCode == 13) { this.parentNode.end_edit(); }
			}
			in_hour.onkeyup = function(event) {
				this.value=Jade.Strings.PadLeft(this.value,'0',3);
			}
			in_min.onkeydown = function(event) {
				if (window.event) { event = window.event; }
				if (event.keyCode == 13) { this.parentNode.end_edit(); }
			}
			in_min.onkeyup = function(event) {
				this.value=Jade.Strings.PadLeft(this.value,'0',2);
			}
			container.end_edit = function() {
				this.cell.value=(this.hours.value*60)+parseInt(this.minutes.value);
				this.cell.references.column.render(this.cell);
				document.body.removeChild(this.cell.references.edit_area);
				if(Jade.Grids._check_event(this.cell,'onendedit')) { this.cell.onendedit(); }	
			}
		}
		
		return Jade.Grids.Column(object);
	},
	HourColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type = 'hour';
		object.SetValue = function(cell,value) {
			value = Jade.Strings.PadLeft(value,'0',4);
			var hours = value.substring(0,2);
			var minutes = value.substring(2,4);
			strTime=hours+':'+minutes;
			
			cell.value = value;
			cell.references.css_cell.innerHTML = '<p class="gr_textbox text-size">'+strTime+'</p>'
		}
		object.render = function(cell) {
			var strTime='00:00';
			if(cell.value!=''){
				var hours = cell.value.substring(0,2);
				var minutes = cell.value.substring(2,4);
				strTime=hours+':'+minutes;
			}
			cell.references.css_cell.innerHTML = '<p class="gr_textbox text-size">'+strTime+'</p>'
		}
		object.editor = function(cell) {
			var hours = cell.value.substring(0,2);
			var minutes = cell.value.substring(2,4);
			
			
			var container = $.Objects.TimeBox(false);
			container.className += ' grid_time';
			container.SetValue(cell.value);
			container.cell = cell;
			cell.references.edit_area.appendChild(container);
			container.Size(cell.references.edit_area.offsetWidth-3, cell.references.edit_area.offsetHeight-2);
			container.hours.focus();
			
			container.onkeyenter=function(){ this.end_edit(); }
			container.onkeyescape=function(){ this.end_edit(); }
			
			/*
			var in_hour = document.createElement('input');
			in_hour.cell = cell;
			in_hour.type='text';
			in_hour.className = 'grid_time_hours text-size';
			in_hour.value=hours;
			in_hour.style.background='none';
			in_hour.style.height=(parseInt(container.style.height)-3)+'px';
			in_hour.style.width='18px';
			in_hour.style.position='absolute';
			in_hour.style.top='2px';
			in_hour.style.left='6px';
			in_hour.style.border='none';
			in_hour.style.outline='none';
			container.appendChild(in_hour);
			//in_hour.style.top='8px';
			//in_hour.style.left='6px';
			//in_hour.style.width='18px';
			
			
			var sep = document.createElement('div');
			sep.className = 'text-size';
			sep.style.height=in_hour.style.height;
			sep.style.position = 'absolute';
			sep.style.top='1px';
			sep.style.left='21px';
			sep.style.padding='2px 0 0 0';
			sep.innerHTML = ':';
			container.appendChild(sep);

			var in_min = document.createElement('input');
			in_min.cell = cell;
			in_min.type='text';
			in_min.className = 'grid_time_minutes text-size';
			in_min.style.background='none';
			in_min.value=minutes;
			in_min.style.height=in_hour.style.height;
			in_min.style.width='18px';
			in_min.style.position='absolute';
			in_min.style.top='2px';
			in_min.style.left='25px';
			in_min.style.border='none';
			in_min.style.outline='none';
			container.appendChild(in_min);
			
			container.hours = in_hour;
			container.minutes = in_min;


			in_hour.onkeypress = function(event) {
				if(window.event){ event=windows.event; }
				var keycode=event.keyCode||event.which||0;
				var value=$.Convert.ToInt(this.value);
			
				switch(keycode){
					case 38:
						if(value<23){ value=value+1; }
						break;
					case 40:
						if(value>0){ value=value-1; }
						break;
					case 39:
						in_min.focus();
						break;
					default:
						var key=String.fromCharCode(keycode);
						if(key>=0 && key<=9){
							key=$.Convert.ToInt(key);
							if(value==1){ value=this.value+key; }
							else if(value==2 && key<=3){ value=this.value+key; }
							else{ value=key; }
						}
				}
				this.value=Jade.Strings.PadLeft(value,'0',2);
				return false;
			}
			in_hour.onkeydown = function(event){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_hour.onkeyup = function(event){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_hour.onselect=function(){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_hour.onclick=function(){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_hour.onfocus=function(){ $.DOM.SetCaretSelection(this, 0, this.value.length); this.parentNode.className += ' focus';	}
			in_hour.onblur=function(){ this.parentNode.className=this.parentNode.className.replace(/ focus/,''); }
			
			in_hour.focus();

			in_min.onkeypress = function(event) {
				if(window.event){ event=windows.event; }
				var keycode=event.keyCode||event.which||0;
				var value=$.Convert.ToInt(this.value);
			
				switch(keycode){
					case 38:
						if(value<59){ value=value+1; }
						break;
					case 40:
						if(value>0){ value=value-1; }
						break;
					case 37:
						in_hour.focus();
						break;
					default:
						var key=String.fromCharCode(keycode);
						if(key>=0 && key<=9){
							key=$.Convert.ToInt(key);
							if(value<=5){ value=this.value+key; }
							else{ value=key; }
						}
				}
				this.value=Jade.Strings.PadLeft(value,'0',2);
				return false;
			}
			in_min.onkeydown = function(event){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_min.onkeyup = function(event){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_min.onselect=function(){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_min.onclick=function(){ $.DOM.SetCaretSelection(this, 0, this.value.length); }
			in_min.onfocus=function(){ $.DOM.SetCaretSelection(this, 0, this.value.length); this.parentNode.className += ' focus'; }
			in_min.onblur=function(){ this.parentNode.className=this.parentNode.className.replace(/ focus/,''); }
			
			/*
			in_hour.onfocus = function(){ in_hour.got_focus = true; }
			in_min.onfocus = function (){ in_min.got_focus = true; }

			in_hour.onblur = function(){ 
				in_hour.got_focus=false;
				this.value=Jade.Strings.PadLeft(this.value,'0',2);
				setTimeout(function() {	if( !in_min.got_focus && !in_hour.got_focus) { container.end_edit(); } },100);
			}
			in_hour.onkeydown = function(event){
				if (window.event) { event = window.event; }
				if (event.keyCode == 13) { this.parentNode.end_edit(); }
			}
			
			in_min.onblur = function(){
				in_min.got_focus=false;
				this.value=Jade.Strings.PadLeft(this.value,'0',2);
				setTimeout(function() {	if( !in_min.got_focus && !in_hour.got_focus) { container.end_edit(); } },100);
			}
			in_hour.onkeyup = function(event){
				if(this.value > 24) this.value = '24';
				this.value=Jade.Strings.PadLeft(this.value,'0',2);
			}
			in_min.onkeydown = function(event){
				if (window.event) { event = window.event; }
				if (event.keyCode == 13) { this.parentNode.end_edit(); }
			}
			in_min.onkeyup = function(event){
				if(this.value > 59) this.value = '59';
				this.value=Jade.Strings.PadLeft(this.value,'0',2);
			}
			*/
			container.onblur = function(){ container.end_edit(); }
			container.end_edit = function(){
				this.cell.value=this.GetValue();
				this.cell.references.column.render(this.cell);
				document.body.removeChild(this.cell.references.edit_area);
				if($.Grids._check_event(this.cell,'onendedit')){ this.cell.onendedit(); }
			}
		}
		return Jade.Grids.Column(object);
	},
	ComboColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type='combo';
		object.render = function(cell) {
			var dt_input = new Jade.Objects.ComboBox();
			dt_input.SetStyle('grid');
			dt_input.inside_container=true;
			dt_input.Location(0,0,parseInt(cell.references.css_cell.style.width)+1,19);
			dt_input.onchangekey = function() {
				if(Jade.Grids._check_event(this.cell,'onchangekey')) { this.cell.onchangekey(); }	
			}
			dt_input.onchange = function() {
				this.cell.value = this.value;
			}
			dt_input.onclick = function() {
				this.expand(); 
			}
			dt_input.onexpand = function() {
				if(Jade.Grids._check_event(this.cell,'onbeginedit')) { this.cell.onbeginedit(); }				
			}
			dt_input.Add(cell.references.css_cell);
			dt_input.cell = cell;
			cell.references.object = dt_input;
		}
		object.editor = function(cell) {
			document.body.removeChild(cell.references.edit_area);
		}
		return Jade.Grids.Column(object);
	},
	DateColumn: function(object) {
		if(typeof(object)=="undefined") object = {};
		object.type='date';
		object.render = function(cell) {
			var dt_input = new Jade.Objects.DatePicker();
			dt_input.SetStyle('grid');
			dt_input.SetValue(cell.value);
			dt_input.Location(1,0,parseInt(cell.references.css_cell.style.width)-1,19);
			dt_input.onchangevalue = function() { this.cell.value = this.value;	}
			dt_input.Add(cell.references.css_cell);
			dt_input.cell = cell;
		}
		object.editor = function(cell) {
			document.body.removeChild(cell.references.edit_area);
		}
		return Jade.Grids.Column(object);
	},
	Row: function(values) {
		this.cells = new Array;
		if( typeof(values)!="undefined" && values.constructor == Array )
		{
			for(var i in values)
			{
				var new_cell = {}
				new_cell.value = values[i];
				new_cell.index = i;
				this.cells.push(new_cell);
			}
		}
		this.Parse = function (columnmodel) {
			var tmpCells = new Array;
			for(var i=0; i < columnmodel.columns.length; i++) {
				if(typeof(this.cells[i])!="undefined") { tmpCells.push(this.cells[i]); } 
				else {
					var new_cell = {};
					new_cell.value ='';
					new_cell.index=i;
					tmpCells.push(new_cell);
				}
				
			}
			this.cells = tmpCells;
		}
	},
	
	ColumnProperties: [['type',null],['key',null], ['text',''], ['width',100], ['sortable',true], ['editable',true],['fixedfocus', false], ['align','left'], ['selectable',true], ['clickable',true], ['visible',true]]

}
Jade.LoadLibrary('Grids',JadeGrids);