var map;
var gmarkers = [];
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];
var icon1g = new GIcon();
icon1g.image = "resources/person1g.gif";
var icon2g = new GIcon();
icon2g.image = "resources/person2g.gif";
var icon3g = new GIcon();
icon3g.image = "resources/person3g.gif";
var icon4g = new GIcon();
icon4g.image = "resources/person4g.gif";
var icon1r = new GIcon();
icon1r.image = "resources/person1r.gif";
var icon2r = new GIcon();
icon2r.image = "resources/person2r.gif";
var icon3r = new GIcon();
icon3r.image = "resources/person3r.gif";
var icon4r = new GIcon();
icon4r.image = "resources/person4r.gif";
var icon1w = new GIcon();
icon1w.image = "resources/climbingwall1.gif";
var icon2w = new GIcon();
icon2w.image = "resources/climbingwall2.gif";
var icon3w = new GIcon();
icon3w.image = "resources/climbingwall3.gif";
var icon4w = new GIcon();
icon4w.image = "resources/climbingwall4.gif";
var icon1star = new GIcon();
icon1star.image = "resources/starvenue1.png";
var icon2star = new GIcon();
icon2star.image = "resources/starvenue2.png";
var icon3star = new GIcon();
icon3star.image = "resources/starvenue3.png";
var icon4star = new GIcon();
icon4star.image = "resources/starvenue4.png";
var icon1starred = new GIcon();
icon1starred.image = "resources/starvenuered1.png";
var icon2starred = new GIcon();
icon2starred.image = "resources/starvenuered2.png";
var icon3starred = new GIcon();
icon3starred.image = "resources/starvenuered3.png";
var icon4starred = new GIcon();
icon4starred.image = "resources/starvenuered4.png";
var icon1d = new GIcon();
icon1d.image = "resources/deepwater1.png";
var icon2d = new GIcon();
icon2d.image = "resources/deepwater2.png";
var icon3d = new GIcon();
icon3d.image = "resources/deepwater3.png";
var icon4d = new GIcon();
icon4d.image = "resources/deepwater4.png";
var icon1s = new GIcon();
icon1s.image = "resources/steep1.png";
var icon2s = new GIcon();
icon2s.image = "resources/steep2.png";
var icon3s = new GIcon();
icon3s.image = "resources/steep3.png";
var icon4s = new GIcon();
icon4s.image = "resources/steep4.png";
var icon1m = new GIcon();
icon1m.image = "resources/mystery1.png";
var icon2m = new GIcon();
icon2m.image = "resources/mystery2.png";
var icon3m = new GIcon();
icon3m.image = "resources/mystery3.png";
var icon4m = new GIcon();
icon4m.image = "resources/mystery4.png";
var icon1p = new GIcon();
icon1p.image = "resources/parking1.gif";
//icon.shadow = "http://www.clivedowie.com/betaguides/resources/pushpin_shadow.png";
icon1g.iconAnchor = new GPoint(14, 48);
icon2g.iconAnchor = new GPoint(7, 24);
icon3g.iconAnchor = new GPoint(3, 12);
icon4g.iconAnchor = new GPoint(1, 6);
icon1r.iconAnchor = new GPoint(14, 48);
icon2r.iconAnchor = new GPoint(7, 24);
icon3r.iconAnchor = new GPoint(3, 12);
icon4r.iconAnchor = new GPoint(1, 6);
icon1w.iconAnchor = new GPoint(14, 48);
icon2w.iconAnchor = new GPoint(7, 24);
icon3w.iconAnchor = new GPoint(3, 12);
icon4w.iconAnchor = new GPoint(1, 6);
icon1star.iconAnchor = new GPoint(14, 48);
icon2star.iconAnchor = new GPoint(7, 24);
icon3star.iconAnchor = new GPoint(3, 12);
icon4star.iconAnchor = new GPoint(1, 6);
icon1starred.iconAnchor = new GPoint(14, 48);
icon2starred.iconAnchor = new GPoint(7, 24);
icon3starred.iconAnchor = new GPoint(3, 12);
icon4starred.iconAnchor = new GPoint(1, 6);
icon1d.iconAnchor = new GPoint(14, 48);
icon2d.iconAnchor = new GPoint(7, 24);
icon3d.iconAnchor = new GPoint(3, 12);
icon4d.iconAnchor = new GPoint(1, 6);
icon1s.iconAnchor = new GPoint(14, 48);
icon2s.iconAnchor = new GPoint(7, 24);
icon3s.iconAnchor = new GPoint(3, 12);
icon4s.iconAnchor = new GPoint(1, 6);
icon1m.iconAnchor = new GPoint(14, 48);
icon2m.iconAnchor = new GPoint(7, 24);
icon3m.iconAnchor = new GPoint(3, 12);
icon4m.iconAnchor = new GPoint(1, 6);
icon1p.iconAnchor = new GPoint(14, 48);
icon1g.infoWindowAnchor = new GPoint(14, 20);
icon2g.infoWindowAnchor = new GPoint(7, 24);
icon3g.infoWindowAnchor = new GPoint(3, 12);
icon4g.infoWindowAnchor = new GPoint(1, 6);
icon1r.infoWindowAnchor = new GPoint(14, 20);
icon2r.infoWindowAnchor = new GPoint(7, 24);
icon3r.infoWindowAnchor = new GPoint(3, 12);
icon4r.infoWindowAnchor = new GPoint(1, 6);
icon1w.infoWindowAnchor = new GPoint(14, 20);
icon2w.infoWindowAnchor = new GPoint(7, 24);
icon3w.infoWindowAnchor = new GPoint(3, 12);
icon4w.infoWindowAnchor = new GPoint(1, 6);
icon1star.infoWindowAnchor = new GPoint(14, 20);
icon2star.infoWindowAnchor = new GPoint(7, 24);
icon3star.infoWindowAnchor = new GPoint(3, 12);
icon4star.infoWindowAnchor = new GPoint(1, 6);
icon1starred.infoWindowAnchor = new GPoint(14, 20);
icon2starred.infoWindowAnchor = new GPoint(7, 24);
icon3starred.infoWindowAnchor = new GPoint(3, 12);
icon4starred.infoWindowAnchor = new GPoint(1, 6);
icon1d.infoWindowAnchor = new GPoint(14, 20);
icon2d.infoWindowAnchor = new GPoint(7, 24);
icon3d.infoWindowAnchor = new GPoint(3, 12);
icon4d.infoWindowAnchor = new GPoint(1, 6);
icon1s.infoWindowAnchor = new GPoint(14, 20);
icon2s.infoWindowAnchor = new GPoint(7, 24);
icon3s.infoWindowAnchor = new GPoint(3, 12);
icon4s.infoWindowAnchor = new GPoint(1, 6);
icon1m.infoWindowAnchor = new GPoint(14, 20);
icon2m.infoWindowAnchor = new GPoint(7, 24);
icon3m.infoWindowAnchor = new GPoint(3, 12);
icon4m.infoWindowAnchor = new GPoint(1, 6);
icon1p.infoWindowAnchor = new GPoint(14, 20);

