/*
Use but give credit, usual deal
Too lazy to write stuff up here, GPL or whatever its called
*/

var colorPresets = new Array();

var debug = false;

var colorPickerTop;
var colorPickerBottom;

var topColorRTot;
var topColorGTot;
var topColorBTot;

var topColorRInc;
var topColorGInc;
var topColorBInc;

var bottomColorRTot;
var bottomColorGTot;
var bottomColorBTot;

var bottomColorRInc;
var bottomColorGInc;
var bottomColorBInc;

var imagePlaying;
var imageStoppped;

function swapPresets()
{
	$('#colorPresetList').slideFadeToggle("fast");
}

function setBgColor()
{	
	colorPickerTop.style.color		= colorPickerTop.style.backgroundColor;
	colorPickerBottom.style.color 	= colorPickerBottom.style.backgroundColor;;

	document.body.style.background 	= "-moz-linear-gradient(top, " + colorPickerTop.style.backgroundColor + ",  " + colorPickerBottom.style.backgroundColor + ") no-repeat " + colorPickerBottom.style.backgroundColor;
	document.body.style.background 	= "-webkit-gradient(linear, left top, left bottom, from(" + colorPickerTop.style.backgroundColor + "), to(" + colorPickerBottom.style.backgroundColor + ")) no-repeat " + colorPickerBottom.style.backgroundColor;
	document.body.style.filter		= "progid:DXImageTransform.Microsoft.gradient(startColorstr='" + colorPickerTop.style.backgroundColor + "', endColorstr='" + colorPickerBottom.style.backgroundColor + "')";

	if (debug)
	{
		document.getElementById("debug").innerHTML = colorPickerTop.style.backgroundColor + ", " + colorPickerBottom.style.backgroundColor;

		/*document.getElementById("debug").innerHTML = "T: " + topColorRInc + ", " + topColorRTot;
		document.getElementById("debug").innerHTML += "<br/>";
		document.getElementById("debug").innerHTML += "TOP: " + topColorR + ", " + topColorG + ", " + topColorB;
		document.getElementById("debug").innerHTML += "<br/>";
		document.getElementById("debug").innerHTML += "BOT: " + bottomColorR + ", " + bottomColorG + ", " + bottomColorB;*/
	}
}

function swapColor()
{
	var tempColor 								= colorPickerTop.style.backgroundColor;
	colorPickerTop.style.backgroundColor 		= colorPickerBottom.style.backgroundColor;
	colorPickerBottom.style.backgroundColor 	= tempColor;
	setBgColor();
}

function addPresetColor(name, top, bottom)
{
	colorPresets[name] 				= new Array();
	colorPresets[name]["top"] 		= top;
	colorPresets[name]["bottom"] 	= bottom;
}

function initSetTheColor()
{
	colorPickerTop 		= document.getElementById("colorPickerTop");
	colorPickerBottom 	= document.getElementById("colorPickerBottom");
	
	bottomColorRInc = 0.013;
	bottomColorGInc = 0.027;
	bottomColorBInc = 0.041;

	bottomColorRTot = 0;
	bottomColorGTot = 0;
	bottomColorBTot = 0;

	topColorRInc = 0.024;
	topColorGInc = 0.048;
	topColorBInc = 0.053;
	
	topColorRTot = 0;
	topColorGTot = 0;
	topColorBTot = 0;
	
	// Preload images
	imageStopped = new Image(50,50);
	imagePlaying = new Image(50,50);
	imageStopped.src = "Images/ButtonSwap/Cycle.png";
	imagePlaying.src = "Images/ButtonSwap/Cycle.gif";
	
	addPresetColor("Default", "#23314E", "#A2A8B4");
	addPresetColor("Sunset",  "#EB6517", "#F2FF80");
	addPresetColor("Winter",  "#AFB5C7", "#555555");
	addPresetColor("Candy",   "#27AB13", "#B43131");
}

var cycling = false;
var intervalId;
var hexLovingBrowser;

