var HtmlTable=new Class({Implements:[Options,Events,Class.Occlude],options:{properties:{cellpadding:0,cellspacing:0,border:0},rows:[],headers:[],footers:[]},property:"HtmlTable",initialize:function(){var params=Array.link(arguments,{options:Type.isObject,table:Type.isElement,id:Type.isString});this.setOptions(params.options);if(!params.table&&params.id){params.table=document.id(params.id)}this.element=params.table||new Element("table",this.options.properties);if(this.occlude()){return this.occluded}this.build()},build:function(){this.element.store("HtmlTable",this);this.body=document.id(this.element.tBodies[0])||new Element("tbody").inject(this.element);$$(this.body.rows);if(this.options.headers.length){this.setHeaders(this.options.headers)}else{this.thead=document.id(this.element.tHead)}if(this.thead){this.head=this.getHead()}if(this.options.footers.length){this.setFooters(this.options.footers)}this.tfoot=document.id(this.element.tFoot);if(this.tfoot){this.foot=document.id(this.tfoot.rows[0])}this.options.rows.each(function(row){this.push(row)},this)},toElement:function(){return this.element},empty:function(){this.body.empty();return this},set:function(what,items){var target=(what=="headers")?"tHead":"tFoot",lower=target.toLowerCase();this[lower]=(document.id(this.element[target])||new Element(lower).inject(this.element,"top")).empty();var data=this.push(items,{},this[lower],what=="headers"?"th":"td");if(what=="headers"){this.head=this.getHead()}else{this.foot=this.getHead()}return data},getHead:function(){var rows=this.thead.rows;return rows.length>1?$$(rows):rows.length?document.id(rows[0]):false},setHeaders:function(headers){this.set("headers",headers);return this},setFooters:function(footers){this.set("footers",footers);return this},update:function(tr,row,tag){var tds=tr.getChildren(tag||"td"),last=tds.length-1;row.each(function(data,index){var td=tds[index]||new Element(tag||"td").inject(tr),content=(data?data.content:"")||data,type=typeOf(content);if(data&&data.properties){td.set(data.properties)}if(/(element(s?)|array|collection)/.test(type)){td.empty().adopt(content)}else{td.set("html",content)}if(index>last){tds.push(td)}else{tds[index]=td}});return{tr:tr,tds:tds}},push:function(row,rowProperties,target,tag,where){if(typeOf(row)=="element"&&row.get("tag")=="tr"){row.inject(target||this.body,where);return{tr:row,tds:row.getChildren("td")}}return this.update(new Element("tr",rowProperties).inject(target||this.body,where),row,tag)},pushMany:function(rows,rowProperties,target,tag,where){return rows.map(function(row){return this.push(row,rowProperties,target,tag,where)},this)}});["adopt","inject","wraps","grab","replaces","dispose"].each(function(method){HtmlTable.implement(method,function(){this.element[method].apply(this.element,arguments);return this})});