var MultiCell = Class.create({
  initialize: function(el) {
		this.element = $(el);
		this.row_count = 1;
		this.height = this.element.down('textarea').rows;
		
		var link = new Element('a', {'href':'javascript:void(0)'}).update('<img src="/include/img/icons/add.png" border="0" /> ADD').observe('click', this.addRow.bind(this));
		this.element.insert({after: link});
  },

	addRow: function(e){
		var row = new Element('tr');
		
		
		this.element.firstDescendant().select("th").each(function(s, cell_index){
			row.insert(new Element('td').insert(new Element('textarea', {'rows':this.height, 'name':this.element.id+"["+this.row_count+"]["+cell_index+"]"})));
		}, this);
		
		this.element.down('tbody').insert(row);
		this.row_count = this.row_count+1;
	}
});




document.observe('dom:loaded', function(s){
	$$('.multicell').each(function(s){
		new MultiCell(s);
	});
});
