/*
	Author: Riccardo Degni, <http://www.riccardodegni.it/>
	
	License: GNU GPL License
	
	Filename: moo.rd_table.js
	
	Class: Table

*/

Table = new Class({
		
				initialize: function(element) {
					this.element = $(element);
				},
		
				zebra: function(color1, color2, firstLine) {
					if(this.element.getTag() != 'table')  return false;
					
					this.cells = this.element.getElements('tr');
					
					this.cells.each(function(cell, index) {
			
						if(index%2 == 0)
							if(firstLine && index == 0) cell.setStyles(firstLine);
							else cell.setStyle('background-color', color1);
						else
							cell.setStyle('background-color', color2);

					});
						
				},
				
				overClickRows: function(color, clickcolor) {
					this.rows = this.element.getElements('tr');
					
					this.rows.each(function(row, index) {
						row.addEvent('mouseover', function() {
							if(!this.init) this.init = row.getStyle('background-color');
							if(this.getStyle('background-color') != clickcolor)
								row.setStyle('background-color', color);
						});
						
						row.addEvent('mouseout', function() {
							if(this.getStyle('background-color') != clickcolor)
								row.setStyle('background-color', this.init);
						});
						
						row.addEvent('click', function() {
							if(this.getStyle('background-color') != clickcolor)
								this.setStyle('background-color', clickcolor);
							else
								this.setStyle('background-color', this.init);
						});
					});
				},
				
				overRows: function(color) {
					this.rows = this.element.getElements('tr');
					
					this.rows.each(function(row, index) {
						row.addEvent('mouseover', function() {
							this.init = row.getStyle('background-color');
							row.setStyle('background-color', color);
						});
						
						row.addEvent('mouseout', function() {
							row.setStyle('background-color', this.init);
						});
					});
				},
				
				clickRows: function(color) {
					this.rows = this.element.getElements('tr');
					this.rows.each(function(row, index) {
					
						row.addEvent('click', function() {
							if(!this.initBc) this.initBc = row.getStyle('background-color');
							if(row.getStyle('background-color') != this.initBc)
								row.setStyle('background-color', this.initBc);
							else
								row.setStyle('background-color', color);	
						});
					});
				},
				
				overClickCells: function(color, clickcolor) {
					this.rows = this.element.getElements('tr');
					this.rows.each(function(row, index) {
						row.getElements('td').each(function(td,index) {
							td.addEvent('mouseover', function() {
								if(!this.init) this.init = td.getStyle('background-color');
								if(this.getStyle('background-color') != clickcolor)
									td.setStyle('background-color', color);
							});
							
							td.addEvent('mouseout', function() {
								if(this.getStyle('background-color') != clickcolor)
									td.setStyle('background-color', this.init);
							});
							
							td.addEvent('click', function() {
								if(this.getStyle('background-color') != clickcolor)
									this.setStyle('background-color', clickcolor);
								else
									this.setStyle('background-color', this.init);
							});
						});
					});
				},
				
				overCells: function(color) {
					this.rows = this.element.getElements('tr');
					this.rows.each(function(row, index) {
						row.getElements('td').each(function(td,index) {
							td.addEvent('mouseover', function() {
								this.init = td.getStyle('background-color');
								td.setStyle('background-color', color);
							});
							
							td.addEvent('mouseout', function() {
								td.setStyle('background-color', this.init);
							});
						});
					});
				},
				
				clickCells: function(color) {
					this.rows = this.element.getElements('tr');
					this.rows.each(function(row, index) {
						row.getElements('td').each(function(td,index) {
							td.addEvent('click', function() {
								if(!this.initC) this.initC = row.getStyle('background-color');
								if(td.getStyle('background-color') != this.initC)
									td.setStyle('background-color', this.initC);
								else
									td.setStyle('background-color', color);
							});
						});
					});
				}	
				
});