var curSelectionArray = new Array();
var curColor = new Array();
var curColorName = new Array();

function tint(layer, color, setColor, arrayIndex, colorName)
{
	//var info = Concept.registry.lastDisplayInfo;
	//swfId = info.embedElementId;
	
	
	//loop through all the views to keep them in sync
	for(i=0; i < viewInfo.length; i++){
		conceptretail.ConfigurableImageRenderer.setLayerColor(swfId, layer, "0x" + color, [varName,viewInfo[i].inventoryId,"MAIN"]);
		conceptretail.ConfigurableImageRenderer.setLayerColor(swfId, layer, "0x" + color, [viewInfo[i].inventoryId,"THUMB"]);
				
		if(isExternalMag){
			conceptretail.ConfigurableImageRenderer.setLayerColor(extSwfId, layer, "0x" + color, [varName, viewInfo[i].inventoryId,"ZOOM"]);
		}
		else{
			conceptretail.ConfigurableImageRenderer.setLayerColor(swfId, layer, "0x" + color, [varName, viewInfo[i].inventoryId,"ZOOM"]);
		}
	}
		
	//set the color display label
	if(colorName != undefined)
		document.getElementById(layer + "CurrentColor").innerHTML = colorName;
	
	if(setColor){
	
		if(curSelectionArray[layer] == undefined)
			curSelectionArray[layer] = 0;
	
		//set the previous selection back to the non-selected style
		document.getElementById("border" + layer + curSelectionArray[layer]).className = 'swatchBorder';
		
		//set the current selection so we can undo it later
		curSelectionArray[layer] = arrayIndex;
		curColor[layer] = color;
		curColorName[layer] = colorName;
	}
	//set the indicator on the current swatch
	document.getElementById("border" + layer + arrayIndex).className = 'swatchBorderCurrent';
}



function resetColor(layer, arrayIndex)
{
	tint(layer, curColor[layer], false, arrayIndex, curColorName[layer]);
	document.getElementById("border" + layer + arrayIndex).className = 'swatchBorderCurrent';
	
	if(arrayIndex != curSelectionArray[layer]){
		document.getElementById("border" + layer + arrayIndex).className = 'swatchBorder';
		//tint(layer, curColor[layer], true, arrayIndex, curColorName[layer]);
	}
}

function setDefaults(){
		
	for (var layer in attributes){
		tint(layer, attributes[layer].colorArray[0].hex, true, 0, attributes[layer].colorArray[0].name);
	}
}

					
function drawLayerPalette(layerName, layerLabel, colorArray){

	document.write("<div class=\"swatchBox\">");
	
	document.write("<span class=\"swatchHeadingText\">" + layerLabel + ":</span>");
	document.write("<span id=\"" + layerName + "CurrentColor\" class=\"swatchColorText\">" + colorArray[0].name + "</span><br clear=\"all\"/>");


	for(var i=0; i < colorArray.length; i++){
		var layerNameIndexed = layerName + i;
			
		if(i==0)
			thisSwatchStyle = "swatchBorderCurrent";
		else
			thisSwatchStyle = "swatchBorder";
											
		document.write("<div id=\"border" + layerNameIndexed + "\" class=\"" + thisSwatchStyle + "\">");
		
			document.write("\n<div id=\"" + layerNameIndexed + "\" class=\"swatchDiv\"");
			
			//write the onclick handler
			document.write("\n onclick=\"tint('" + layerName + "', '");
			document.write(colorArray[i].hex);
			document.write("', true," + i + ", '" + colorArray[i].name + "');\"");

			//write the onmouseover handler
			document.write("\n onmouseover=\"tint('" + layerName + "', '");
			document.write(colorArray[i].hex);
			document.write("', false, " + i + ", '" + colorArray[i].name + "');\"");

			//write the onmouseout handler
			document.write("\n onmouseout=\"resetColor('" + layerName + "', " + i + ");\"");
			
			document.write(">\n");
			document.write("<img src=\"/images/spacer.gif\" class=\"spacerImg\" >");
			document.write("</div>\n");
			
			document.getElementById(layerNameIndexed).style.backgroundColor = "#" + colorArray[i].hex;
			
		document.write("</div>");
			

	}
	
	document.write("<br clear=\"all\"></div>");
	
	
	document.write("<br clear=\"all\" />");

}

var curFontColor = 0;
var huhVar;
function changeFontColor(colorId, huh, saveSetting){
	
	if(saveSetting){
		curFontColor = colorId;
		huhVar = huh;
	}
	
	Concept.Personalization.selectColor(colorId, huh );
	return false;
}

function resetFontColor(){
	changeFontColor(curFontColor, huhVar, true);
}