/***
 * MooRainbowLight
 *
 * @version		1.2b2-gw (Unofficial version for MooTools 1.2 by Geoffray Warnants < gwarnants [at] gmail.com >)
 * @license		MIT-style license
 * @author		Djamil Legato (w00fz) - < w00fzIT [at] gmail.com >
 * @infos		http://moorainbow.woolly-sheep.net
 * @copyright	Author
 * 
 *
 */

var MooRainbowLight = new Class({
	Implements: [Options, Events],
	options: {
		id: 'mooRainbow',
		prefix: 'moor-',
		onComplete: Class.empty,
		onChange: Class.empty,
		color : null,
		colors : [
		"#FF8080","#FFFF80","#80FF80","#00FF80","#80FFFF","#33CED5","#FF80C0","#FF80FF",
		"#EB5170","#FFFF00","#80FF00","#00FF40","#6CC0DC","#8080FF","#FF7282","#FF00FF",
		"#FF0000","#FF9900","#99CC00","#0BCF00","#0080FF","#0080C0","#D62956","#FF0080",
		"#800000","#FF8000","#808000","#008000","#0000FF","#426E81","#993366","#8000FF",
		"#400000","#804000","#004000","#004040","#000080","#004080","#800080","#400080",
		"#000000","#333333","#5B5249","#666666","#808080","#7F8089","#CCCCCC","#FFFFFF"]
	},
	
	initialize: function(el, options) {
		this.element = el; if (!this.element) return;
		this.setOptions(options);
		
		this.sliderPos = 0;
		this.pickerPos = {x: 0, y: 0};
		this.color = this.options.color;
		this.colors = this.options.colors;
		this.id = this.options.id;
		this.sets = [];
		this.pickerClick = this.sliderClick  = false;
		if (!this.layout) this.doLayout();
		this.element.addEvent('click', function(e) { 
			e.stop(); 
			this.toggle(e);
		}.bind(this));
		this.visible = false;
		
		$(this.id).getElements("a.color").each(function(elt){
			elt.addEvent('click',function(e){
				e.stop();
				
				$(this.id).getElements("a.selected").removeClass('selected');
				elt.addClass('selected');
				
				xpression = new RegExp(this.id+"-(.*)");
		    	res = xpression.exec(elt.id);
		    	if (res && res[1]){
		    		this.sets = [this.HexToR(res[1]),this.HexToG(res[1]),this.HexToB(res[1])];
					this.fireEvent('onComplete', [this.sets, this]);
					this.hide();
		    	}
			}.bind(this));
		}.bind(this));
		
		if (Browser.Engine.webkit) this.hide();
	},
	
	HexToR: function (h) {
		return parseInt((this.cutHex(h)).substring(0,2),16)
	},
	HexToG: function (h) {
		return parseInt((this.cutHex(h)).substring(2,4),16)
	},
	HexToB: function (h) {
		return parseInt((this.cutHex(h)).substring(4,6),16)
	},
	cutHex: function (h) {
		return (h.charAt(0)=="#") ? h.substring(1,7):h
	},
	
	toggle: function() {
		if (this.layout.getStyle('display')=='block'){
			this.hide();
		} else {
			this.show();
		}
	},
	
	show: function() {
		this.rePosition();
		this.layout.setStyles({'display': 'block', 'position': 'absolute'});
		this.visible = true;
	},
	
	hide: function() {
		this.layout.setStyles({'display': 'none'});
		this.visible = false;
	},
	
	doLayout: function() {
		var id = this.options.id, prefix = this.options.prefix;
		var idPrefix = id + ' .' + prefix;

		this.layout = new Element('div', {
			'class' : 'colorPicker',
			'styles': {'display': 'block', 'position': 'absolute'},
			'id': id
		}).inject(document.body);

		var total = this.colors.length;
		
		this.tableColor = new Element('table',{'cellpadding':0, 'cellspacing':2}).injectInside(this.layout);
		this.tbody = new Element('tbody').injectInside(this.tableColor);
		
		this.arrTrColor = new Elements([new Element('tr').injectInside(this.tbody),
		new Element('tr').injectInside(this.tbody),
		new Element('tr').injectInside(this.tbody),
		new Element('tr').injectInside(this.tbody),
		new Element('tr').injectInside(this.tbody),
		new Element('tr').injectInside(this.tbody)]);
		
		this.arrTrColor.each(function (tr, index){
			var cpt = index*8;
			for (var i=cpt; i<(cpt+8); i++) {
				var tdColor = new Element('td',{
					'class' : 'color'
				}).injectInside(tr);
				
				var aColor = new Element('a',{
					'href': '#',
					'class' : 'color',
					'style': "background-color:"+this.colors[i],
					'id' :  this.id+'-'+this.colors[i],
					'text' : 'a'
				}).injectInside(tdColor);
				
				if (this.colors[i] == this.color){
					aColor.addClass('selected');
				}
				
			}
		}.bind(this));
		 
		this.rePosition();
		
		if (!Browser.Engine.webkit419) this.hide();
	},
	rePosition: function() {
		var coords = this.element.getCoordinates();
		this.layout.setStyles({
			'left': coords.left,
			'top': coords.top + coords.height + 1
		});
	}
});
