/**
* ColorPicker
* 
* Treemotion Produkt zum auswaehlen einer schoenen Farbe
* 
*/

// globale filer-Instanz fuer gesicherten Zugriff aus dem filer-Fenster
var colorPickerInstance;

var ColorPicker = new Object();
Object.extend(ColorPicker, {
	
	format: "hex" // im mom nur hex...
,	type: "div" // oder "pop"
,	currentShowId: null
,	currentInputId: null
,	callbacks: new Hash()
,	opened: []
,	isOver: false	
,	inte: null
,	register: function(showid, inputid, palette, std, callback ) {
		var col = "#000000";
		if( $( inputid ).value != "" )
			col = $( inputid ).value;
		else if( typeof std != "undefined" )
			col = std;
		$( inputid ).value = col;
		Element.setStyle(showid, {
			backgroundColor: col
			,	cursor: "pointer"
		});
		Element.hide($(inputid));
		Element.observe($(showid), "click", function() {
			ColorPicker.showPicker(showid, inputid, palette);
		});
		if (typeof callback == 'function') {
			ColorPicker.callbacks.set(inputid, callback);
		}
	}
	
,	showPicker: function(showid, inputid, palette) {
		//this.inte = setInterval( "ColorPicker.closeByInte()", 2000 );
		params = "";
		if (typeof palette != "undefined" && palette != null) {
			params += "palette="+palette;
		}
		ColorPicker.currentShowId = showid;
		ColorPicker.currentInputId = inputid;
		if (ColorPicker.type == "div") {
			if ($("ColorPicker") == null) {
				new Insertion.Bottom(document.body,
					'<div id="ColorPicker" style="position:absolute;display:none;width:130px;height:190px;"></div>');
				new Draggable('ColorPicker', {
					handle: 'dragHandler'
				});
				new Ajax.Updater(
						"ColorPicker"
					,	"/treemotion/products/ColorPicker/app/colorTable.php"
					,	{
							parameters: params
						,	method: "get"
						}
				);
			}
			Position.clone(showid, "ColorPicker", {
					setWidth: false
				,	setHeight: false
			});
			$('ColorPicker').setStyle({
				zIndex: 999
			});
		Element.show($("ColorPicker"));
		} else {
			ColorPicker.opened.push(
				window.open("/treemotion/products/ColorPicker/app/index.php?"+params));
		}
	}
	
,	pick: function(color) {
		Element.setStyle(ColorPicker.currentShowId, {
			backgroundColor: "#"+color
		});
		$( ColorPicker.currentInputId ).value = '#' + color;
		this.hide();
		var func = ColorPicker.callbacks.get(ColorPicker.currentInputId);
		if (func != null) {
			func();
		}
	}
,	hide: function()
	{
		if (ColorPicker.type == "div" && $( 'ColorPicker' ) ) {
			Element.hide($("ColorPicker"));
		} else {
			for (var i = 0; i < ColorPicker.opened.length; i++) {
				ColorPicker.opened[i].close();
			}
		}
		clearInterval( this.inte );
	}
,	over: function()
	{
		clearInterval( this.inte );
		this.isOver = true;
	}
,	out: function()
	{
		this.isOver = false;
		this.inte = setInterval( "ColorPicker.closeByInte()", 2000 );
	}
,	closeByInte: function()
	{
		this.hide();
	}
});
