// Variable to record whether towns are currently displayed
var townsOn = true;

// Fixed variables for CA display
var origCAColour = "#dcda00"; // Meadow
var highlightFill = "#f0a800"; // Honey
var selectedFill = "#e00741"; // Rosehip
var highlightSelectedFill = "#fc8505"; // Nectar


var lastCA = null;
var lasthighlightCA = null;

var fieldHeaders1 = "";
var fieldHeaders2 = "";

// set clicked button icon to set mode
function setOverPic(fName,tName) {
	var svgobj = svgMapDoc.getElementById(fName);
	if (fName=="zoomInImage") {
		svgobj.setAttribute("xlink:href","images/zoominaqua.gif");
	} else if (fName=="zoomOutImage") {
		svgobj.setAttribute("xlink:href","images/zoomoutaqua.gif");
	} else if (fName=="zoomFullImage") {
		svgobj.setAttribute("xlink:href","images/zoomfullextentaqua.gif");
	}
}

// set clicked button icon to set mode
function setOutPic(fName,tName) {
	var svgobj = svgMapDoc.getElementById(fName);
	if (fName=="zoomInImage") {
		svgobj.setAttribute("xlink:href","images/zoomingrey.gif");
	} else if (fName=="zoomOutImage") {
		svgobj.setAttribute("xlink:href","images/zoomoutgrey.gif");
	} else if (fName=="zoomFullImage") {
		svgobj.setAttribute("xlink:href","images/zoomfullextentgrey.gif");
	}
}

