/*
	Filename: moo.rd - A lightweight Mootools extension
	
	Author: Riccardo Degni, <http://www.riccardodegni.it/> and the moo.rd Team
	
	License: GNU GPL License
	
	Copyright: copyright 2007 Riccardo Degni
	
	[Credits]
		[li] moo.rd is based on the MooTools framework <http://mootools.net/>, and uses the MooTools syntax
		[li] moo.rd constructors extends some of the MooTools Classes
		[li] moo.rd Documentation is written by Riccardo Degni
	[/Credits]
*/

var Moo = {};

Moo.Rd = {
	version: '1.3.2',
	author: 'Riccardo Degni',
	members: [
		'Cristiano Fino',
		'Moocha'
	]
};

/*
	Filename: constructors.js
	
	[Description] 
		Contains some of the moo.rd native Constructors based on the MooTools Class. It permits a major modularity.
	[/Description]
	
	Contains: Class Table, Class Make
*/
/*
	Class: Table
	Description: Allows you to customize tables, tables rows, cells and columns 
*/
var Table = new Class({	
	initialize: function(element) {
		this.element = $(element);
		//var _class = this;
		this.rows = this.element.getElements('tr');
		this.cells = this.element.getElements('tr').getElements('td');
		this.offcolor = "";
		this.overcolor = "";
		this.clickcolor = "";
	}
});

/*
	Class: Make
	Description: Wrapper to create Classes that make dinamically Elements.  
*/
var Make = new Class({
	Implements: [Options],
	options: {
		content: 'text'
	}
});

