						var permUp = null;
						var keepText;
						// Show the hover over popups
						function showPopup(item)
						{
								// If there is a permanent popup do nothing
								if (permUp != null) return; 
								// Get the mouse position
								xMousePos = window.event.x+document.body.scrollLeft;
								yMousePos = window.event.y+document.body.scrollTop;
								keepText = item.innerHTML;
								item.innerHTML = "HOVERING";
								item.style.color = "red";
								//Get the name of the cell and concat Pop                
								// This refers to the correct div tag
								pop = document.getElementById(item.name+"Pop");
								// Set position of popup
								pop.style.top = yMousePos+10;
								pop.style.left = 20;
								pop.style.visibility = "visible";
						}
						// Remove the hover over popup
						function hidePopup(item)
						{
								// If there is a permanent popup do nothing
								if (permUp != null) return; 
								item.innerHTML = keepText;
								item.style.color = "white";
								pop = document.getElementById(item.name+"Pop"); 
								pop.style.visibility = "hidden";
						}
						// On click display popup until unclicked
						function permPopup(item, summary) 
						{
						if (permUp != null && permUp != item) return;
						if (item.onmouseout == null) 
						{
						item.onmouseout = function()
						{
						hidePopup(item)
						}
						item.onmouseover = function() 
						{
						showPopup(item)
						};
						hidePopup(item);
						permUp = null;
						} 
						else
						{
						item.onmouseout = null;
						item.onmouseover = null;
						permUp = item;
						}
						}
						
						// Function to operate the Sort drop down
						function testSelect()
						{	
						var pick = document.getSort.category.selectedIndex;
						var fish = document.getSort.category.options[pick].value;
						alert("You Selected:" + fish);
						}	
						
						function getAverage(sum)
						{
								alert(sum+"  TOTAL LEGS");
						}

// Function to operate the XKCD drop down
			function getXKCDFave(selected)
			{					
				var pick = selected.selectedIndex;
				var fish = selected.options[pick].value;
				if(fish !=0)
				{
					var url = "http://www.xkcd.com/"+fish;
					var win = window.open(url,'name');	
				}
				else
				{
					return;
				}														
			}
			//Function for Farmville calculator
			function getMinutesRemaining(str)
			{
				var pick = document.getElementById("daysSel").selectedIndex;
				var days = document.getElementById("daysSel").options[pick].value;
				pick = document.getElementById("hoursSel").selectedIndex;
				var hours = document.getElementById("hoursSel").options[pick].value;
				pick = document.getElementById("percentSel").selectedIndex;
				var percent = document.getElementById("percentSel").options[pick].value;
				var mins = 0;
				if(hours > 0)
				{
					mins = mins+hours*60;
				}
				if(days > 0)
				{
					mins = mins+(days*24*60)
				}
				var answer = (mins/100)*(100-percent);
				var daysLeft = answer/1440
				var remainderH = answer%1440;
				var hoursLeft = remainderH/60;
				var minsLeft = remainderH%60;
				document.getElementById('days').innerHTML=days;
				document.getElementById('percent').innerHTML=percent;
				document.getElementById('hours').innerHTML=hours;
				document.getElementById('leftDays').innerHTML=Math.floor(daysLeft);
				document.getElementById('leftHours').innerHTML=Math.floor(hoursLeft);
				document.getElementById('leftMins').innerHTML=Math.floor(minsLeft);
			}





var osMap, screenOverlay, mapOV, postcodeService;

var e=document.getElementById("map");

// Variables for postcode/gazetteer searches
var inputStr, sectorFlag, globalGazArray, locationFound, zoomVal, eastVal, eastValstr, o, da;

// Start of Functions required for postcode/gazetteer searches
//clear search box when clicked on
function clearText(){
document.getElementById("searchArea").value = "";
}

function searchPostcode()
{

//hide and clear list box
document.getElementById('selectGaz').style.display='none';
da = document.getElementById("selectGaz");
da.options.length = 0;
locationFound = 0;
eastValstr = "";

//clear menu if already populated
da.options.length = 0;
sectorFlag = 0;

var query = document.getElementById("searchArea");
inputStr = query.value;
//document.getElementById("markersCheckBox").checked = false;
document.getElementById("searchArea").value = "enter a place/postcode";

//ascertain if postcode sector or full postcode
if (inputStr.length < 5)
{
sectorFlag = 1;
}

//search postcode service
postcodeService.getLonLat(inputStr, onResult);
return;
}

//result of search postcode is passed here
function onResult(mapPoint)
{
//set zoom level depending on sector or full postcode
if  (sectorFlag == 0)
{zoomVal = 9;}
else {zoomVal = 5}

//if not a valid PostCode, pass to gazetteer search
//an eastValStr of length three indicates no match found for postcode
if (mapPoint != null)
{
eastVal = mapPoint.getEasting();
eastValstr = eastVal.toString();
}

//no postcode match, so search gazetteer
if (eastValstr.length == 3 || mapPoint == null)
{
var osGaz = new OpenSpace.Gazetteer;
var gazArray = osGaz.getLocations(inputStr, gazOptions)
}

//zoom to postcode
if (mapPoint != null && eastValstr.length > 3)
{
osMap.setCenter(mapPoint, zoomVal);
locationFound = 1;
document.getElementById("postcode").value = "";
}
return false;
}

