var oSwatchColors = new Object();
var oPaletteBuilder;
var aSelectedSwatches = new Array();
var sPaletteBuilderDescription;
var sSubDevelopmentID;
var sPdfUrl;

function SwatchColor( sID, sLayerID, sColorID, sColorName, sColor, sApplication )
{
	this.ID = sID;
	this.LayerID = sLayerID;
	this.ColorID = sColorID;
	this.ColorName = sColorName;
	this.Color = sColor;
	this.Application = sApplication;
	
	this.Layer = document.getElementById( this.LayerID );
	this.Layer.onmouseover = onMouseOver;
	this.Layer.onmouseout = onMouseOut;
	this.Layer.onclick = onClick;
}

function registerPaletteBuilder( sID, sSubDevelopment, sDownloadUrl )
{
	sSubDevelopmentID = sSubDevelopment;
	
	oPaletteBuilder = document.getElementById( sID );
	
	sPaletteBuilderDescription = oPaletteBuilder.innerHTML;
	
	sPdfUrl = sDownloadUrl;
	
	window.onscroll = onWindowScroll;
}

function onWindowScroll()
{
	var nMinTop = 0;
	
	oPaletteBuilder.style.top = Math.max( nMinTop, document.body.scrollTop - 600 );
}

function registerSwatchColor( sID, sLayerID, sColorID, sColorName, sColor, sApplication )
{
	var oSwatchColor = new SwatchColor( sID, sLayerID, sColorID, sColorName, sColor, sApplication );

	oSwatchColors[ sLayerID ] = oSwatchColor;
}

function renderSelectedSwatches()
{
	var sChipOrderLink = 'http://www.dunnedwards.com/retail/content.asp?content=80';
	var sDownloadLink = sPdfUrl + '?subdevelopment=' + sSubDevelopmentID + '&colors=';
	
	oPaletteBuilder.innerHTML = sPaletteBuilderDescription;
	
	for( var nIndex = 0; nIndex < aSelectedSwatches.length; nIndex++ )
	{
		var oSwatchColor = aSelectedSwatches[ nIndex ];
		var sSwatchCaption = oSwatchColor.ColorID + ' ' + oSwatchColor.ColorName + ' - ' + oSwatchColor.Application;
		var nColorChip = nIndex + 1;
		
		oPaletteBuilder.innerHTML += '<div class="paletteSwatch" style="border-left:solid 48px ' + oSwatchColor.Color + ';">' + sSwatchCaption + '</div>';
		
		sChipOrderLink += '&chip_amount' + nColorChip + '=1&chip_number' + nColorChip + '=' + oSwatchColor.ColorID;
		
		if( nIndex != 0 )
			sDownloadLink += ',';
			
		sDownloadLink += oSwatchColor.ID;
	}
	
	oPaletteBuilder.innerHTML += '<a href="' + sDownloadLink + '" target="_blank">Download color scheme</a>';
	oPaletteBuilder.innerHTML += '<a href="' + sChipOrderLink + '" target="_blank">Order color chips</a>';
	
}

function onClick()
{	
	var oSwatchColor = oSwatchColors[ this.id ];
	var aNewSwatches = new Array();
	var bAdded = false;
	
	for( var nIndex = 0; nIndex < aSelectedSwatches.length; nIndex++ )
	{
		var oSelectedSwatchColor = aSelectedSwatches[ nIndex ];
		
		if( oSelectedSwatchColor.Application == oSwatchColor.Application )
		{
			bAdded = true;
			aNewSwatches.push( oSwatchColor );
		}
		else
		{
			aNewSwatches.push( oSelectedSwatchColor );
		}		
	}
	
	if( !bAdded )
		aNewSwatches.push( oSwatchColor );
	
	aSelectedSwatches = aNewSwatches;
	
	renderSelectedSwatches();
}

function onMouseOver()
{
	this.style.backgroundColor = '#dddddd';
	document.body.style.cursor = 'pointer';
	
}
function onMouseOut()
{
	this.style.backgroundColor = '#eeeeee';
	document.body.style.cursor = 'auto';
}

function findAssociation( sCity, sState, sZip )
{	
	var oCity = document.getElementById( sCity );
	var oState = document.getElementById( sState );
	var oZip = document.getElementById( sZip );
	
	if( oZip )
	{
		location.href = '/article/content/informationcenter/color-arkhomeownersassociationcolorarchive.html?city=' + oCity.value + '&state=' + oState.value + '&zip=' + oZip.value;
	}
	
	return false;
}


function onAssociationKeyDown( sCity, sState, sZip, oEvent )
{
	if( oEvent.keyCode == 13 )
	{
		oEvent.returnValue=false;
		oEvent.cancel = true;
		
		var oCity = document.getElementById( sCity );
		var oState = document.getElementById( sState );
		var oZip = document.getElementById( sZip );
		
		if( oZip )
		{
			location.href = '/article/content/informationcenter/color-arkhomeownersassociationcolorarchive.html?city=' + oCity.value + '&state=' + oState.value + '&zip=' + oZip.value;
		}
		
		return false;
	}	
}
