//------------------------------------------------------------------------------
// Class:  ColorPicker
//------------------------------------------------------------------------------
// Author:  CL
// Date:  2007/04/27
// Description:  This class is for the color picker
//------------------------------------------------------------------------------
function ColorPicker()
{
			var hue;
			var picker;
			var r;
			var g;
			var b;
			
			// Methods:
			this.getWindowSize = getWindowSize;
			this.init = init;
			this.pickerInit = pickerInit;
			this.pickerUpdate = pickerUpdate;
			this.hueUpdate = hueUpdate;
			this.pickerSwatchUpdate = pickerSwatchUpdate;
			this.displayPicker = displayPicker;
			this.closePicker = closePicker;
			
			function getWindowSize()
			{
					var width = height = 0;
					var scrollLeft = scrollTop = 0;
					
					if ( typeof(window.innerWidth) == "number" )
					{
						width = window.innerWidth;
						height = window.innerHeight;
						scrollLeft = window.pageXOffset;
						scrollTop = window.pageYOffset;
					}
					else if ( document.documentElement &&
						(document.documentElement.clientWidth || document.documentElement.clientHeight) )
					{
						width = document.documentElement.clientWidth;
						height = document.documentElement.clientHeight;
						scrollLeft = document.documentElement.scrollLeft;
						scrollTop = document.documentElement.scrollTop;
					}
					else if ( document.body &&
						(document.body.clientWidth || document.body.clientHeight) )
					{
						width = document.body.clientWidth;
						height = document.body.clientHeight;
						scrollLeft = document.body.scrollLeft;
						scrollTop = document.body.scrollTop;
					}
			
					return {width:width, height:height, scrollLeft:scrollLeft, scrollTop:scrollTop};
			}
		
			function init() {
				pickerInit();
				if (document.getElementById("colorfield1"))
					ddcolorposter.fillcolorbox("colorfield1", "colorbox1"); 
				if (document.getElementById("colorfield2"))
					ddcolorposter.fillcolorbox("colorfield2", "colorbox2");
				if (document.getElementById("colorfield3"))
					ddcolorposter.fillcolorbox("colorfield3", "colorbox3");
				if (document.getElementById("colorfield4"))
					ddcolorposter.fillcolorbox("colorfield4", "colorbox4");
				if (document.getElementById("colorfield5"))
					ddcolorposter.fillcolorbox("colorfield5", "colorbox5");
				if (document.getElementById("colorfield6"))
					ddcolorposter.fillcolorbox("colorfield6", "colorbox6");
				if (document.getElementById("colorfield7"))
					ddcolorposter.fillcolorbox("colorfield7", "colorbox7");
			}
		
			// Picker ---------------------------------------------------------
		
			function pickerInit() {
				hue = YAHOO.widget.Slider.getVertSlider("hueBg", "hueThumb", 0, 180);
				hue.onChange = function(newVal) { hueUpdate(newVal); };
		
				picker = YAHOO.widget.Slider.getSliderRegion("pickerDiv", "selector",
						0, 180, 0, 180);
				picker.onChange = function(newX, newY) { pickerUpdate(newX, newY); };
		
				hueUpdate();

				dd1 = new YAHOO.util.DD("pickerPanel");
				dd1.setHandleElId("pickerHandle");
				dd1.endDrag = function(e) {
					// picker.thumb.resetConstraints();
					// hue.thumb.resetConstraints();
				};
			}
		
			executeonload(init);
		
			function pickerUpdate(newX, newY) {
				pickerSwatchUpdate();
			}
		
			function hueUpdate(newVal) {
		
				var h = (180 - hue.getValue()) / 180;
				if (h == 1) { h = 0; }
		
				var a = YAHOO.util.Color.hsv2rgb( h, 1, 1);
		
				document.getElementById("pickerDiv").style.backgroundColor =
					"rgb(" + a[0] + ", " + a[1] + ", " + a[2] + ")";
		
				pickerSwatchUpdate();
			}
		
			function pickerSwatchUpdate() {
				var h = (180 - hue.getValue());	
				h = h / 180;
		
				var s = picker.getXValue() / 180;
			
				var v = (180 - picker.getYValue()) / 180;
		
				var a = YAHOO.util.Color.hsv2rgb( h, s, v );
		
				document.getElementById("pickerSwatch").style.backgroundColor =
					"rgb(" + a[0] + ", " + a[1] + ", " + a[2] + ")";
	
				var hexvalue = document.getElementById("pickerhexval").value =
					YAHOO.util.Color.rgb2hex(a[0], a[1], a[2]);
					ddcolorposter.initialize(a[0], a[1], a[2], hexvalue)
			}
			
			function displayPicker(currentPickerID)
			{
				var TheColorPicker = document.getElementById('colorpickerUtil');
				var TheCurrentColorValue = document.getElementById(currentPickerID).value;
								
				if(TheCurrentColorValue.length > 0)
				{
					var rgb = YAHOO.util.Color.hex2rgb(TheCurrentColorValue);				
					var hsv = YAHOO.util.Color.rgb2hsv( rgb[0], rgb[1], rgb[2] );
					r = rgb[0];
					g = rgb[1];
					b = rgb[2];
					
					document.getElementById("pickerDiv").style.backgroundColor = "rgb(" + rgb + ")";
					document.getElementById("pickerSwatch").style.backgroundColor = "rgb(" + rgb + ")";
					document.getElementById("pickerhexval").value = TheCurrentColorValue;
				}
				
				
				TheColorPicker.style.display = "block";
				TheColorPicker.style.visibility = "visible";

				/*var oWindowSize = getWindowSize();
				var widthDiff = oWindowSize.width + 300;
				var heightDiff = oWindowSize.height - 100;
						
				TheColorPicker.style.left = ((widthDiff > 0 ? (widthDiff/2) : 0)+oWindowSize.scrollLeft) + "px";
				TheColorPicker.style.top =  ((heightDiff > 0 ? (heightDiff/2) : 0)+oWindowSize.scrollTop) + "px";
				
				TheColorPicker.style.display = "block";
				if(TheColorPicker.style.visibility == "hidden")
				{
					TheColorPicker.style.visibility = "visible";
				}
				*/	
				
				if(BrowserDetect.browser == 'Explorer')
				{
							for(var i=0; i<document.images.length; i++)
							{
								  var img = document.images[i]
								  var imgName = img.src.toUpperCase()
								  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
									 {
									 var imgID = (img.id) ? "id='" + img.id + "' " : ""
									 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
									 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
									 var imgStyle = "display:inline-block;" + img.style.cssText
									 if (img.align == "left") imgStyle = "float:left;" + imgStyle
									 if (img.align == "right") imgStyle = "float:right;" + imgStyle
									 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
									 var strNewHTML = "<span " + imgID + imgClass + imgTitle
									 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
									 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
									 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
									 img.outerHTML = strNewHTML
									 i = i-1
									 }
							}

				}

				if(TheCurrentColorValue.length > 0)
				{	
					/*hue.setValue(Math.round(180 - (180*hsv[0])));
					picker.setRegionValue(Math.round((hsv[1] * 180)), Math.round(180 - (180*hsv[2])));*/
					
					hue.setValue(Math.round((1 - hsv[0]) * 181));
					picker.setRegionValue(Math.round(hsv[1] * 180),Math.round((1 - hsv[2]) * 180));
				}
				
			}
			
			function closePicker()
			{
				var TheColorPicker = document.getElementById('colorpickerUtil');
				TheColorPicker.style.display = "none";
			}
}

// Create ColorPicker object.
oColorPicker = new ColorPicker();