/* DHTML Color Picker : v1.0.4 : 2008/04/17 */
/* http://www.colorjack.com/software/dhtml+color+picker.html */

/**
 * jscolor, JavaScript Color Picker
 *
 * @version 1.3.1
 * @license GNU Lesser General Public License, http://www.gnu.org/copyleft/lesser.html
 * @author  Jan Odvarko, http://odvarko.cz
 * @created 2008-06-15
 * @updated 2010-01-23
 * @link    http://jscolor.com
 */

// BUGS: internet explorer, scroll down a bit, and move the hue bar

var pejcolor = {
	
	// VALUES
	
	maxValue: {'H':360,'S':100,'V':100},
	HSV: {H:360, S:100, V:100},

	slideHSV: {H:360, S:100, V:100},
	zINDEX: 15,
	stop: 1,
	
	colorChanged: null, // must be defined
	
	// HELPER FUNCTIONS
	
	$: function(v,o)
	{
		return((typeof(o)=='object'?o:document).getElementById(v));
	},
	
	$S: function (o)
	{
		o=$(o);
		if(o)
			return(o.style);
	},

	// return coordinates of top most absolute positioned element
	// in this case it's the big plugin div
	abPos: function(o)
	{
		var o = (typeof(o)=='object' ? o : $(o));
		var z = {X:0,Y:0};
		
		while(o != null)
		{
			z.X += o.offsetLeft;
			z.Y += o.offsetTop;
			o = o.offsetParent;
		};
		return(z);
	},

	agent: function(v)
	{
		return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0));
	},
	
	toggle: function(v)
	{
		$S(v).display=($S(v).display=='none'?'block':'none');
	},

	// return whether v is between a and z
	within: function(v,a,z)
	{
		return((v>=a && v<=z)?true:false);
	},

	XY: function(e,v) // second parameter is to return X value (0) or Y value (1)
	{
		var z = this.agent('msie') ? [event.clientX + document.body.scrollLeft, event.clientY + document.body.scrollTop] : [e.pageX, e.pageY];
		return(z[this.zero(v)]);
	},

	zero: function(v)
	{
		v = parseInt(v);
		return(!isNaN(v) ? v : 0);
	},
	
	init: function()
	{
		alert('gay');
	},
	
	// scale HSV value depending on range, as detirmined by pixels
	mkHSV: function(a,b,c)
	{
		return(Math.min(a, Math.max(0, Math.ceil((parseInt(c)/b) * a))));
	},
	
	// "REAL" FUNCTIONS
	
	load: function(parentid)
	{
		var html = '<div id="picker" style="TOP: 58px; Z-INDEX: 20; display: none;"> \
			\
			<div id="pickerCUR"></div> \
			\
			<div id="pickerHEX" onmousedown="stop=0; setTimeout(\'stop=1\',100);">FFFFFF</div> \
			\
			<div id="pickerCLOSE" onmousedown="spot_remove();" title="Remove Color">X</div> \
			\
			<br /> \
			\
			<div id="pickerSV" onmousedown="pejcolor.HSVslide(\'pickerSVslide\',\'picker\',event)" title="Saturation + Value"> \
				<div id="pickerSVslide" style="TOP: -4px; LEFT: -4px;"> \
					<br /> \
				</div> \
			</div> \
			\
			<form id="pickerH" onmousedown="pejcolor.HSVslide(\'pickerHslide\',\'picker\',event)" title="Hue"> \
				<div id="pickerHslide" style="TOP: -7px; LEFT: -8px;"> \
					<br /> \
				</div> \
				<div id="pickerHmodel"></div> \
			</form> \
			\
		</div>';
		
		$(parentid).innerHTML = html + $(parentid).innerHTML;
		
		this.loadSV();
	},
	
	HSVslide: function(d,o,e)
	{
		var inst = this;
		
		// sets variables of mouse position relative to top left of SV div
		function tXY(e)
		{
			tY=inst.XY(e,1)-ab.Y;
			tX=inst.XY(e)-ab.X;
			
			//console.log(tX + " " + tY);
		}
		
		function ckHSV(a,b)
		{
			if(inst.within(a,0,b)) // check if a is between 0 and b
				return(a);
			else if(a>b)
				return(b);
			else if(a<0)
				return('-'+oo);
		}
		
		// mouse dragging along SVslide or Hslide
		function drag(e)
		{
			if(!inst.stop)
			{
				tXY(e);
			
				if(d=='pickerSVslide')
				{
					// move SVslide circle
					ds.left=ckHSV(tX-oo,162)+'px';
					ds.top=ckHSV(tY-oo,162)+'px';
					
					//console.log(ds.left + ds.top);
					inst.slideHSV.S = inst.mkHSV(100,162,ds.left);
					inst.slideHSV.V = 100 - inst.mkHSV(100,162,ds.top);
					
					//console.log("H:" + slideHSV.H + " S:" + slideHSV.S + " V:" + slideHSV.V);
					
					inst.HSVupdate();
				}
				else if(d=='pickerHslide')
				{
					var ck=ckHSV(tY-oo,163), r='HSV', z={};
					//console.log(ck);
					
					ds.top=(ck-5)+'px'; // from -7 to 158
					//console.log(ds.top);
					
					inst.slideHSV.H = inst.mkHSV(360,163,ck);
					inst.slideHSV['H'] = 360 - inst.slideHSV['H']; // retarded
					//console.log("H:" + slideHSV.H + " S:" + slideHSV.S + " V:" + slideHSV.V);
					
					/*for(var i in r)
					{
						i=r.substr(i,1);
						z[i]=(i=='H')?inst.maxValue[i]-inst.mkHSV(inst.maxValue[i],163,ck):inst.HSV[i];
						
						//if(i=='H')
						//	console.log(maxValue[i] + " 163 " + ck);
					}
					
					z['H'] = 360 - z['H']; // retarded*/
					
					//console.log(inst.HSV.H);
					//inst.HSVupdate(z);
					//console.log(inst.HSV.H);
					
					inst.HSVupdate();
					
					// background of SV item, to show color differences on square
					$S('pickerSV').backgroundColor=color.HSV_HEX({H:inst.HSV.H, S:100, V:100});
				}
			}
		}

		if(this.stop)
		{
			this.stop='';
			
			// style of SVslide or Hslide
			var ds=this.$S(d);
			
			// picker div coordinates
			var ab = this.abPos(this.$(o));
			var tX, tY;
			
			var oo=(d=='pickerHslide')?2:4;
			
			// top left of SV box
			ab.X+=10;
			ab.Y+=22;
			
			if(d=='pickerSVslide')
				this.slideHSV.H=this.HSV.H;

			document.onmousemove=drag;
			document.onmouseup=function() {
				var ins = inst;
				ins.stop=1;
				document.onmousemove='';
				document.onmouseup='';
			};
			
			drag(e);
		}
	},

	HSVset: function(hex)
	{
		hex = color.colorToHex(hex);
		//hex = hex.substr(1);
		
		v=hex;

		$('pickerHEX').innerHTML=v.toUpperCase();
		$S('pickerCUR').background=v;
		
		hsv = color.RGB_HSV(color.HEX_RGB(hex));
		
		// to default to the top red zone rather than bottom
		if(hsv.H == 360)
			hsv.H = 0;
		
		// move Hslide
		var px = this.mkHSV(163, 360, (360 - hsv.H)); // reverse hue because this color picker is retarded
		//console.log(px);
		$S('pickerHslide').top = (px - 5) + 'px';
		//console.log($S('Hslide').top);
		
		// move SVslide
		var leftpx = this.mkHSV(162, 100, hsv.S);
		if(leftpx == 0)
			leftpx = -4;
		$S('pickerSVslide').left = leftpx + 'px';
		var toppx = this.mkHSV(162, 100, (100 - hsv.V));
		if(toppx == 0)
			toppx = -4;
		$S('pickerSVslide').top = toppx + 'px';
		
		// background of SV item, to show color differences on square
		$S('pickerSV').backgroundColor=color.HSV_HEX({H:hsv.H, S:100, V:100});
		
		this.HSV = hsv;
		this.slideHSV = hsv;
	},

	HSVupdate: function(v)
	{
		//console.log('v: ' + v.H);
		//console.log(this.HSV.H);
		this.HSV = this.slideHSV;
		//v=color.HSV_HEX(this.HSV=v?v:this.slideHSV);
		//console.log(this.HSV.H);
		v = color.HSV_HEX(this.slideHSV);
		
		$('pickerHEX').innerHTML=v.toUpperCase();
		$S('pickerCUR').background=v;
		
		//$S('plugID').background='#'+v;
		this.colorChanged();
		
		return(v);

	},

	loadSV: function()
	{
		var z='';

		for(var i=165; i>=0; i--)
		{
			z+="<div style=\"BACKGROUND: "+color.HSV_HEX({H:Math.round((360/165)*i), S:100, V:100})+";\"><br /><\/div>";
		}

		$('pickerHmodel').innerHTML=z;
		
	}
}