// a click on a county or a selection from the dropdown calls this function
// Text objects are written the dataDisplay in the SVG page
function setPoly(int1,int2) {
	currentCO = int2;

	var svgobj = svgMapDoc.getElementById("text1");
	var child = svgobj.getFirstChild();

	child.setData(co0203[int2][1]);
	svgobj.setAttribute("font-size",15);

	Yvar = countyYear.length;

	if (Yvar == 4) {
		fY = countyYear.substring(0,2);
		sY = countyYear.substring(2,4);
		yearText = ["f/y 20" + fY + "/" + sY];

		fieldHeaders1 = ["Agreement", "", "% area meeting"];
		fieldHeaders2 = ["area (ha)", "Total cost (£)", "PSA Target"];
	} else {
		fY = countyYear.substring(0,2);
		sY = countyYear.substring(2,4);
		tY = countyYear.substring(4,6);
		oY = countyYear.substring(6,8);
		yearText = ["f/y 20" + fY + "/" + sY + " to 20" + tY + "/" + oY];

		fieldHeaders1 = ["increase in", "increase to", "increase to % area"];
		fieldHeaders2 = ["area (ha)", "total cost (£)", "meeting PSA Target"];
	}

	if (svgMapDoc.getElementById("header99")) {
		svgMapDoc.getElementById("header99").getFirstChild().setData(yearText);
	} else {
		var node=svgMapDoc.createElement("text");
		node.setAttribute("x",10);
		node.setAttribute("y",40);
		node.setAttribute("id","header99");
		node.setAttribute("class","headerText");
		node.setAttribute("text-anchor","left");

		var textNode;
		textNode=svgMapDoc.createTextNode(yearText);

		node.appendChild(textNode);
		displayObj=svgMapDoc.getElementById("tableDisplay");
		displayObj.appendChild(node);
	}

	var xval;
	var yval;
	var eltName;
	var m;

	// write headers in table
	for (a=0; a<=1; a++) {
		for (n=0; n<=2; n++) {
			xval = 0;
			if (n==0) xval = 100;
			else if (n==1) xval = 200;
			else if (n==2) xval = 310;
			
			yval = 90;
			if (a==1) yval = 105;
			//if (a==2) yval = 70;
			
			eltName = "header" + n + a;
	
			if (svgMapDoc.getElementById(eltName)) {
				if (a == 0) svgMapDoc.getElementById(eltName).getFirstChild().setData(fieldHeaders1[n]);
				else svgMapDoc.getElementById(eltName).getFirstChild().setData(fieldHeaders2[n]);
			} else {
	
				var node=svgMapDoc.createElement("text");
				node.setAttribute("x",xval);
				node.setAttribute("y",yval);
				node.setAttribute("id",eltName);
				node.setAttribute("class","tableColumnHeaderText");

				var textNode;

				if (a==0) textNode=svgMapDoc.createTextNode(fieldHeaders1[n]);
				else textNode=svgMapDoc.createTextNode(fieldHeaders2[n]);

				node.appendChild(textNode);
				displayObj=svgMapDoc.getElementById("tableDisplay");
				displayObj.appendChild(node);
			}
		}	
	}
	
	var rowHeaders = ["WES","ESA","CSS"];
	// First element to write is array value 3
	var counter = 2;

	// Now write the values
	for (n=0; n<=2; n++) {
		// 1st loop through = WES, 2nd = ESA, 3rd = CSS
		xval = 0;		
		yval = (n*30) + 140;
			
		// Write the row header
		eltName = "row" + n;

		if (svgMapDoc.getElementById(eltName)) {
			svgMapDoc.getElementById(eltName).getFirstChild().setData(rowHeaders[n]);
		} else {
			var node=svgMapDoc.createElement("text");
			node.setAttribute("x",xval);
			node.setAttribute("y",yval);
			node.setAttribute("class","tableRowHeaderText");
			node.setAttribute("id",eltName);
			var textNode=svgMapDoc.createTextNode(rowHeaders[n]);
			node.appendChild(textNode);
			displayObj=svgMapDoc.getElementById("tableDisplay");
			displayObj.appendChild(node);
		}

		for (m=0; m<=2; m++) {

			if (m==0) xval = 100;
			else if (m==1) xval = 200;
			else if (m==2) xval = 310;
	
			eltName = "text" + counter;

			if (countyYear == "0203") {
				var dataText = co0203[int2][counter];
			} else if (countyYear == "0304") {
				var dataText = co0304[int2][counter];
			} else if (countyYear == "0405") {
				var dataText = co0405[int2][counter];
			} else if (countyYear == "0506") {
				var dataText = co0506[int2][counter];
			} else if (countyYear == "02030304") {
				var dataText = co02030304[int2][counter];
			} else if (countyYear == "02030405") {
				var dataText = co02030405[int2][counter];
			} else if (countyYear == "02030506") {
				var dataText = co02030506[int2][counter];
			} else if (countyYear == "03040405") {
				var dataText = co03040405[int2][counter];
			} else if (countyYear == "03040506") {
				var dataText = co03040506[int2][counter];
			} else if (countyYear == "04050506") {
				var dataText = co04050506[int2][counter];
			}

			dataText = commaFormatted(dataText);
			//if ((m==1) && (dataText!="-") && (dataText!="")) dataText = "£ " + dataText;
			if ((m==2) && (dataText!="-") && (dataText!="")) dataText = dataText + " %";		
			
			if (svgMapDoc.getElementById(eltName)) {
				svgMapDoc.getElementById(eltName).getFirstChild().setData(dataText);
			} else {
				var node=svgMapDoc.createElement("text");
				node.setAttribute("x",xval);
				node.setAttribute("y",yval);
				node.setAttribute("class","tableCellText");
				node.setAttribute("id",eltName);
				var textNode=svgMapDoc.createTextNode(dataText);
				node.appendChild(textNode);
				displayObj=svgMapDoc.getElementById("tableDisplay");
				displayObj.appendChild(node);
			}
			counter += 1;
		}
	}	
	
	if (countyYear == "0203") {
		changeStyle(co0203[int2][0]);
	} else if (countyYear == "0304") {
		changeStyle(co0304[int2][0]);
	} else if (countyYear == "0405") {
		changeStyle(co0405[int2][0]);
	} else if (countyYear == "0506") {
		changeStyle(co0506[int2][0]);
	} else if (countyYear == "02030304") {
		changeStyle(co02030304[int2][0]);
	} else if (countyYear == "02030405") {
		changeStyle(co02030405[int2][0]);
	} else if (countyYear == "02030506") {
		changeStyle(co02030506[int2][0]);
	} else if (countyYear == "03040405") {
		changeStyle(co03040405[int2][0]);
	} else if (countyYear == "03040506") {
		changeStyle(co03040506[int2][0]);
	} else if (countyYear == "04050506") {
		changeStyle(co04050506[int2][0]);
	}

	if (int1==0) resetSelList();
}

