// Color Picker

// COLOR PICKER
//create a namespace object in the example namespace:
YAHOO.namespace("qw.colorpicker")

//create a new object for this module:
YAHOO.qw.colorpicker.inDialog = function(){

	//Some shortcuts to use in our example:
	var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom, lang = YAHOO.lang;
	
	return {
		
		init: function()
		{
		
			// Instantiate the Dialog
			this.dialog = new YAHOO.widget.Dialog("yui-picker-panel", {
				width: "400px",
				height: "275px",
				fixedcenter: true,
				visible: false,
				close: false,
				constraintoviewport: true,
				buttons: [{
					text: "Set Color",
					handler: function(){
						hex = this.picker.getAttributeConfig('hex').value;
						this.cancel();
						onColorPickSet(hex,this.op);
					},
					isDefault: true
				}, {
					text: "Cancel",
					handler: function(){
						this.cancel();
						onColorPickCancel(this.op);
					}
				}]
			});
			
			// Once the Dialog renders, we want to create our Color Picker
			// instance.
			this.dialog.renderEvent.subscribe(function() {
				if (!this.picker) { //make sure that we haven't already created our Color Picker
					YAHOO.log("Instantiating the color picker", "info", "example");
					this.picker = new YAHOO.widget.ColorPicker("yui-picker", {
						container: this.dialog,
						images: {
							PICKER_THUMB: "http://developer.yahoo.com/yui/examples/colorpicker/assets/picker_thumb.png",
							HUE_THUMB: "http://developer.yahoo.com/yui/examples/colorpicker/assets/hue_thumb.png"
						},
						showhexcontrols: true // default is false
					});
			
					//listen to rgbChange to be notified about new values
					this.picker.on("rgbChange", function(o,picker) {
						onColorPickChange(o,picker.op);
					},this.picker);
				}
			});	
			
			
			this.show = function(op)
			{
				this.dialog.op = op;
				this.dialog.picker.op = op;
				var rgb = YAHOO.util.Color.hex2rgb(op.value.replace("#",""));
				this.dialog.picker.setValue(rgb,false);
				this.dialog.show();
			}
			
			// We're all set up with our Dialog's configurations. Now, render the Dialog 
			
			this.dialog.render(); 
		}
		
		
	}

}()

YAHOO.util.Event.onDOMReady(YAHOO.qw.colorpicker.inDialog.init, YAHOO.qw.colorpicker.inDialog, true); 


function onColorPickSet(hex,op)
{
	hex = "#" + hex;
	qwCommit(op,hex);
}

function onColorPickChange(o,op)
{
	hex = "#" + YAHOO.util.Color.rgb2hex(o.newValue[0],o.newValue[1],o.newValue[2]);
	qwSet(op,hex);
}

function onColorPickCancel(op)
{
	qwSet(op,op.value);
}

function doColorDialog(op)
{
	YAHOO.qw.colorpicker.inDialog.show(op);
}

	