var topColor;
var topColorR;
var topColorG;
var topColorB;

var bottomColor;
var bottomColorR;
var bottomColorG;
var bottomColorB;

// HEX 2 component
function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}

// RGB 2 component
function RgbToR(h) {return parseInt(h.substring(h.indexOf("(") + 1, h.indexOf(",")))}
function RgbToG(h) {return parseInt(h.substring(h.indexOf(",") + 1, h.indexOf(",", h.indexOf(",") + 1)))}
function RgbToB(h) {return h.substring(h.lastIndexOf(",") + 1, h.indexOf(")"))}

// Compnent to Hex (everybody loves IE)
function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

function cycleColor()
{
	cycling = !cycling;
	if (cycling)
	{
		document.getElementById("cycleImage").src = imagePlaying.src;
		
		topColor 	= colorPickerTop.style.backgroundColor;
		bottomColor = colorPickerBottom.style.backgroundColor;
	
		hexLovingBrowser = (topColor.search("#") != -1);

		// IE
		if (hexLovingBrowser)
		{
			topColorR 		= HexToR(topColor);
			topColorG 		= HexToG(topColor);
			topColorB 		= HexToB(topColor);
			bottomColorR 	= HexToR(bottomColor);
			bottomColorG 	= HexToG(bottomColor);
			bottomColorB 	= HexToB(bottomColor);
		}
		else
		{
			topColorR 		= RgbToR(topColor);
			topColorG 		= RgbToG(topColor);
			topColorB 		= RgbToB(topColor);
			bottomColorR 	= RgbToR(bottomColor);
			bottomColorG 	= RgbToG(bottomColor);
			bottomColorB 	= RgbToB(bottomColor);
		}

		intervalId = setInterval(cycleColorRepeater, 100);
	}
	else
	{
		clearInterval(intervalId);
		document.getElementById("cycleImage").src = imageStopped.src;
	}
}

function minValue(value)
{
	if (value > Math.PI * 2)
		value -= Math.PI * 2;
	
	return value;
}

function cycleColorRepeater()
{
	topColorRTot = minValue(topColorRTot + topColorRInc);
	topColorGTot = minValue(topColorGTot + topColorGInc);
	topColorBTot = minValue(topColorBTot + topColorBInc);
	
	topColorR = parseInt(Math.sin(topColorRTot) * 125 + 127);
	topColorG = parseInt(Math.sin(topColorGTot) * 125 + 127);
	topColorB = parseInt(Math.sin(topColorBTot) * 125 + 127);
	
	bottomColorRTot = minValue(bottomColorRTot + bottomColorRInc);
	bottomColorGTot = minValue(bottomColorGTot + bottomColorGInc);
	bottomColorBTot = minValue(bottomColorBTot + bottomColorBInc);
	
	bottomColorR = parseInt(Math.sin(bottomColorRTot) * 125 + 127);
	bottomColorG = parseInt(Math.sin(bottomColorGTot) * 125 + 127);
	bottomColorB = parseInt(Math.sin(bottomColorBTot) * 125 + 127);	
	
	if (hexLovingBrowser)
	{
		topColor = "#" + RGBtoHex(topColorR, topColorG, topColorB);
		bottomColor = "#" + RGBtoHex(bottomColorR, bottomColorG, bottomColorB);
	}
	else
	{
		topColor = "rgb(" + topColorR + "," + topColorG + "," + topColorG + ")";
		bottomColor = "rgb(" + bottomColorR + "," + bottomColorG + "," + bottomColorG + ")";
	}
	colorPickerTop.style.backgroundColor 	= topColor;
	colorPickerTop.style.color				= topColor;

	setBgColor();
}

function setPresetColor(preset)
{
	colorPickerTop.style.backgroundColor 	= colorPresets[preset]["top"];
	colorPickerBottom.style.backgroundColor = colorPresets[preset]["bottom"];
	setBgColor();
}


