/* Original Interclasse Colour Picker by Jean-Luc Antoine on http://www.interclasse.com/scripts/colorpicker.php */
/* Oct07 - Modified by Paul Blackman for use in SDNMenu demonstration http://www.ictinus.com/sdnmenu/ */

ICP = function () {
	if (!document.getElementById || !document.getElementByTagName)
		return false;

};

ICP.prototype.init = function(id) {

	var thisPicker = this;
	this.picker = document.getElementById(id);
	this.picker.innerHTML = "<div id='icpimg'><div id='icplabel'>test</div></div>";
	this.picker.style.marginLeft = 'auto';
	this.picker.style.marginRight= 'auto';
	this.picker.style.height = '64px';
	this.picker.style.width = '128px';
	this.pickerimg = document.getElementById('icpimg');
	this.pickerimg.style.float = 'left';
	this.pickerimg.style.width = '64px';
	this.pickerimg.style.height = '64px';
	this.pickerimg.style.backgroundImage = "URL(./icp/ICPColours.png)";
	this.pickerimg.style.repeat = "no-repeat";
	this.pickerimg.onmousemove = function (e) { thisPicker.t(e); };
	this.pickerimg.onclick = function (e) { thisPicker.p(e); };
	this.pickerrgb = document.getElementById('icplabel');
	this.pickerrgb.style.float = 'right';
	this.pickerrgb.style.height = '64px';
	this.pickerrgb.style.width = '64px';
	this.pickerrgb.style.marginLeft = '64px';

	with (this) {
		this.total=1657; this.X=this.Y=this.j=this.RG=this.B=0;
		this.aR=new Array(total);this.aG=new Array(total);this.aB=new Array(total);
		for (var i=0;i<256;i++){
			this.aR[i+510]=this.aR[i+765]=this.aG[i+1020]=this.aG[i+5*255]=this.aB[i]=this.aB[i+255]=0;
			this.aR[510-i]=this.aR[i+1020]=this.aG[i]=this.aG[1020-i]=this.aB[i+510]=this.aB[1530-i]=i;
			this.aR[i]=this.aR[1530-i]=this.aG[i+255]=this.aG[i+510]=this.aB[i+765]=this.aB[i+1020]=255;
			if(i<255){this.aR[i/2+1530]=127;this.aG[i/2+1530]=127;this.aB[i/2+1530]=127;}
		}
		this.hexbase=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
		var i=0;
		this.jl=new Array();
		this.strTarget = "";
		for(x=0;x<16;x++)for(y=0;y<16;y++) this.jl[i++]=this.hexbase[x]+this.hexbase[y];
		var H=W=63;
		var rgb;
/*
		strTarget = '<'+'table width=64 height=64 border="0" cellspacing="0" cellpadding="0" onMouseover="myPicker.t(event)" onClick="myPicker.p(event)">';
 		for (Y=0;Y<=H;Y++){
			s='<'+'tr height=1>';j=Math.round(Y*(510/(H+1))-255)
			for (X=0;X<=W;X++){
				i=Math.round(X*(total/W))
				R=aR[i]-j;if(R<0)R=0;if(R>255||isNaN(R))R=255
				G=aG[i]-j;if(G<0)G=0;if(G>255||isNaN(G))G=255
				B=aB[i]-j;if(B<0)B=0;if(B>255||isNaN(B))B=255
				s=s+'<'+'td width=1 bgcolor=#'+jl[R]+jl[G]+jl[B]+'><'+'/td>'
			}
			strTarget = strTarget+s+'</tr>';
		}
		strTarget = strTarget+'</table>';
		this.picker.innerHTML = strTarget;
*/

		this.ns6=document.getElementById&&!document.all
		this.ie=document.all
		this.artabus=''
	}
};
ICP.prototype.getRGB = function (cX,cY) {
	var H=W=63;
	with (this) {
		var j=Math.round(cY*(510/(H+1))-255);
		var i=Math.round(cX*(total/W));
		R=aR[i]-j;if(R<0)R=0;if(R>255||isNaN(R))R=255
		G=aG[i]-j;if(G<0)G=0;if(G>255||isNaN(G))G=255
		B=aB[i]-j;if(B<0)B=0;if(B>255||isNaN(B))B=255
		return "#"+jl[R]+jl[G]+jl[B];
	}
};
ICP.prototype.p = function (e) {
	with (this) {
		if (!e) e = window.event;
		source = (e.target)?e.target:e.srcElement;
		var posEvent = findPos_Event(e);
		var posElement = findPos_Element(source);
		var cX = (posEvent[0] - posElement[0]);
		var cY = (posEvent[1] - posElement[1]);
		this.strRGB = getRGB(cX,cY);
		if (typeof(update) != "undefined") update(this.strRGB);
	}
};
ICP.prototype.t = function (e) {
	with (this) {
		if (!e) e = window.event;
		source = (e.target)?e.target:e.srcElement;
		var posEvent = findPos_Event(e);
		var posElement = findPos_Element(source);
		var cX = (posEvent[0] - posElement[0]); if (cX > 64) return;
		var cY = (posEvent[1] - posElement[1]); if (cY > 64) return;
		var strHoverRGB = getRGB(cX,cY);
		pickerrgb.innerHTML = strHoverRGB;
		pickerrgb.style.backgroundColor=strHoverRGB;
		(cY > 30)? pickerrgb.style.color = "#FFF" : pickerrgb.style.color = "#333" ;
	}
};
ICP.prototype.findPos_Element = function (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
};

ICP.prototype.findPos_Event = function (e) {
  if( !e ) {
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.pageX ) == 'number' ) {
    //most browsers
    var xcoord = e.pageX;
    var ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    //Internet Explorer and older browsers
    //other browsers provide this, but follow the pageX/Y branch
    var xcoord = e.clientX;
    var ycoord = e.clientY;
    var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
     ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
     ( navigator.vendor == 'KDE' )
    if( !badOldBrowser ) {
      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //IE 4, 5 & 6 (in non-standards compliant mode)
        xcoord += document.body.scrollLeft;
        ycoord += document.body.scrollTop;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE 6 (in standards compliant mode)
        xcoord += document.documentElement.scrollLeft;
        ycoord += document.documentElement.scrollTop;
      }
    }
  } else {
    //total failure, we have no way of obtaining the mouse coordinates
    return;
  }
  return [xcoord,ycoord];
}