/*
	Filename: table_rows.js
	
	[Description]
		Extends the Table Class adding the capability to customize the the table rows
	[/Description]
	
	Contains: Class Table
	
	Requires: constructors.js
	
	[Summary]
		Table ::: Custom Class to customize tables
	[/Summary]
*/
/*
	Class: Table
	
	[Description]  
		Contains methods that allows you to customize table rows.
		You can also set the mouseover and click events for the table rows: the table datas will result more accessible.
	[/Description]
	
	[Note]
 		The strings represent the colors you pass to each Table method MUST BE UPPERCASED.
	[/Note]
	
	Extends: Class Table
	
	Constructor: new Table (element)
	
	[Properties] 
		element - the element. Must be a table element
	[/Properties]
	
	[Methods]
		zebra -- sets two alternate colors to the table rows
		overClickRows -- changes the background-color of the table rows on mouseover and click events
		overRows -- changes the background-color of the table rows on mouseover event
		clickRows -- changes the background-color of the table rows on click event
	[/Methods]
*/
Table.implement({

	/*
	Method: zebra
	Description: sets two alternate colors to the table rows
	[Arguments]
		color1 :: one of the two color of the zebra
		color2 :: one of the two color of the zebra
		firstLine :: optional. if true the first line won't be styled
	[/Arguments]
	[Example]  
		>  var tb = new Table('tb');
		>  tb.zebra('#CCCCCC','#666666');
		>  // also styling the first line
		>  tb.zebra('#CCCCCC','#666666', true);
	[/Example]
	*/
	zebra: function(color1, color2, firstLine) {
		if(this.element.get('tag') != 'table')  return false;
		
		this.cells.each(function(cell, index) {
			if(firstLine && index == 0)	return;		
			(index%2 == 0) ? cell.setStyle('background-color', color1) : cell.setStyle('background-color', color2);
			/*if(index%2 == 0)
				cell.setStyle('background-color', color1);
			else
				cell.setStyle('background-color', color2);*/
		});
	},
	
	
	get_row_clicked : function (){
		clickcolor = this.clickcolor;
		
		ligne_selected = '';
		this.rows.each(function(row, index) {
			if(row.getElement('td').getStyle('background-color').toUpperCase() == clickcolor){
				ligne_selected = row.getProperty('id');
			}
		});
		return ligne_selected;
	},
	
	click_row : function (id){
		clickcolor = this.clickcolor;
		
		this.rows.each(function(row, index){
			if(row.getProperty('id')==id){
				row.getElements('td').setStyle('background-color', clickcolor);
			}
		});
	},	
		
	/*
	Method: overClickRows
	Description: changes the background-color of the table rows on mouseover and click events
	[Arguments]
		overcolor :: the color that the rows take on mouseover event
		clickcolor :: the color that the rows take on click event
		oneLineClick :: une seule ligne est active à la fois
		handle :: le clic ne se fait pas sur la ligne mais sur un bouton
	[/Arguments]
	[Example]  
		>  tb.overClickRows('blue', 'green');
	[/Example]
	*/
	overClickRows: function( offcolor, overcolor, clickcolor, oneLineClick, handle,all_ligne) {
		this.offcolor = offcolor;
		this.overcolor = overcolor;
		this.clickcolor = clickcolor;
		var _class = this;

		this.rows.each(function(row, index) {
			if(all_ligne!=1 && index == 0 && navigator.appName=="Microsoft Internet Explorer")	return;
			if(row.hasClass('norow')==true) return;
											
			row.addEvent('mouseover', function() {
				if(!this.init) this.init = row.getElement('td').getStyle('background-color');
				if(row.getElement('td').getStyle('background-color').toUpperCase() != clickcolor)
					row.getElements('td').setStyle('background-color', overcolor);
			});
			
			row.addEvent('mouseout', function() {
				if(row.getElement('td').getStyle('background-color').toUpperCase() != clickcolor)
					row.getElements('td').setStyle('background-color', offcolor);
			});
			
			qui = row;
			if(handle!=undefined && row.getElement('.'+handle))qui = row.getElement('.'+handle);
			
			qui.addEvent('click', function() {
				if(oneLineClick==true){
					_class.rows.each(function(row1, index) {
						if(all_ligne!=1 && index == 0 && navigator.appName=="Microsoft Internet Explorer")	return;
						row1.getElements('td').setStyle('background-color', offcolor);
					});
				}
										   
				if(row.getElement('td').getStyle('background-color').toUpperCase() != clickcolor)
					row.getElements('td').setStyle('background-color', clickcolor);
				else
					row.getElements('td').setStyle('background-color', offcolor);

			});

		});
	},
	
	/*
	Method: overRows
	Description:  changes the background-color of the table rows on mouseover event
	[Arguments]
		overcolor :: the color that the rows take on mouseover event
	[/Arguments]
	[Example]  
		>  tb.overRows('#0066FF');
	[/Example]
	*/
	overRows: function(color) {
		this.rows.each(function(row, index) {
			row.addEvent('mouseover', function() {
				this.init = row.getElement('td').getStyle('background-color');
				row.getElements('td').setStyle('background-color', color);
			});
			
			row.addEvent('mouseout', function() {
				row.getElements('td').setStyle('background-color', this.init);
			});
		});
	},
	
	/*
	Method: clickRows
	Description: changes the background-color of the table rows on click event
	[Arguments]
		clickcolor :: the color that the rows take on click event
	[/Arguments]
	[Example]  
		>  tb.clickRows('#0066FF');
	[/Example]
	*/
	clickRows: function(color) {
		this.rows.each(function(row, index) {
			row.addEvent('click', function() {
				if(!this.initBc) this.initBc = row.getElement('td').getStyle('background-color').toUpperCase();
				if(row.getElement('td').getStyle('background-color').toUpperCase() != this.initBc)
					row.getElements('td').setStyle('background-color', this.initBc);
				else
					row.getElements('td').setStyle('background-color', color);	
			});
		});
	}
});

