addListener("load",initMap);	//	Runs this JavaScript when the page loads, if JS is enabled of course


/*	JavaScript stuff for the map	*/

var osMap;
var markersLayer;
var intElement;
var arrLongitude = new Array();
var arrLatitude = new Array();
var minLong = -4.2637;	//	These 4 variables use the longitude and latitude of the Cycling Scotland HQ
var maxLong = -4.2637;
var minLat = 55.863061;
var maxLat = 55.863061;
var aveLong;
var aveLat;
var pos;


function onResult(mapPoint, org, proj, long, lat, orgID, postcode, website)
{	//	mapPoint contains the longditude and latitude of the marker
	//	Puts a marker for location on the map
	if (mapPoint != null)	//	Happens if OS has no location
	{
		//	Create new marker
		marker = new OpenLayers.Marker(mapPoint);
		
		//	Add events to marker
		mapEvent = new OpenLayers.Events(marker,marker.icon.imageDiv,null);
		//website = "";
		website="http://cyclingscotland.msol.org.uk/local/ccif/public_case_study.php?orgID="+orgID+"&postcode="+postcode+"&website="+website;
		if (website != "")
			mapEvent.register("click",null,function() { document.location = website;} );

		mapEvent.register("mouseover",null,function()
			{ 
				popup = new OpenLayers.Popup("organisationdetails",
                   new OpenLayers.LonLat(long, lat),
                   new OpenLayers.Size(160, 130),
                   "<h4>"+org+"</h4><h5>"+proj+"</h5>",
                   false);
				osMap.addPopup(popup);
			} );

		mapEvent.register("mouseout",null,function()
			{
				osMap.removePopup(popup);
			} );
		

		//	Add the marker to the layer
		markersLayer.addMarker(marker);
	}
}

function initMap()
{
	//	Create map
	osMap = new OpenSpace.Map('indexMap');
	markersLayer = new OpenLayers.Layer.Markers("Markers");
	osMap.addLayer(markersLayer);
	postcodeService = new OpenSpace.Postcode();

	//	convert string arrays for longitude and latitude to number arrays
	for (intElement in strArrayLongitude)
	{
		arrLongitude[intElement] = parseFloat(strArrayLongitude[intElement]);	
		arrLatitude[intElement] = parseFloat(strArrayLatitude[intElement]);
	}
	
	//	Create markers on the map for each case study
	for (intElement in strArrayLongitude)
	{
		if ( strArrayLongitude[intElement] != null && strArrayLongitude[intElement] != "0" )	
		{	//	Use long/lat to create marker
			if (arrLongitude[intElement] > maxLong)
				maxLong = arrLongitude[intElement];
			if (arrLongitude[intElement] < minLong)
				minLong = arrLongitude[intElement];
			if (arrLatitude[intElement] > maxLat)
				maxLat = arrLatitude[intElement];
			if (arrLatitude[intElement] < minLat)
				minLat = arrLatitude[intElement];
			organisation = strArrayOrgName[intElement];
			project = strArrayProjName[intElement];
			orgID = strArrayOrgID[intElement];
			postcode = strArrayPostcode[intElement];
			website = strArrayProjLink[intElement];
			orgID = strArrayOrgID[intElement];
			pos = new OpenSpace.MapPoint(arrLongitude[intElement], arrLatitude[intElement]);
			onResult(pos, organisation, project, arrLongitude[intElement], arrLatitude[intElement], orgID, postcode, website);
			//	If there is no long.lats we are going to ignore it altogether as finding a postcode uses callbacks and that causes timing problems
		}
	}
	
	//	work out the average of the long/lats so we can use those to center the screen
	aveLong = ((minLong + maxLong) / 2);
	aveLat = ((minLat + maxLat) / 2);
	//	The 3rd figure is the zoom level between 0 and 10 (0 is the whole of the uk, 8 is OS level and 9 and 10 are street level).  3 shows southern Scotland (mostly). 
	var gridProjection = new OpenSpace.GridProjection();
	var lonlat = new OpenLayers.LonLat(aveLong, aveLat);
	var mapPoint = gridProjection.getMapPointFromLonLat(lonlat);
	osMap.setCenter(mapPoint, 1);
}
 

