var cp_hue = 180;
var cp_sat = 1;
var cp_val = 1;
var cp_r;
var cp_g;
var cp_b;
var cp_rf;
var cp_gf;
var cp_bf;
var cp_hex;
var cp_rgb_string;
var cp_rgb_f_string;

var cp_save_num = 1;
var cp_save_current = 1
var cp_save_total = 44;
var cp_save_array = new Array();

var currentPreview;
var currentValue;

// Open the color picker
function cp_open(v)
{
currentPreview = document.getElementById(v + '_preview');
currentValue = document.getElementsByName(v + '_value')[0];
colorPicker.style.display = "table";
}

// Close the color picker
function cp_close()
{
colorPicker.style.display = "none";
}

// Choose a color
function cp_use()
{
currentPreview.style.backgroundColor = cp_rgb_string;
currentValue.value = cp_rgb_string;
currentPreview.style.borderStyle = "solid";
cp_close();
document.editor.submit();
}

// Reload color items by form contents
function cp_reload(v)
{

if(v)
{
	value = document.getElementsByName(v + '_value')[0].value;
	item = document.getElementById(v + '_preview')

		if(value != "")
		{
			item.style.borderStyle = "solid";
			item.style.backgroundColor = value;
		}
		else
		{
			item.style.borderStyle = "dashed";
			item.style.backgroundColor = "#FFFFFF";	
		}
}
else
{
list="backgroundColor regularTextColor";
items=list.split(' '); 

	for(i=0; i < items.length; i++)
	{
		value = document.getElementsByName(items[i] + '_value')[0].value;
		item = document.getElementById(items[i] + '_preview')
		if(value != "")
		{
			item.style.borderStyle = "solid";
			item.style.backgroundColor = value;
		}
		else
		{
			item.style.borderStyle = "dashed";
			item.style.backgroundColor = "#FFFFFF";	
		}
	}
}

}

function resize_iframe() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

document.getElementById('stupid_iframe').height = myHeight * 0.80;

}

function viewToggle(e,section)
{

	element = document.getElementById('section_' + section);

	if(element.style.display == 'none' || element.style.display == '')
	{
		element.style.display = 'block';
		e.value = 'hide';
	}
	else
	{
		element.style.display = 'none';
		e.value = 'show';
	}
}

function starter()
{

resize_iframe();

cp_reload();

	colorPicker = document.getElementById("cp_layer");

	fixPNG(document.getElementById("cp_sv_img_id"));
	calculateRGB()
	svOffset = new Position(5, 5);
	hueOffset = new Position(5, 0);
	rgbOffset = new Position(0, 5);
	pointerOffset = new Position(0, navigator.userAgent.indexOf("Firefox")>= 0 ? 1 : 0);
	svLowerBounds = new Position(-5, -5);
	svUpperBounds = new Position(250, 250);
	new dragObject("cp_sv_cursor_id", "cp_sv_box_id", svLowerBounds, svUpperBounds, cp_svDown, cp_svMoved, cp_refresh_preview_static);

	hueLowerBounds = new Position(-5, 0);
	hueUpperBounds = new Position(355, 0);
	new dragObject("cp_hue_cursor_id", "cp_hue_box_id", hueLowerBounds,hueUpperBounds, cp_hueDown, cp_hueMoved, cp_refresh_preview_static);

	rgbLowerBounds = new Position(0, -5);
	rgbUpperBounds = new Position(0, 250);
	new dragObject("cp_rgb_r_arrow_id", "cp_rgb_r_box_id", rgbLowerBounds, rgbUpperBounds, cp_rgbDown, cp_rgbMoved, cp_refresh_preview_static);
	new dragObject("cp_rgb_g_arrow_id", "cp_rgb_g_box_id", rgbLowerBounds, rgbUpperBounds, cp_rgbDown, cp_rgbMoved, cp_refresh_preview_static);
	new dragObject("cp_rgb_b_arrow_id", "cp_rgb_b_box_id", rgbLowerBounds, rgbUpperBounds, cp_rgbDown, cp_rgbMoved, cp_refresh_preview_static);

	cp_refresh_hsv();
	cp_refresh_rgb();
	cp_refresh_preview();
	cp_refresh_preview_static();
}