// Function to increase the map extent to the extent specified by the argument 
function changeViewBox(extentNew) {
	var myMap = svgMapDoc.getElementById('map');
	myMap.setAttribute("viewBox",extentNew);
}

// highlight polygon and place polygon name as box title
function over(int1,int2) {
	eltName = "govregNameDisplay";
	if (svgMapDoc.getElementById(eltName)) {
			svgMapDoc.getElementById(eltName).getFirstChild().setData(co0405[int2][1]);
	} else {
		var node=svgMapDoc.createElement("text");
		node.setAttribute("x",15);
		node.setAttribute("y",5);
		node.setAttribute("class","mapText");
		node.setAttribute("id",eltName);
		var textNode=svgMapDoc.createTextNode(co0405[int2][1]);
		node.appendChild(textNode);
		displayObj=svgMapDoc.getElementById("govRegNames");
		displayObj.appendChild(node);
	}
	// Highlight the CA that has the current mouseover
	highlightCA(co0405[int2][0]);
}

// Remove the display text
function out(int1,int2){
	// We only want to implement the onmouseout when the mouse isn't over
	// the map. Everything else will be covered by onmouseover resettings
	eltName = "govregNameDisplay";
	if (svgMapDoc.getElementById(eltName)) {
			svgMapDoc.getElementById(eltName).getFirstChild().setData(" ");
	}

	if (lasthighlightCA != null) {
		if (lasthighlightCA != lastCA) lasthighlightCA.getStyle().setProperty("fill", origCAColour);
 	}
}

// Function to increase the display colour of the selected 
// CA polygon
function changeStyle(caNum) {
	if (lastCA != null) {
		lastCA.getStyle().setProperty('fill', origCAColour);
 	}
	
	var caID = "CO"+caNum;
	var caObj = svgMapDoc.getElementById(caID);

	if (caObj!=null) {
		caObj.getStyle().setProperty('fill',selectedFill);
		lastCA = caObj;
	}
}

// Function to increase the display colour of the selected 
// CA polygon to highlight the polygon
function highlightCA(caNum) {
	// Only reset the highlighted CA if it is NOT the selected CA. 
	if (lasthighlightCA != null) {
		if (lasthighlightCA != lastCA) {
			lasthighlightCA.getStyle().setProperty("fill", origCAColour);
		} else {
			lasthighlightCA.getStyle().setProperty("fill", selectedFill);
		}
 	}

	var caID = "CO"+caNum;
	var caObj = svgMapDoc.getElementById(caID);

	// Only highlight the CA if it is NOT the selected CA. 
	if (caObj != null) {
		if (caObj != lastCA) {
			caObj.getStyle().setProperty('fill', highlightFill);
		} else {
			caObj.getStyle().setProperty('fill', selectedFill);
		}
		lasthighlightCA = caObj;
	}
}
// Function to strip spaces from a string
function ignoreSpaces(inString) {
		var temp = "";
		inString = "" + inString;
		splitstring = inString.split(" ");
		for(i = 0; i < splitstring.length; i++)
			temp += splitstring[i];
		return temp;
}

function zoomToFullExtent() {
	//if (isNav) SVGDoc = document.embeds[0].getSVGDocument();
	//else SVGDoc = document.camap.getSVGDocument();
	var myMap = svgMapDoc.getElementById("map");
	myMap.setAttribute("viewBox","0 0 573301 652261");
}
		
