(function() {
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;

function createCP(idcount)
{
    YAHOO.util.Event.onContentReady("button-container"+idcount, function () {
        function onButtonOption() {
            /*
                Create an empty body element for the Menu instance in order to 
                reserve space to render the ColorPicker instance into.
            */
            oColorPickerMenu.setBody(" ");
            oColorPickerMenu.body.id = "color-picker-container"+idcount;
            // Render the Menu into the Button instance's parent element
            oColorPickerMenu.render(this.get("container"));
            /*
                 Create a new ColorPicker instance, placing it inside the body 
                 element of the Menu instance.
            */
            var oColorPicker = new YAHOO.widget.ColorPicker(oColorPickerMenu.body.id, {
                                    showcontrols: true,
                                    images: {
                                        PICKER_THUMB: "http://yui.yahooapis.com/2.4.1/build/colorpicker/assets/picker_thumb.png",
                                        HUE_THUMB: "http://yui.yahooapis.com/2.4.1/build/colorpicker/assets/hue_thumb.png"
                                    }
                                });
            // Align the Menu to its Button
            oColorPickerMenu.align();
            /*
                Add a listener for the ColorPicker instance's "rgbChange" event
                to update the background color and text of the Button's 
                label to reflect the change in the value of the ColorPicker.
            */
            oColorPicker.on("rgbChange", function (p_oEvent) {
                var sColor = "#" + this.get("hex");
                //UPDATED CODE HERE
                YAHOO.util.Dom.setStyle("current-color-" + idcount, "backgroundColor", sColor);
                YAHOO.util.Dom.get("current-color-" + idcount).innerHTML = "Current color is " + sColor;
				YAHOO.util.Dom.get("hex"+idcount).value = sColor;
            });
            // Remove this event listener so that this code runs only once
            this.unsubscribe("option", onButtonOption);
        }
        // Create a Menu instance to house the ColorPicker instance
        var oColorPickerMenu = new YAHOO.widget.Menu("color-picker-menu"+idcount);
        // Create a Button instance of type "split"
        var oButton = new YAHOO.widget.Button({ 
                                            type: "split", 
                                            id: "color-picker-button"+idcount, 
                                            //UPDATED CODE HERE
                                            label: "<em id=\"current-color-" + idcount + "\">Current color is #FFFFFF.</em>", 
                                            menu: oColorPickerMenu, 
                                            container: this });
        /*
            Add a listener for the "option" event.  This listener will be
            used to defer the creation the ColorPicker instance until the 
            first time the Button's Menu instance is requested to be displayed
            by the user.
        */
        oButton.on("option", onButtonOption);
    });
}
	createCP(1); createCP(2); createCP(3);

})();
 