function cp_svDown(e, circle)
{ 
	var pos = getMousePos(e);

	if(getEventTarget(e) == circle)
	{
		pos.X += parseInt(circle.style.left);
		pos.Y += parseInt(circle.style.top);
	}

	pos = correctOffset(pos, svOffset, true);

	pos = pos.Bound(svLowerBounds, svUpperBounds);

	pos.Apply(circle);

	cp_svMoved(pos);

}

function cp_svMoved(pos, element)
{
	pos = correctOffset(pos, svOffset, false);
	cp_sat = ((255 - pos.Y) / 255.0);
	cp_val = (pos.X / 255.0);

	calculateRGB()

	cp_refresh_rgb()
	cp_refresh_preview('active')
}

function cp_hueDown(e, element)
{ 
	var pos = getMousePos(e);

	if(getEventTarget(e) == element)
	{
		pos.X += parseInt(element.style.left);
	}

	pos = correctOffset(pos, hueOffset, true);

	pos = pos.Bound(hueLowerBounds, hueUpperBounds);

	pos.Apply(element);

	cp_hueMoved(pos);

}

function cp_hueMoved(pos, element)
{
	pos = correctOffset(pos, hueOffset, false);
	cp_hue = pos.X;

	calculateRGB();
	cp_refresh_rgb();

	cp_refresh_preview();
}


function cp_rgbDown(e, element)
{ 
	var pos = getMousePos(e);

	if(getEventTarget(e) == element)
	{
		pos.Y += parseInt(element.style.top);
	}

	pos = correctOffset(pos, rgbOffset, true);

	pos = pos.Bound(rgbLowerBounds, rgbUpperBounds);

	pos.Apply(element);

	cp_rgbMoved(pos,element);

}

function cp_rgbMoved(pos, element)
{
	pos = correctOffset(pos, rgbOffset, false);
	temp = 255 - pos.Y;

	if(element.id == "cp_rgb_r_arrow_id")
	{
		cp_r = temp;
	}
	else if(element.id == "cp_rgb_g_arrow_id")
	{
		cp_g = temp;
	}
	else
	{
		cp_b = temp;
	}

	calculateHSV()
	//cp_rgb_f_string = "rgb(" + cp_rf + "," + cp_gf + "," + cp_bf + ")";
	//document.getElementById('cp_sv_box_id').style.backgroundColor = cp_rgb_f_string;
	cp_refresh_hsv();
	cp_refresh_preview()
}

function cp_refresh_preview()
{
	cp_rgb_f_string = "rgb(" + cp_rf + "," + cp_gf + "," + cp_bf + ")";
	cp_rgb_string = "rgb(" + cp_r + "," + cp_g + "," + cp_b + ")";
	document.getElementById('cp_sv_box_id').style.backgroundColor = cp_rgb_f_string;
	document.getElementById('cp_preview_id').style.backgroundColor = cp_rgb_string;
}

function cp_refresh_hsv()
{	pos = new Position(cp_hue, 0);	pos = correctOffset(pos, hueOffset, true);
	pos = pos.Bound(hueLowerBounds, hueUpperBounds);
	pos.Apply(document.getElementById("cp_hue_cursor_id"));

	pos = new Position(cp_val * 255, (1-cp_sat) * 255);	pos = correctOffset(pos, svOffset, true);
	pos = pos.Bound(svLowerBounds, svUpperBounds);
	pos.Apply(document.getElementById("cp_sv_cursor_id"));
}