// Function created by Alan Smith of ONS 
function Zoom(how)	{
	var MapWindow = svgMapDoc.getElementById('map');
	var MyViewBox = MapWindow.getAttribute('viewBox');
	BoxArray = MyViewBox.split(" ");
	minx = BoxArray[0];
	miny = BoxArray[1];
	width = BoxArray[2];
	height = BoxArray[3];
	numminx = parseInt(minx);
	numminy = parseInt(miny);
	numwidth = parseInt(width);
	numheight = parseInt(height);
	halfwidth = numwidth/2;
	halfheight = numheight/2;
	var midpointx = numminx+halfwidth;
	var midpointy = numminy+halfheight;
	if (how=="in")	{
		newwidth = (numwidth/100)*66;
		newheight = (numheight/100)*66;
		newminx = midpointx-(newwidth/2);
		newminy = midpointy-(newheight/2);
		MapWindow.setAttribute('viewBox',newminx+" "+newminy+" "+newwidth+" "+newheight);
	}
	if (how=="out")	{
		newwidth = (numwidth/100)*133;
		newheight = (numheight/100)*133;
		newminx = midpointx-(newwidth/2);
		newminy = midpointy-(newheight/2);
		MapWindow.setAttribute('viewBox',newminx+" "+newminy+" "+newwidth+" "+newheight);
	}
}
// Function created by Alan Smith of ONS
function pan(direction)	{
	var MyDirection=direction;
	var MapWindow = svgMapDoc.getElementById('map');
	var MyViewBox = MapWindow.getAttribute('viewBox');
	BoxArray = MyViewBox.split(" ");
	minx = BoxArray[0];
	miny = BoxArray[1];
	width = BoxArray[2];
	height = BoxArray[3];
	numminx = parseInt(minx);
	numminy = parseInt(miny);
	numwidth = parseInt(width);
	numheight = parseInt(height);
	var shiftx = (numwidth/100)*16;
	var shifty = (numheight/100)*16;
	if (MyDirection=="east")	{
		var moveeast = numminx+shiftx;
		MapWindow.setAttribute('viewBox',moveeast+" "+numminy+" "+numwidth+" "+numheight);
	}
	if (MyDirection=="west")	{
		var movewest = numminx-shiftx;
		MapWindow.setAttribute('viewBox',movewest+" "+numminy+" "+numwidth+" "+numheight);
	}
	if (MyDirection=="north")	{
		var movenorth = numminy-shifty;
		MapWindow.setAttribute('viewBox',numminx+" "+movenorth+" "+numwidth+" "+numheight);
	}
	if (MyDirection=="south")	{
		var movesouth = numminy+shifty;
		MapWindow.setAttribute('viewBox',numminx+" "+movesouth+" "+numwidth+" "+numheight);
	}
	if (MyDirection=="reset")	{
		MapWindow.setAttribute('viewBox','50000 -585055 650900 500000');
	}
}

function toggleMap(check_element_name, map_element_name, layer_name) {
	var flag_variable;
	// Set flag_variable to true or false
	if (layer_name == "towns") flag_variable = townsOn;

	var svgCheckObj = svgMapDoc.getElementById(check_element_name);
	var svgMapObj = svgMapDoc.getElementById(map_element_name);

	if (flag_variable != null) {
		if (flag_variable == true){
		// Hide layer if it's now on
//alert ("off");
			svgCheckObj.getStyle().setProperty("visibility","hidden");
			svgMapObj.setAttribute("visibility","hidden");
		} else {
//alert ("on");
		// Show layer if it's off
			svgCheckObj.getStyle().setProperty("visibility","visible");
			svgMapObj.setAttribute("visibility","visible");
		}
	}

	if (layer_name == "towns") townsOn = !townsOn;
}

function commaFormatted(amount) {
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = null;
	d = a[1];
	var i = parseInt(a[0]);

	if(isNaN(i)) { return amount; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);

	var n = new String(i);

	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}

	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);

	if (d == null) {
		amount = n; 
	} else { 
		amount = n + '.' + d; 
	}

	amount = minus + amount;

	return amount;
}
