//used to create an instance
function cPicker()
{	
	this.open = open;
	this.generateSafe = GenerateSafe;
	this.generateMac = TCGenerateMac;
	this.generateWind = TCGenerateWind;
	this.dec2hex = TCDec2Hex;
	this.bldCell = TCBuildCell;
	this.setColour = selectColour;
	this.submit = submitColour;
	this.cancel = cancelColour;
}
//Uses to open the object
function open(spawnObj2, valueObj2)
{
	
	pickerId = 'ColourPicker';
	PickerCount = 0;
	//check of other instances
	if(document.getElementById(pickerId) != null)
	{
		cancelColour();
	}
	if(PickerCount < 1)
	{
		PickerCount = '';
	}
	//Create vars to re-use
	spawnObj =  spawnObj2;
	valueObj = valueObj2;
	if(!valueObj.id)
	{
		valueObj.id = 'ColourField'+PickerCount;
	}
	
	//generate position
	var curleft = curtop = 0;
	cur = spawnObj2;
	if (cur.offsetParent) {
		curleft = cur.offsetLeft
		curtop = cur.offsetTop
		while (cur = cur.offsetParent) {
			curleft += cur.offsetLeft
			curtop += cur.offsetTop
		}
	}
	div = document.createElement('div');
	div.id = 'ColourPicker'+PickerCount;
	div.style.width = '700px';
	div.style.padding = '8px';
	div.style.backgroundColor = '#FFF3CC';
	div.style.border = "#CCCCCC 1px solid";
	div.style.position = 'absolute';
	div.style.left = (curleft + 8 + spawnObj.clientWidth)+'px';
	div.style.top = (curtop) + 'px';
	
	div.innerHTML = '<div id="ColourPickerWrapper" style=" background-color: #FFF3CC;border: #CCCCCC 1px solid;"></div>'+
		'<div>'+
			'<div style="text-align:right;">'+
			'<input style="width:99%" id="CurrentColourText'+ PickerCount +'" type="text" />'+
			'<div id="CurrentColour'+ PickerCount +'" style="margin-top: 4px;margin-bottom: 4px;">&nbsp;</div>'+
			'<input type="button" onclick="colourPicker.cancel()" value="Cancel" /><input type="button" onclick="colourPicker.submit(\''+spawnObj.id+'\', \''+ PickerCount +'\')" name="ok" value="Done"/>'+ '</div></div>';
	
	
	
	document.body.appendChild(div);	
	document.getElementById('ColourPickerWrapper').innerHTML = '<table width="100%"><tr><td>' + this.generateWind(PickerCount) + '</td><td>' + this.generateMac(PickerCount) + '</td><td>' + this.generateSafe(PickerCount) + '</td></tr></table>';	
	
}
function submitColour(DestObj, PickerCount)
{
	value_ = document.getElementById('CurrentColourText'+PickerCount).value;
	document.getElementById(DestObj).value = value_;
	document.getElementById(DestObj).id = '';
	document.body.removeChild(document.getElementById('ColourPicker'));
	spawnObj = null;
	valueObj = null;
}

function cancelColour()
{
	try
	{
		document.body.removeChild(document.getElementById('ColourPicker'));
		document.getElementById(valueObj.id).id = '';
		spawpObj = null;
		valueObj = null;
	}
	catch(e){} // used to catch exceptions thrown when cancling the colourPicker
}
function selectColour(objectNo, Colour){
	if(objectNo < 1){
		objectNo = '';
	}
	
	oText = document.getElementById('CurrentColourText' + objectNo);
	oColour = document.getElementById('CurrentColour' + objectNo);
	oText.value = ''+Colour;
	oColour.style.backgroundColor = '#'+Colour;
}

function GenerateSafe(obj) {
	var s = '<table width="100%" height="175">';
	for (j = 0; j < 12; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i ++)
				s += this.bldCell(obj, k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, i * 51, 8, 10);
		s += "</tr>";
	}
	s += '</table>';
	return s;
}

function TCBuildCell (obj, R, G, B, w, h) {
	return '<td bgcolor="#' + this.dec2hex((R << 16) + (G << 8) + B) + '" onclick="colourPicker.setColour(\''+obj+'\', \'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')"></td>';
}

function TCDec2Hex(v) {
	v = v.toString(16);
	for(; v.length < 6; v = '0' + v);
	return v;
}

function TCGenerateWind(obj) {
	var s = '<table width="100%" height="175">';
	for (j = 0; j < 12; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i++)
				s += this.bldCell(obj, i * 51, k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, 8, 10);
		s += "</tr>";
	}
	s += '</table>';
	return s	
}
function TCGenerateMac(obj) {
	var s = '<table width="100%" height="175">'
	var c = 0,n = 1;
	var r,g,b;
	for (j = 0; j < 15; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i++){
				if(j<12){
				s += this.bldCell(obj, 255-(Math.floor(j / 2) * 51), 255-(k * 51 + (j % 2) * 51 * 3),255-(i * 51), 8, 10);
				}else{
					if(n<=14){
						r = 255-(n * 17);
						g=b=0;
					}else if(n>14 && n<=28){
						g = 255-((n-14) * 17);
						r=b=0;
					}else if(n>28 && n<=42){
						b = 255-((n-28) * 17);
						r=g=0;
					}else{
						r=g=b=255-((n-42) * 17);
					}
					s += this.bldCell(obj, r, g,b, 8, 10);
					n++;
				}
			}
		s += "</tr>";
	}
	s += '</table>'
	return s;
}

function TCGenerateGray() {
	var s = '';
	for (j = 0; j <= 15; j ++) {
		s += "<tr>";
		for (k = 0; k <= 15; k ++) {
			g = Math.floor((k + j * 16) % 256);
			s += this.bldCell(g, g, g, 9, 7);
		}
		s += '</tr>';
	}
	return s
}

var colourPicker = new cPicker();