/* functions to handle the color editor */

function ChangeColorName()
{
	if (selected < 0) return;
	var colorname = document.getElementById("colorname").value;
	colors[selected].name = colorname;
}

function ChangeColorValue()
{
	if (selected < 0) return;
	if (colors[selected].isbase)
	{
		var r = document.getElementById("red").value;
		var g = document.getElementById("green").value;
		var b = document.getElementById("blue").value;
		colors[selected].Update(r, g, b);
		UpdateSwatch(selected, r, g, b);
	}
	else
	{
		var formula = document.getElementById("colorformula").value;
		colors[selected].UpdateFormula(formula);
	}
	UpdateAllColors();
}

function UpdateColor(c, name, r, g, b)
{
	c.name = name;
	c.Update(r, g, b);
	c.SetBase();
	UpdateSwatch(c.id, r, g, b);
}

function UpdateDerivedColor(c, name, formula)
{
	c.name = name;
	c.SetDerived();
	c.UpdateFormula(formula);
	UpdateSwatch(c.id, c.r, c.g, c.b);
}

function SetBase()
{
	if (selected < 0) return;
	document.getElementById("basecolorcontrols").style.display = "block";
	document.getElementById("derivedcolorcontrols").style.display = "none";
	colors[selected].SetBase();
}

function SetDerived()
{
	if (selected < 0) return;
	document.getElementById("basecolorcontrols").style.display = "none";
	document.getElementById("derivedcolorcontrols").style.display = "block";
	colors[selected].SetDerived();
}

function SelectColor(i)
{
	if (selected > -1)
	{
		document.getElementById("color" + selected).className = "colorswatch";
	}
	document.getElementById("coloreditor").style.display = "block";
	selected = i;
	document.getElementById("colorname").value = colors[i].name;
	document.getElementById("red").value = colors[i].r;
	document.getElementById("green").value = colors[i].g;
	document.getElementById("blue").value = colors[i].b;
	document.getElementById("colorformula").value = colors[i].formula;
	if (colors[i].isbase == 1) SetBase();
	else SetDerived();
	document.getElementById("color" + selected).className = "colorswatch selected";
}

function CreateColor()
{
	var c = AddColor();
	AddColorSwatch(c);
	return c;
}

function NewColor()
{
	CreateColor();
}

