/*
 * $Id: themes.js,v 1.2 2008/05/30 18:43:29 mripplin Exp $
 *
 * +------+   Copyright 2009, ICx Transportation Group
 * | ICx  |                   All Rights Reserved
 * +------+----------------------------------------------------------
 * This file is property of ICx Transportation Group.  Any unauthorized use or
 * duplication of this file, or removal of the header, constitutes
 * theft of intellectual property.
 *
 * This code customizes the map display based on the user's selected theme.
 * This allows colorblind or visually-impaired users to change the map colors
 * to be more accessible.
 */


// The default color for highway lines.
var hwyColor = '204,204,204';

/**
 * Generate an ArcXML <EXACT> element specifying a solid fill color.
 * @param value	The value to be matched by the <EXACT> element.
 * @param color	The fill color to be specified by the element.
 */
function createExactXML(value, color) {
  result =
  '<EXACT value="'+ value + '">' +
  '<SIMPLEPOLYGONSYMBOL fillcolor="' + color + '"' + 
  ' boundary="false" />' +
  '</EXACT>';
  return result;
}

/**
 * Generate a renderer for the highway layer, showing highways as lines
 * of the given color.
 * @param color	The color in which to render the highways.
 */
function createHwyXML(color)
{
	return '<SIMPLERENDERER><SIMPLELINESYMBOL color="' + color + '" width="2" captype="round" /></SIMPLERENDERER>';
}


/**
 * Generate a renderer for the traffic layer, using a different color
 * for each speed.
 * @param colors	An array mapping "SpeedBand" values to colors.
 */
function createTrafficXML(colors) {
	var xml = '<GROUPRENDERER>';
// DEBUG: Prints the link ids on top of the links
//	xml += '<SIMPLELABELRENDERER field="'
//  xml += document.SessionVariables.strDatabaseName.value + '.dbo.vwTrafficRenderInfo.LinkID"'
//  xml += ' >';
//  xml += '<TEXTSYMBOL font="Arial" fontsize="10" fontstyle="bold" fontcolor="0,0,0" antialiasing="true" />';
//  xml += '</SIMPLELABELRENDERER>';
	xml += '<SIMPLERENDERER>';
		xml += '<SIMPLEPOLYGONSYMBOL fillcolor="' + colors[1] + '" boundary="false" />';
	xml += '</SIMPLERENDERER>';
	xml += '<VALUEMAPRENDERER lookupfield="' + document.SessionVariables.strDatabaseName.value + '.dbo.vwTrafficRenderInfo.SpeedBand">';
	// The statement above requires a FORM named "SessionVariables" in the page that includes it.
	// alert(xml); 
	for (var i = 2; i<7; i++) {  // bug 1484 - adjusted to 7 - oh how I hate magic numbers
		xml += createExactXML(i-1, colors[i]);
		}
	xml += '</VALUEMAPRENDERER>';
	xml += '</GROUPRENDERER>';

  //alert(xml);
	return xml;
}

// The following arrays contain the speed-to-color mapping 
// and the renderers for each theme.

// Standard theme (for most users):

var colorsStandard = new Array();
colorsStandard[0] = '204,204,255'; // Light blue
colorsStandard[1] = '204,204,255'; // Light blue
colorsStandard[2] = '0,0,0'; // Black
colorsStandard[3] = '255,0,51'; // Red
colorsStandard[4] = '255,255,0'; // Yellow 
colorsStandard[5] = '0,255,0'; // Green 
colorsStandard[6] = '255,0,204'; // Magenta -- 1484

var themeStandard = new Array();
themeStandard["Traffic"] = colorsStandard;
themeStandard["Hwy"] = createHwyXML(hwyColor);

// High-contrast theme (for some low-vision users):

var colorsContrast = new Array();
colorsContrast[1] = colorsContrast[0] = '255,255,255'; // White
colorsContrast[2] = '255,0,51'; // Red 
colorsContrast[3] = '255,255,0'; // Yellow
colorsContrast[4] = '0,255,204'; // Aqua
colorsContrast[5] = '51,255,0'; // Light green 
colorsContrast[6] = '255,0,204'; // Magenta -- 1484