function gazOptions(searchVal)
{
//if one match found
if (searchVal.length == 1)
{
osMap.setCenter(searchVal[0].location, 7);
locationFound = 1;
}

//if several choices, create a list box
if (searchVal != null && searchVal.length > 1)
{
locationFound = 1;
globalGazArray = searchVal;
o = document.createElement("OPTION");
o.text= "Select a place";
da.options.add(o);

//build list box
for (var i=0 ; i < searchVal.length; i++)
{
o = document.createElement("OPTION");
o.text= searchVal[i].name + ", " + searchVal[i].county;
da.options.add(o);
}

//make list box visible
document.getElementById('selectGaz').style.display='block';
}

if (locationFound == 0)
{
alert("neither postcode or place found");
}
}

//zoom to item selected from list box
function zoomGazSel(selObj)
{
osMap.setCenter(globalGazArray[selObj.selectedIndex-1].location, 7);

//hide list box
document.getElementById('selectGaz').style.display='none';

//clear text field
document.getElementById("searchArea").value = "enter a place/postcode";
}

// End of Functions required for postcode/gazetteer searches
function initmapbuilder()
{
// Creating the Openspace map and the postcode service
osMap = new OpenSpace.Map('map');
postcodeService = new OpenSpace.Postcode();

// Adding the Grid Coordinates (mouse over map) box 
screenOverlay = new OpenSpace.Layer.ScreenOverlay("coords");
var leftPos = osMap.div.clientWidth - 116;
screenOverlay.setPosition(new OpenLayers.Pixel(leftPos, 5));
osMap.addLayer(screenOverlay);
var gridProjection = new OpenSpace.GridProjection();
osMap.events.register("mousemove", osMap, function(e) {
var pt = osMap.getLonLatFromViewPortPx(e.xy);
var lonlat = gridProjection.getLonLatFromMapPoint(pt);
screenOverlay.setHTML("<DIV style=\"width: 108px; height=86px; padding-left: 4px; color: white; background-color: #222; font-size: 12px\">" +
"EAST: " + (pt.lon).toFixed(0) + "<BR>" +
"NORTH: " + (pt.lat).toFixed(0) + "<BR>" +
"LON: " + (lonlat.lon).toFixed(8) + "<BR>" +
"LAT: " + (lonlat.lat).toFixed(8) + " </DIV>");
});

// Adding the map overview
mapOV = new OpenSpace.Control.OverviewMap();
osMap.addControl(mapOV);
//fix to put copyright on top of overview map: Needs api fix for later version
var ccControl = osMap.getControlsByClass("OpenSpace.Control.CopyrightCollection")
osMap.removeControl(ccControl[0]);
ccControl = new OpenSpace.Control.CopyrightCollection();
osMap.addControl(ccControl);
ccControl.activate();
//end of fix
// Turning the overview map on
mapOV.maximizeControl();

// Adding the postcode/gazetteer search box
//define an overlay for search box
searchBox = new OpenSpace.Layer.ScreenOverlay("search");
//set its position
searchBox.setPosition(new OpenLayers.Pixel(80, 0));
//and add to the map
osMap.addLayer(searchBox);
searchBox.setHTML("<div id=\"OpenSpace.Layer.ScreenOverlay_132\" style=\"position: absolute; width: 200px; height: 100%; z-index: 340; left: 0px; top: 3px;\" class=\"olLayerDiv\">" + 
"<div id=\"div1\" style=\"z-index:999; padding-left: 0px; font-size: 14px;\">" + 
"<form name=\"searchForm\" onsubmit=\"return false;\">" + 
"<input type=\"text\" name=\"searchArea\" id=\"searchArea\" onclick=\"clearText()\" value=\"enter a place/postcode\"/><input type=\"button\" onclick=\"searchPostcode();\" value=\"Find\" title=\"find place by postcode or 1:50,000 gazetteer\"></button>" + 
"<select name=\"select\" id=\"selectGaz\" onchange=\"zoomGazSel(this.form.select)\" style=\"display: block\">" + 
"<option>Select a place</option><option></option></select></form></div></div>");
//hide list box select
document.getElementById('selectGaz').style.display='none';
searchBox.events.register("mouseover", searchBox, function(){
//de-activate keyboard and navigation controls
osMap.controls[0].deactivate();
osMap.controls[1].deactivate();
});
searchBox.events.register("mouseout", searchBox, function(){
//activate keyboard and navigation controls
osMap.controls[0].activate();
osMap.controls[1].activate();
});
// Defining the center of the map and the zoom level
osMap.setCenter(new OpenSpace.MapPoint(426822,564423),10);

clusterControl = new OpenSpace.Control.ClusterManager();
osMap.addControl(clusterControl);
clusterControl.activate();}