function cp_refresh_rgb()
{
	pos = new Position(0, (255 - cp_r));	pos = correctOffset(pos, rgbOffset, true);
	pos = pos.Bound(rgbLowerBounds, rgbUpperBounds);
	pos.Apply(document.getElementById("cp_rgb_r_arrow_id"));

	pos = new Position(0, (255 - cp_g));	pos = correctOffset(pos, rgbOffset, true);
	pos = pos.Bound(rgbLowerBounds, rgbUpperBounds);
	pos.Apply(document.getElementById("cp_rgb_g_arrow_id"));

	pos = new Position(0, (255 - cp_b));	pos = correctOffset(pos, rgbOffset, true);
	pos = pos.Bound(rgbLowerBounds, rgbUpperBounds);
	pos.Apply(document.getElementById("cp_rgb_b_arrow_id"));
}

function cp_refresh_preview_static()
{
	cp_rgb_f_string = "rgb(" + cp_rf + "," + cp_gf + "," + cp_bf + ")";
	document.getElementById('cp_sv_box_id').style.backgroundColor = cp_rgb_f_string;
	document.getElementById('cp_preview_static_id').style.backgroundColor = "rgb(" + cp_r + "," + cp_g + "," + cp_b + ")";
	document.getElementById('cp_input_hex_id').value = hexString();
	document.getElementById('cp_input_h_id').value = cp_hue;
	document.getElementById('cp_input_s_id').value = Math.round(cp_sat * 100);
	document.getElementById('cp_input_v_id').value = Math.round(cp_val * 100);
	document.getElementById('cp_input_r_id').value = cp_r;
	document.getElementById('cp_input_g_id').value = cp_g;
	document.getElementById('cp_input_b_id').value = cp_b;
}

function hexString()
{

	var rStr = cp_r.toString(16);
	if (rStr.length == 1)
		rStr = '0' + rStr;
	var gStr = cp_g.toString(16);
	if (gStr.length == 1)
		gStr = '0' + gStr;
	var bStr = cp_b.toString(16);
	if (bStr.length == 1)
		bStr = '0' + bStr;
	return ('#' + rStr + gStr + bStr).toUpperCase();
}

function hexStringChange(hexString)
{
	if(hexString == null || typeof(hexString) != "string")
	return false;

	if (hexString.substr(0, 1) == '#')
		hexString = hexString.substr(1);

	if(hexString.length != 6)
		return false;
	 
	cp_r = parseInt(hexString.substr(0, 2), 16);
	cp_g = parseInt(hexString.substr(2, 2), 16);
	cp_b = parseInt(hexString.substr(4, 2), 16);

	calculateHSV();
	cp_refresh_hsv();
	cp_refresh_rgb();
	cp_refresh_preview();
	cp_refresh_preview_static();

}

function cp_stringChange(element)
{
	if(element.value == parseInt(element.value) && element.value.length <= 3)
	{
		if( element.id == "cp_input_h_id" ||  element.id == "cp_input_s_id" ||  element.id == "cp_input_v_id")
		{
			cp_hue = (parseInt(document.getElementById('cp_input_h_id').value)%361);
			cp_sat = ((parseInt(document.getElementById('cp_input_s_id').value)%101)/100);
			cp_val = ((parseInt(document.getElementById('cp_input_v_id').value)%101)/100);
			calculateRGB();
		}

		if( element.id == "cp_input_r_id" ||  element.id == "cp_input_g_id" ||  element.id == "cp_input_b_id")
		{
			cp_r = parseInt(document.getElementById('cp_input_r_id').value)%256;
			cp_g = parseInt(document.getElementById('cp_input_g_id').value)%256;
			cp_b = parseInt(document.getElementById('cp_input_b_id').value)%256;
			calculateHSV();
		}
	}
	else
	{
		document.getElementById('cp_input_hex_id').value = hexString();
		document.getElementById('cp_input_h_id').value = cp_hue;
		document.getElementById('cp_input_s_id').value = Math.round(cp_sat * 100);
		document.getElementById('cp_input_v_id').value = Math.round(cp_val * 100);
		document.getElementById('cp_input_r_id').value = cp_r;
		document.getElementById('cp_input_g_id').value = cp_g;
		document.getElementById('cp_input_b_id').value = cp_b;
	}
	
	cp_refresh_hsv();
	cp_refresh_rgb();
	cp_refresh_preview();
	cp_refresh_preview_static();
}