var themeContrast = new Array();
themeContrast["Traffic"] = colorsContrast;
themeContrast["Hwy"] = createHwyXML(hwyColor);
themeContrast["Water"] =
	'<SIMPLERENDERER>' +
	'	<SIMPLEPOLYGONSYMBOL fillcolor="64,128,192" filltransparency="1.0" />' +
	'</SIMPLERENDERER>';
	
themeContrast["Counties"] =
	'<SIMPLERENDERER>' +
		'<SIMPLEPOLYGONSYMBOL fillcolor="0,0,0" filltransparency="1.0" boundary="false" />' +
	'</SIMPLERENDERER>';
	
themeContrast["HighwayMask"] = 
	'<SIMPLERENDERER>' +
		'<SIMPLEPOLYGONSYMBOL fillcolor="0,0,0" boundary="false" />' +
	'</SIMPLERENDERER>';

// Red-green colorblind theme:

var colorsDeutan = new Array();
colorsDeutan[1] = colorsDeutan[0] = '255,255,255'; // White
colorsDeutan[2] = '0,0,0'; // Black 
colorsDeutan[3] = '255,0,51'; // Red 
colorsDeutan[4] = '255,255,0'; // Yellow 
colorsDeutan[5] = '51,0,255'; // Blue 
colorsDeutan[6] = '255,0,204'; // Magenta -- 1484

var themeDeutan = new Array();
themeDeutan["Traffic"] = colorsDeutan;
themeDeutan["Hwy"] = createHwyXML(hwyColor);

// Blue-yellow colorblind theme:

var colorsTritan = new Array();
colorsTritan[1] = colorsTritan[0] = '255,255,255'; // White
colorsTritan[2] = '255,204,255'; // Black 
colorsTritan[3] = '255,0,51'; // Red 
colorsTritan[4] = '0,255,204'; // Aqua 
colorsTritan[5] = '51,0,255'; // Blue 
colorsTritan[6] = '255,0,204'; // Magenta -- 1484

var themeTritan = new Array();
themeTritan["Traffic"] = colorsTritan;
themeTritan["Hwy"] = createHwyXML(hwyColor);
themeTritan["Water"] =
	'<SIMPLERENDERER>' +
	'	<SIMPLEPOLYGONSYMBOL fillcolor="0,0,0" filltransparency="1.0" />' +
	'</SIMPLERENDERER>';


var themes = new Array();
themes["standard"] = themeStandard;
themes["contrast"] = themeContrast;
themes["deutan"] = themeDeutan;
themes["tritan"] = themeTritan;

/**
 * Should the named layer be visible in the given theme?
 */
function themeVisible(themeName, layerName)
{
	if (themeName == "contrast")
		if (layerName == "Streets" || layerName == "Cities")
			return false;
	return true;
}

/**
 * Add extra parameters to the image element based on the theme.
 */
function themeImageParams(themeName) {
	//if (themeName == "contrast")
	//	return '<BACKGROUND color="0,0,0" />';
	return '';
}

/**
 * Return the traffic-speed-to-color mapping for the named theme.
 */
function themeColors(themeName) {
	if (themes[themeName] && themes[themeName]["Traffic"])
		return themes[themeName]["Traffic"];
	else return colorsStandard;
}

/**
 * Write the renderer for the named theme and layer.
 * Returns an ArcXML string.
 */
function writeThemeXML(themeName, layerName)
{
	if (themes[themeName]) {
		if (layerName.indexOf("Traffic") == 0) {
			if (themes[themeName]["Traffic"])
				return createTrafficXML(themes[themeName]["Traffic"]);
		}
		else {
			if (layerName.indexOf("Water") == 0)
				layerName = "Water";
			else if (layerName.indexOf("Hwy") == 0 && layerName.indexOf("Shields") == -1)
				layerName = "Hwy";
			else if (layerName.indexOf("HighwayMask") == 0)
				layerName = "HighwayMask";
				
			var result = "";
			if (themes[themeName][layerName])
				result = themes[themeName][layerName];

			if (typeof result != "undefined")
				return result;
		}
	}
	return "";
}
