var c_type = '';
var out_obj = '';

var SelectedObj = '';

ColorPicker_targetInput = null;

function ColorPicker_writeDiv() {
	document.writeln("<div id=\"colorPickerDiv\" style=\"position:absolute;visibility:hidden;\"></div>");
}

function ColorPicker_show(anchorname) {
	this.showPopup(anchorname);
}

function ColorPicker_pickColor(color,obj) {
	obj.hidePopup();
	pickColor(color);
}

// A Default "pickColor" function to accept the color passed back from popup.
// User can over-ride this with their own function.
function pickColor(color) {
	if (ColorPicker_targetInput == null) {
		alert("Target Input is null, which means you either didn't use the 'select' function or you have no defined your own 'pickColor' function to handle the picked color!");
		return;
	}
	ColorPicker_targetInput.value = color;
}

// This function is the easiest way to popup the window, select a color, and
// have the value populate a form field, which is what most people want to do.
function ColorPicker_select(inputobj, linkname, val, o_val) {
    c_type = val;
    out_obj= o_val;
    SelectedObj = inputobj;
	if (inputobj.type!="text" && inputobj.type!="hidden" && inputobj.type!="textarea") { 
		alert("colorpicker.select: Input object passed is not a valid form input object"); 
		window.ColorPicker_targetInput=null;
		return;
	}
	window.ColorPicker_targetInput = inputobj;
	this.show(linkname);
}
	
// This function runs when you move your mouse over a color block, if you have a newer browser
function ColorPicker_highlightColor(c) {
	var thedoc = (arguments.length > 1) ? arguments[1] : window.document;
	var d = thedoc.getElementById("colorPickerSelectedColor");
	d.style.backgroundColor = c;
	d = thedoc.getElementById("colorPickerSelectedColorValue");
	d.innerHTML = c;
	PickerHover(SelectedObj, c);
}

function ColorPicker() {
	var windowMode = false;
	// Create a new PopupWindow object
	if (arguments.length == 0) {
		var divname = "colorPickerDiv";
	} else if (arguments[0] == "window") {
		var divname = '';
		windowMode = true;
	} else {
		var divname = arguments[0];
	}
	
	if (divname != "") {
		var cp = new PopupWindow(divname);
	} else {
		var cp = new PopupWindow();
		cp.setSize(225,250);
	}

	// Object variables
	cp.currentValue = "#FFFFFF";
	
	// Method Mappings
	cp.writeDiv = ColorPicker_writeDiv;
	cp.highlightColor = ColorPicker_highlightColor;
	cp.show = ColorPicker_show;
	cp.select = ColorPicker_select;

	// Code to populate color picker window
	var colors = new Array("#E88C6D","#F2C079","#FFF884","#BEDD8B","#83C590","#80C7C1","#7CC8F7","#8085C8","#E88EBF","#E88C96","#E15E47",
							"#EDA84B","#FFF647","#A7D15B","#4FB265","#4AB4AC","#41B6F4","#5863B6","#E15DA7","#E15E73","#DA1225","#E78A00",
							"#FFF300","#88C311","#00A03D","#00A294","#00A3F1","#2647A4","#DA0088","#DA0057","#8A0512","#935700","#9F9900",
							"#547A09","#006728","#006760","#00679F","#162A6A","#8C0059","#8A0038","#FFFFFF","#CCCCCC","#999999","#999999",
							"#666666","#333333","#000000","#FFF884","#82CCE4","#A7E4D0");
	var total = colors.length;
	var width = 10;
	var cp_contents = "";
	var windowRef = (windowMode)?"window.opener.":"";
	if (windowMode) {
		cp_contents += "<html><head><title>Select Color</title></head>";
		cp_contents += "<body marginwidth=\"0\" marginheight=\"0\" leftmargin=\"0\" topmargin=\"0\"><center>";
	}
	cp_contents += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"border: 1px solid #000000; border-left: none;\">";
	var use_highlight = (document.getElementById || document.all) ? true : false;
	for (var i = 0; i < total; i++) {
		if ((i % width) == 0) { cp_contents += "<tr>"; }
		if (use_highlight) { 
			var mo = 'onMouseOver="'+windowRef+'ColorPicker_highlightColor(\''+colors[i]+'\',window.document)"'; 
		} else { mo = ""; }
		cp_contents += '<td style=\"width: 13px; height: 13px; font-size: 7px; border-left: 1px solid #000000; border-bottom: 1px solid #000000\" bgcolor="'+colors[i]+'"><a href="#" onClick="'+windowRef+'ColorPicker_pickColor(\''+colors[i]+'\','+windowRef+'window.popupWindowObjects['+cp.index+']);return false;" '+mo+' style="text-decoration: none;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></td>';
		if ( ((i+1)>=total) || (((i+1) % width) == 0)) { 
			cp_contents += "</tr>";
		}
	}
	if (document.getElementById) { // If the browser supports dynamically changing TD cells, add the fancy stuff
		var width1 = Math.floor(width/2);
		var width2 = width = width1;
		cp_contents += "<tr><td style=\"border: 2px solid #EEEEEE\" colspan='"+width1+"' bgcolor='#ffffff' id='colorPickerSelectedColor'>&nbsp;</td><td bgcolor=\"#FFFFFF\" style=\"font-size: 11px;\" colspan='"+width2+"' align='center' id='colorPickerSelectedColorValue'>#FFFFFF</td></tr>";
	}
	cp_contents += "</table>";
	if (windowMode) {
		cp_contents += "</center></body></html>";
	}
	// end populate code

	cp.populate(cp_contents+"\n"); // Write the contents to the popup object
	cp.offsetY = 25; // Move the table down a bit so you can see it
	cp.autoHide();
	
	return cp;
}