/*
	Filename: table_cells.js
	
	[Description]
		Extends the Table Class adding the capability to customize the the table cells
	[/Description]
	
	Contains: Class Table
	
	Requires: constructors.js
	
	[Summary]
		Table ::: Custom Class to customize tables
	[/Summary]
*/
/*
	Class: Table
	
	[Description]  
		Contains methods that allows you to customize table cells.
	[/Description]
	
	[Note]
 		The strings represent the colors you pass to each Table method MUST BE UPPERCASED.
	[/Note]
	
	Extends: Class Table
	
	Constructor: new Table (element)
	
	[Properties] 
		element - the element. Must be a table element
	[/Properties]
	
	[Methods]
		zebraCells -- sets two alternate colors to the table cells
		overClickCells -- changes the background-color of the table cells on mouseover and click events
		overCells -- changes the background-color of the table cells on mouseover event
		clickCells -- changes the background-color of the table cells on click event
	[/Methods]
*/
Table.implement({

	/*
	Method: add_classe
	Description: joe permet de mettre des classes dans toutes les cellules d'un tableau
	*/
	add_class: function() {
		if(this.element.getChildren('tbody')[0] && this.element.getChildren('tbody')[0].getChildren('tr')) {
			trows = this.element.getChildren('tbody')[0].getChildren('tr');
			nb_row = trows.length;
			
			trows.each(function(row, index) {
								
				// on ajoute les classes de lignes
				row.addClass('Tlig_'+index); // base sur les numéro
				(index%2 == 0) ? row.addClass('Tlig_a') : row.addClass('Tlig_b'); // une ligne sur deux
				if(index==nb_row-1)row.addClass('Tlig_end'); // dernière ligne
				
				// on parcours les celleules de cette lignes
				nb_cel = row.getChildren('td').length;
	
				row.getChildren('td').each(function(td, i) {
					// on ajoute les classes de colonnes
					td.addClass('Tcol_'+i);
					(i%2 == 0) ? td.addClass('Tcol_a') : td.addClass('Tcol_b'); // une colonne sur deux
					if(i==nb_cel-1)td.addClass('Tcol_end'); // derniere colonne
				});
			});
		}
	},

	/*
	Method: zebraCells
	Description: sets two alternate colors to the table cells
	[Arguments]
		color1 :: one of the two color of the zebra
		color2 :: one of the two color of the zebra
		firstLine :: optional. if true the first line won't be styled
	[/Arguments]
	[Example]  
		>  // doesn't style the first line
		>  tb.zebraCells('#C17878', '#7389AE', true);
		>  // styles the first line too
		>  tb.zebraCells('#C17878', '#7389AE');
	[/Example]
	*/
	zebraCells: function(color1, color2, firstLine) {
		this.rows.each(function(row, index) {
			if(firstLine && index==0) return;
			row.getElements('td').each(function(td, i) {
				if(i%2 == 0)
					(index%2 == 0) ? td.setStyle('background-color', color1) : td.setStyle('background-color', color2);
				else
					(index%2 == 0) ? td.setStyle('background-color', color2) : td.setStyle('background-color', color1);
			});
		});
	},
	
	/*
	Method: overClickCells
	Description: changes the background-color of the table cells on mouseover and click events
	[Arguments]
		overcolor :: the color that the cells take on mouseover event
		clickcolor :: the color that the cells take on click event
	[/Arguments]
	[Example]  
		>  tb.overClickCells('blue', 'green');
	[/Example]
	*/
	overClickCells: function(color, clickcolor) {
		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(td.getStyle('background-color').toUpperCase() != clickcolor)
						td.setStyle('background-color', color);
				});
				
				td.addEvent('mouseout', function() {
					if(td.getStyle('background-color').toUpperCase() != clickcolor)
						td.setStyle('background-color', this.init);
				});
				
				td.addEvent('click', function() {
					if(td.getStyle('background-color').toUpperCase() != clickcolor)
						td.setStyle('background-color', clickcolor);
					else
						td.setStyle('background-color', this.init);
				});
			});
		});
	},
	
	/*
	Method: overCells
	Description:  changes the background-color of the table cells on mouseover event
	[Arguments]
		overcolor :: the color that the cells take on mouseover event
	[/Arguments]
	[Example]  
		>  tb.overCells('#0066FF');
	[/Example]
	*/
	overCells: function(color) {
		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);
				});
			});
		});
	},
	
	/*
	Method: clickCells
	Description: changes the background-color of the table cells on click event
	[Arguments]
		clickcolor :: the color that the cells take on click event
	[/Arguments]
	[Example]  
		>  tb.clickCells('#0066FF');
	[/Example]
	*/
	clickCells: function(color) {
		this.rows.each(function(row, index) {
			row.getElements('td').each(function(td,index) {
				td.addEvent('click', function() {
					if(!this.initC) this.initC = row.getElement('td').getStyle('background-color').toUpperCase();
					if(td.getStyle('background-color').toUpperCase() != this.initC)
						td.setStyle('background-color', this.initC);
					else
						td.setStyle('background-color', color);
				});
			});
		});
	}
});