function cp_save_old()
{

	for(x=cp_save_num; x > 0;x--)
	{

		if(x < cp_save_total && x > 1)
		{
			cp_save_array['cp_pallet_color_' + (x+1)] = cp_save_array['cp_pallet_color_' + x];
			cp_save_array['cp_pallet_color_' + x] = cp_save_array['cp_pallet_color_' + (x-1)];
			document.getElementById('cp_pallet_color_' + (x+1)).style.backgroundColor = document.getElementById('cp_pallet_color_' + (x)).style.backgroundColor;
			document.getElementById('cp_pallet_color_' + (x)).style.backgroundColor = document.getElementById('cp_pallet_color_' + (x-1)).style.backgroundColor;
		}
		else if(x == 1)
		{
			document.getElementById('cp_pallet_color_1').style.backgroundColor = hexString();
			cp_save_array['cp_pallet_color_1'] = hexString();
		}
		else if(x == cp_save_total)
		{
			cp_save_array['cp_pallet_color_' + x] = cp_save_array['cp_pallet_color_' + (x-1)];
			document.getElementById('cp_pallet_color_' + (x)).style.backgroundColor = document.getElementById('cp_pallet_color_' + (x-1)).style.backgroundColor;
		}
	}

	document.getElementById('cp_pallet_color_' + (cp_save_num)).style.backgroundImage = "none";
	if(cp_save_num < cp_save_total)
		cp_save_num++;

}

function cp_save()
{


for(i = 1; i < cp_save_total; i++)
{
document.getElementById('cp_pallet_color_' + i).style.backgroundImage = 'none';
}

document.getElementById('cp_pallet_color_' + cp_save_current).style.backgroundColor = hexString();

cp_save_array['cp_pallet_color_' + cp_save_current] = hexString();

if(cp_save_num < cp_save_total)
{
	if(cp_save_num == cp_save_current)
	{
		cp_save_num++;
	}
}
else
{
	cp_save_num = 1;
}

//cp_save_current %= (cp_save_total);
//cp_save_current++;

cp_save_current = cp_save_num;

for(i = 1; i <= cp_save_total; i++)
{
document.getElementById('cp_pallet_color_' + i).style.backgroundImage = 'none';
}

document.getElementById('cp_pallet_color_' + cp_save_current).style.backgroundImage = 'url(/images/cp/x.png)';

}

function cp_load(element)
{
	var temp = element.substr(16,(element.length - 16));

	if(temp == cp_save_current)
	{
		hexStringChange(cp_save_array['cp_pallet_color_' + cp_save_current]);
	}

	cp_save_current = temp;

	for(i = 1; i < cp_save_total; i++)
	{
	document.getElementById('cp_pallet_color_' + i).style.backgroundImage = 'none';
	}

	for(i = 1; i <= cp_save_total; i++)
	{
	document.getElementById('cp_pallet_color_' + i).style.backgroundImage = 'none';
	}

	document.getElementById('cp_pallet_color_' + cp_save_current).style.backgroundImage = 'url(/images/cp/x.png)';


}

function fixPNG(myImage) 
{
  if(!document.body.filters)
    return;
  var arVersion = navigator.appVersion.split("MSIE");
  var version = parseFloat(arVersion[1]);
  if(version < 5.5 || version >= 7)
    return;

  var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
  var imgStyle = "display:inline-block;" + myImage.style.cssText
  var strNewHTML = "<span " + imgID 
              + " style=\"" + "width:" + myImage.width 
              + "px; height:" + myImage.height 
              + "px;" + imgStyle + ";"
              + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
              + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
  myImage.outerHTML = strNewHTML
}