// Creates a marker at the given point with the given number label
function createMarker(point, html, icon, thatlat, thatlon, thatlocation) {
	var marker = new GMarker(point, icon);
	var mcount = gmarkers.length;
	// The info window version with the "to here" form open
    to_htmls[mcount] = html + "<br><span style='color:#000;'>Directions: <b>To here<\/b> - <a href='javascript:fromhere(" + mcount + ")'>From here<\/a><br><span class='directionslabel'>Start address:</span><form action='javascript:getDirections()'><input type='text' SIZE=40 MAXLENGTH=40 name='saddr' id='saddr' value='' /><br></span><INPUT value='Get Directions' TYPE='SUBMIT' name='go' onclick='getDirections(this.form)'><input type='hidden' id='daddr' value='" + thatlocation + "@" + point.lat() + "," + point.lng() + "'/>";
	// The info window version with the "from here" form open
    from_htmls[mcount] = html + "<br><span style='color:#000;'>Directions: <a href='javascript:tohere(" + mcount + ")'>To here<\/a> - <b>From here<\/b><br><span class='directionslabel'>End address:</span><form action='javascript:getDirections()'><input type='text' SIZE=40 MAXLENGTH=40 name='daddr' id='daddr' value='' /><br></span><INPUT value='Get Directions' TYPE='SUBMIT' name='go' onclick='getDirections(this.form)'><input type='hidden' id='saddr' value='" + thatlocation + "@" + point.lat() + "," + point.lng() + "'/>";
	// The inactive version of the direction info
	html = html + "<br><span style='color:#000;'>Directions: </span><a href='javascript:tohere(" + mcount + ")'>To here<\/a> - <a href='javascript:fromhere(" + mcount +")'>From here<\/a>";
	GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
		gmarkers.push(marker);

  return marker;
}