/*
	Filename: table_cols.js
	
	[Description]
		Extends the Table Class adding the capability to customize the the table columns
	[/Description]
	
	Contains: Class Table
	
	Requires: constructors.js
	
	[Summary]
		Table ::: Custom Class to customize tables
	[/Summary]
*/
/*
	Class: Table
	
	[Description]  
		Contains methods that allows you to customize table columns.
	[/Description]
	
	[Note]
 		The strings represent the colors you pass to each Table method MUST BE UPPERCASED.
	[/Note]
	
	Extends: Class Table
	
	Constructor: new Table (element)
	
	[Properties] 
		element - the element. Must be a table element
	[/Properties]
	
	[Methods]
		zebraCols -- sets two alternate colors to the table columns
		overClickCols -- changes the background-color of the table columns on mouseover and click events
		overCols -- changes the background-color of the table columns on mouseover event
		clickCols -- changes the background-color of the table columns on click event
	[/Methods]
*/
Table.implement({	
	
	/*
	Method: zebraCols
	Description: sets two alternate colors to the table columns
	[Arguments]
		color1 :: one of the two color of the zebra
		color2 :: one of the two color of the zebra
		firstLine :: if true the first line won't be styled
	[/Arguments]
	[Example]  
		>  // doesn't style the first line
		>  tb.zebraCols('#C17878', '#7389AE', true);
		>  // styles the first line too
		>  tb.zebraCols('#C17878', '#7389AE');
	[/Example]
	*/
	zebraCols: function(color1, color2, firstLine) {
		this.rows.each(function(row, index) {
			if(firstLine && index==0) return;
			row.getElements('td').each(function(td, i) {
				if(i%2 == 0)
					td.setStyle('background-color', color1);
				else
					td.setStyle('background-color', color2);
			});
		});
	},
				
	/*
	Method: overClickCols
	Description: changes the background-color of the table columns on mouseover and click events
	[Arguments]
		overcolor :: the color that the columns take on mouseover event
		clickcolor :: the color that the columns take on click event
		firstLine :: boolean. If true the first line wont'be styled
	[/Arguments]
	[Example]  
		>  tb.overClickCols('blue', 'green', true);
	[/Example]
	*/
	overClickCols: function(color, clickcolor, firstLine) {
		this.colors = [];
		this.rows.each(function(row, indexRow) {
			
		  row.getElements('td').each(function(td, indexTd) {
			 
			var i = indexTd;
			
			if(!this.colors[indexRow]) { 
				this.colors[indexRow] = []; 
			}
			this.colors[indexRow][indexTd] = td.getStyle('background-color').toUpperCase();
			td.store('color', this.colors[indexRow][indexTd]);
			
			
			td.addEvent('mouseover', function() {
				td.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if((firstLine) && (indexR==0))
								c.setStyle('background-color', c.retrieve('color'));
							else if(c.getStyle('background-color').toUpperCase() != clickcolor)
								c.setStyle('background-color', color);
							else
								c.setStyle('background-color', clickcolor);
					}, this);
				}, this);
			}.bind(this));
			
			td.addEvent('mouseout', function() {
				td.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if(c.getStyle('background-color').toUpperCase() == color)
								c.setStyle('background-color', c.retrieve('color'));
					}, this);
				}, this);
			}.bind(this));
				
			td.addEvent('click', function() {
				td.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if((firstLine) && (indexR!=0))
							  if(c.getStyle('background-color').toUpperCase() != clickcolor)
								  c.setStyle('background-color', clickcolor);
							  else
								  c.setStyle('background-color', c.retrieve('color'));
							else if((!firstLine))
								 if(c.getStyle('background-color').toUpperCase() != clickcolor)
								  c.setStyle('background-color', clickcolor);
							  else
								  c.setStyle('background-color', c.retrieve('color'));
					}, this);
				}, this);
			}.bind(this));
				
		  }, this);		
		}, this);
	},
	
	/*
	Method: overCols
	Description:  changes the background-color of the table columns on mouseover event
	[Arguments]
		overcolor :: the color that the columns take on mouseover event
		firstLine :: boolean. If true the first line wont'be styled
	[/Arguments]
	[Example]  
		>  tb.overCols('#0066FF', true);
	[/Example]
	*/
	overCols: function(color, firstLine) {
		this.colors = [];
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, indexRow) {
			
			  row.getElements('td').each(function(td, indexTd) {
				 
				var i = indexTd;
				if(!this.colors[indexRow]) { 
					this.colors[indexRow] = []; 
				}
				this.colors[indexRow][indexTd] = td.getStyle('background-color').toUpperCase();
				td.store('color', this.colors[indexRow][indexTd]);
				
				td.addEvent('mouseover', function() {
					td.getParent().getParent().getElements('tr').each(function(r, indexR) {
						r.getElements('td').each(function(c, indexC) {
							if(indexC == i)
								if((firstLine) && (indexR==0))
									c.setStyle('background-color', c.retrieve('color'));
								else if(c.getStyle('background-color').toUpperCase() != color)
									c.setStyle('background-color', color);
								else
									c.setStyle('background-color', c.retrieve('color'));
						}, this);
					}, this);
				}.bind(this));
				
				td.addEvent('mouseout', function() {
					td.getParent().getParent().getElements('tr').each(function(r, indexR) {
						r.getElements('td').each(function(c, indexC) {
							if(indexC == i)
								if(c.getStyle('background-color').toUpperCase() == color)
									c.setStyle('background-color', c.retrieve('color'));
						}, this);
					}, this);
				}.bind(this));
			  
			}, this);		
		}, this);
	},
	
	/*
	Method: clickCols
	Description:  changes the background-color of the table columns on click event
	[Arguments]
		clickcolor :: the color that the columns take on click event
		firstLine :: boolean. If true the first line wont'be styled
	[/Arguments]
	[Example]  
		>  tb.clickCols('#0066FF', true);
	[/Example]
	*/
	clickCols: function(color, firstLine) {
		this.colors = [];
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, indexRow) {
			
		  row.getElements('td').each(function(td, indexTd) {
			 
			var i = indexTd;
			if(!this.colors[indexRow]) { 
				this.colors[indexRow] = []; 
			}
			this.colors[indexRow][indexTd] = td.getStyle('background-color').toUpperCase();
			td.store('color', this.colors[indexRow][indexTd]);
			
			td.addEvent('click', function() {
				td.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if((firstLine) && (indexR==0))
								c.setStyle('background-color', c.retrieve('color'));
							else if(c.getStyle('background-color').toUpperCase() != color)
								c.setStyle('background-color', color);
							else
								c.setStyle('background-color', c.retrieve('color'));
					}, this);
				}, this);
			}.bind(this));
		  
		  }, this);		
		}, this);
	}
});