function calculateRGB()
{
	
	var tHue = (cp_hue / 60);
	var i = Math.floor(tHue);
	var f = tHue - i;
	var p = cp_val * (1 - cp_sat);
	var q = cp_val * (1 - cp_sat * f);
	var w = (1 - f);
	var t = cp_val * (1 - cp_sat * (1 - f));

	switch(i)
	{
		case 0:
			cp_r = Math.round(cp_val * 255); cp_g = Math.round(t * 255); cp_b = Math.round(p * 255);
			cp_rf = 255; cp_gf = Math.round(f * 255); cp_bf = 0;
			break;
		case 1:
			cp_r = Math.round(q * 255); cp_g = Math.round(cp_val * 255); cp_b = Math.round(p * 255);
			cp_rf = Math.round(w * 255); cp_gf = 255; cp_bf = 0;
			break;
		case 2:
			cp_r = Math.round(p * 255); cp_g = Math.round(cp_val * 255); cp_b = Math.round(t * 255);
			cp_rf = 0; cp_gf =255; cp_bf = Math.round(f * 255);
			break;
		case 3:
			cp_r = Math.round(p * 255); cp_g = Math.round(q * 255); cp_b = Math.round(cp_val * 255);
			cp_rf = 0; cp_gf = Math.round(w * 255); cp_bf = 255;
			break;
		case 4:
			cp_r = Math.round(t * 255); cp_g = Math.round(p * 255); cp_b = Math.round(cp_val * 255);
			cp_rf = Math.round(f * 255); cp_gf = 0; cp_bf = 255;
			break;
		case 5:
			cp_r = Math.round(cp_val * 255); cp_g = Math.round(p * 255); cp_b = Math.round(q * 255);
			cp_rf = 255; cp_gf = 0; cp_bf = Math.round(w * 255);
			break;
		case 6:
			cp_r = Math.round(cp_val * 255); cp_g = Math.round(p * 255); cp_b = 0;
			cp_rf = 255; cp_gf = 0; cp_bf = 0;
			break;
		default:
			break;
	}

	if(cp_val == 0 || cp_sat == 0)
	{
		cp_r = cp_val * 255;
		cp_g = cp_val * 255;
		cp_b = cp_val * 255;
	}

}

function calculateHSV()
{

	var max = Math.max(Math.max(cp_r, cp_g), cp_b);
	var min = Math.min(Math.min(cp_r, cp_g), cp_b);

	cp_val = max / 255;

	cp_sat = 0;
	if(max != 0)
	cp_sat = 1 - min/max;

	cp_hue = 0;
	if(min != max)
	{
		var delta = (max - min);
		if (cp_r == max)
			cp_hue = (cp_g - cp_b) / delta;
		else if (cp_g == max)
			cp_hue = 2 + ((cp_b - cp_r) / delta);
		else
			cp_hue = 4 + ((cp_r - cp_g) / delta);
		cp_hue *= 60;
		if(cp_hue <0)
		cp_hue += 360;

		cp_hue = Math.round(cp_hue);
	}

	if(cp_val == 0 || cp_sat == 0)
	{
		cp_r = cp_val * 255;
		cp_g = cp_val * 255;
		cp_b = cp_val * 255;
	}
	else
	{
		var tHue = (cp_hue / 60);
		var i = Math.floor(tHue);
		var f = tHue - i;
		var w = (1 - f);
		switch(i)
		{
			case 0:				cp_rf = 255; cp_gf = Math.round(f * 255); cp_bf = 0;
				break;
			case 1:				cp_rf = Math.round(w * 255); cp_gf = 255; cp_bf = 0;
				break;
			case 2:
				cp_rf = 0; cp_gf =255; cp_bf = Math.round(f * 255);
				break;
			case 3:
				cp_rf = 0; cp_gf = Math.round(w * 255); cp_bf = 255;
				break;
			case 4:
				cp_rf = Math.round(f * 255); cp_gf = 0; cp_bf = 255;
				break;
			case 5:
				cp_rf = 255; cp_gf = 0; cp_bf = Math.round(w * 255);
				break;
			case 6:
				cp_rf = 255; cp_gf = 0; cp_bf = 0;
				break;
			default:
				alert('Weird error!  Tell somebody.  Thomas perhaps.');
		}

	}

}

