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

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

	//Some shortcuts to use in our example:
	var Event=YAHOO.util.Event,
		Dom=YAHOO.util.Dom,
		lang=YAHOO.lang;

	return {
		colourChangedEvent : new YAHOO.util.CustomEvent("colourChangedEvent"),
		currentRGB:null, //keeps track rgb value set by user. No effect on edges until user clicks apply
		//In our initialization function, we'll create the dialog;
		//in its render event, we'll create our Color Picker instance.
        init: function() {

            // Instantiate the Dialog
            this.dialog = new YAHOO.widget.Dialog("yui-picker-panel", { 
				width : "350px",
				close: false,
				fixedcenter : true,
				visible : false, 
				zIndex : 51,
				constraintoviewport : true,
				buttons : [ { text:"Apply", handler:this.handleApply.bind(this), isDefault:true },
							{ text:"Cancel", handler:this.handleCancel.bind(this) } ]
             });
             var t=this;
			// 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: "/javascript/yui2.8/build/colorpicker/assets/picker_thumb.png", 
							                HUE_THUMB: "/javascript/yui2.8/build/colorpicker/assets/hue_thumb.png" 
							            } ,
						//Here are some other configurations we could use for our Picker:
						showcontrols: false  // default is true, false hides the entire set of controls
						//showhexcontrols: true, // default is false
						//showhsvcontrols: true  // default is false
					});

					//listen to rgbChange to be notified about new values
					this.picker.on("rgbChange", function(o) {
						//YAHOO.log(lang.dump(o), "info", "example");
						t.currentRGB=o.newValue;
					});
				}
			});	
			
			// If we wanted to do form validation on our Dialog, this
			// is where we'd do it.  Remember to return true if validation
			// passes; otherwise, your Dialog's submit method won't submit.
            this.dialog.validate = function() {
				return true;
            };

            // Wire up the success and failure handlers
            this.dialog.callback = { success: this.handleSuccess, thisfailure: this.handleFailure };
            
            // We're all set up with our Dialog's configurations;
			// now, render the Dialog
            this.dialog.render();
//            Event.on("exceedthresholdbuttonid", "click", this.dialog.show, this.dialog, true);
			//initialization complete:
			YAHOO.log("Example initialization complete.", "info", "example");

		},
		
		//We'll wire this to our Dialog's submit button:
		handleApply: function() {
			//submit the Dialog:
			//this.submit();
			this.colourChangedEvent.fire(this.type, this.currentRGB);
			this.dialog.hide();
		},
 
 		//If the Dialog's cancel button is clicked,
		//this function fires
		handleCancel: function() {
			
			this.dialog.hide();
			//log this step to logger:
			YAHOO.log("Dialog was submitted.", "info", "example");
		},
		
		//We'll use Connection Manager to post our form data to the
		//server; here, we set up our "success" handler.
		handleSuccess: function(o) {
			YAHOO.log("Connection Manager returned results to the handleSuccess method.", "info", "example");
			var response = o.responseText;
			//On Yahoo servers, we may get some page stamping;
			//we can trim off the trailing comment:
			response = response.split("<!")[0];
			//write the response to the page:
			response = "<strong>The data received by the server was the following:</strong> " + response;
			document.getElementById("resp").innerHTML = response;
		},
		
		handleFailure: function(o) {
			YAHOO.log("Connection Manager returned results to the handleFailure method.", "error", "example");
			YAHOO.log("Response object:" + lang.dump(o), "error", "example");
		},
		
		showPicker: function(rgb, type){
			rgb=rgb.replace("rgb(", "").replace(/\).*/,"").replace(/ /g,"").split(",");
			this.dialog.picker.setValue(rgb, true);
			this.dialog.show();
			this.type=type; // type can be 1 or 0 to indicate exceed or below threshold button pressed.
		}
   
	}


}();

//The earliest safe moment to instantiate a Dialog (or any
//Container element is onDOMReady; we'll initialize then:
YAHOO.util.Event.onDOMReady(YAHOO.example.colorpicker.inDialog.init, YAHOO.example.colorpicker.inDialog, true);