function mapLoad() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	var point = new GLatLng(thislat, thislon);

	map.setCenter(point, zoom);
	map.setMapType(G_SATELLITE_MAP);
	var mm = new GMarkerManager(map);

	if (pointcount > 0)
		{
		var areaptr = 0;
		var lonptr = 0;
		var latptr = 0;
		var locationptr = 0;
		var loctypeptr = 0;
		var facilitiesptr = 0;
		var thatarea;
		var thatlon;
		var thatlat;
		var thatlocation;
		var thatlocationtype;
		var thatfacilities;
		var thattextcolour
		var html

		for (i=0;i<=pointcount ;i=i+1 )
			{
			thatarea = arealist.substr(areaptr,arealist.indexOf("|",areaptr) - areaptr);
			areaptr += thatarea.length + 1;
			thatlon = longitudelist.substr(lonptr,longitudelist.indexOf("|",lonptr) - lonptr);
			lonptr += thatlon.length + 1;
			thatlat = latitudelist.substr(latptr,latitudelist.indexOf("|",latptr) - latptr);
			latptr += thatlat.length + 1;
			thatlocation = locationlist.substr(locationptr,locationlist.indexOf("|",locationptr) - locationptr);
			locationptr += thatlocation.length + 1;
			thatlocationtype = loctypelist.substr(loctypeptr,loctypelist.indexOf("|",loctypeptr) - loctypeptr);
			if (issuulist.substr(i,1) == "y")
				{
					thattextcolour="red";
				}
			else if (thatlocationtype == "P")
				{
					thattextcolour="#33f";
				}
			else if (thatlocationtype == "W" || thatlocationtype == "*" || thatlocationtype == "D" || thatlocationtype == "S" || thatlocationtype == "M")
				{
					thattextcolour="#00f"; 
				}
			else
				{
				thattextcolour="#666"; 
				}
			loctypeptr += thatlocationtype.length + 1;
			thatfacilities = facilitieslist.substr(facilitiesptr,facilitieslist.indexOf("|",facilitiesptr) - facilitiesptr);
			facilitiesptr += thatfacilities.length + 1;

			html = "<a href='index.php?country=" + country + "&location=" + thatlocation + "'><span style='color:" + thattextcolour +";'>" + thatlocation + "</span>";
//			if (thatfacilities == "P")
//			{
//			html = html + " <img src='resources/icon_parking.gif' title='Parking' alt='Parking' />";
//			}
			html = html + "<br><span style='color:#000;'>" + thatarea + "</span></a>";


			if (thatlocationtype == "W")
				{
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1w, thatlat, thatlon, thatlocation),8,17);
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2w, thatlat, thatlon, thatlocation),5,7); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3w, thatlat, thatlon, thatlocation),3,4); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4w, thatlat, thatlon, thatlocation),0,2);
				}
			else if (thatlocationtype == "*")
				{

					if (issuulist.substr(i,1) == "y") {
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1starred, thatlat, thatlon, thatlocation),8,17);
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2starred, thatlat, thatlon, thatlocation),5,7); 
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3starred, thatlat, thatlon, thatlocation),3,4); 
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4starred, thatlat, thatlon, thatlocation),0,2);
					} else {
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1star, thatlat, thatlon, thatlocation),8,17);
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2star, thatlat, thatlon, thatlocation),5,7); 
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3star, thatlat, thatlon, thatlocation),3,4); 
						mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4star, thatlat, thatlon, thatlocation),0,2);
					}
				}
			else if (thatlocationtype == "D")
				{
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1d, thatlat, thatlon, thatlocation),8,17);
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2d, thatlat, thatlon, thatlocation),5,7); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3d, thatlat, thatlon, thatlocation),3,4); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4d, thatlat, thatlon, thatlocation),0,2);
				}
			else if (thatlocationtype == "S")
				{
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1s, thatlat, thatlon, thatlocation),8,17);
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2s, thatlat, thatlon, thatlocation),5,7); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3s, thatlat, thatlon, thatlocation),3,4); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4s, thatlat, thatlon, thatlocation),0,2);
				}
			else if (thatlocationtype == "M")
				{
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1m, thatlat, thatlon, thatlocation),8,17);
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2m, thatlat, thatlon, thatlocation),5,7); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3m, thatlat, thatlon, thatlocation),3,4); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4m, thatlat, thatlon, thatlocation),0,2);
				}
			else if (thatlocationtype == "P")
				{
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1p, thatlat, thatlon, thatlocation),12,17);
				}
			else if (issuulist.substr(i,1) == "y")
				{
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1r, thatlat, thatlon, thatlocation),8,17);
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2r, thatlat, thatlon, thatlocation),5,7); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3r, thatlat, thatlon, thatlocation),3,4); 
					mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4r, thatlat, thatlon, thatlocation),0,2); 
				}
			else
				{
				mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon1g, thatlat, thatlon, thatlocation),8,17);
				mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon2g, thatlat, thatlon, thatlocation),5,7); 
				mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon3g, thatlat, thatlon, thatlocation),3,4); 
				mm.addMarker(createMarker(new GLatLng(thatlat, thatlon), html,icon4g, thatlat, thatlon, thatlocation),0,2); 
				}
			}
		}
  }
	mm.refresh();
}

// ===== request the directions =====
function getDirections() {
	// ==== set the start and end locations ====
	var saddr = document.getElementById("saddr").value;
	var daddr = document.getElementById("daddr").value;
	window.open( "http://maps.google.co.uk/maps?f=d&source=s_d&saddr=" + saddr + "&daddr=" + daddr + "&hl=en&geocode=&mra=ls&ie=UTF8&z=7", "myWindow", "resizable = 1" )
}

// This function picks up the click and opens the corresponding info window
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

// functions that open the directions forms
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}
addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
