// JavaScript Document

 //<![CDATA[
    if (GBrowserIsCompatible()) {
      // this variable will collect the html which will eventually be placed in the sidebar
      var sidebar_html = "";
    
      // arrays to hold copies of the markers and html used by the sidebar
      // because the function closure trick doesnt work there
      var gmarkers = [];
      var htmls = [];
      var i = 0;
	  // arrays to hold variants of the info window html with get direction forms open
      var to_htmls = [];
      var from_htmls = [];

	  var gicons=[];
	  gicons["mpg"] = new GIcon(G_DEFAULT_ICON, "images/google/giconMPG1.png");
  	  gicons["tower"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-tower.png");
	  gicons["house"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-house.png");
	  gicons["manutd"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-utd.png");
	  gicons["mancity"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-city.png");
	  gicons["men"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-men.png");
	  gicons["gmex"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-gmex.png");
	  gicons["thall"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-thall.png");
	  gicons["wheel"] = new GIcon(G_DEFAULT_ICON, "images/google/icon-wheel.png"); 

      // A function to create the marker and set up the event window
      function createMarker(point,name,html,icon) {
        // === create a marker with the requested icon ===
        var marker = new GMarker(point, gicons[icon]);
		
		        // The info window version with the "to here" form open
        to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT"/>' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
                  // "(" + name + ")" + 
           '"/></form>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" + 
           '"/></form>';
        // The inactive version of the direction info
        html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
		
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });

        // save the info we need to use later for the sidebar
        gmarkers[i] = marker;
        htmls[i] = html;
        // add a line to the sidebar html
        sidebar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
        i++;
        return marker;
      }


      // 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]);
      }
	  // Test Function to reset Map centre
	  function areaShift(a,b,c){
	  	map.setCenter(new GLatLng(a,b), c, G_HYBRID_MAP);
	  }
      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());//                        Sets the zoom bar
      //map.addControl(new GMapTypeControl());                         Sets the switch for Map, Satellite or Hybrid
      map.setCenter(new GLatLng(53.47940770611113,-2.24470853805542), 14, G_HYBRID_MAP);
	  
	  //add overlay test code
	    photo = new TPhoto();
  		photo.id = 'addphoto';
  		photo.src = 'http://www.manchesterpropertyguide.co.uk/images/overlayv2.png';
  		photo.percentOpacity = 50;
  		photo.anchorTopLeft = new GLatLng(53.48988396018834,-2.2664237022399902);
  		photo.anchorBottomRight = new GLatLng(53.46878836290587,-2.2164058685302734);

  		map.addTPhoto(photo);
	  
		  function addIt(a) {
			map.addTPhoto(a);
			document.getElementById('overlayLink').innerHTML='<a href="javascript: removeIt(photo);">Hide Coloured Overlay</a>';
		  }
		  function removeIt(a) {
			map.removeTPhoto(a);
			document.getElementById('overlayLink').innerHTML='<a href="javascript: addIt(photo);">Show Central Manchester Areas Coloured Overlay</a>';
		  }
	  
      // Read the data from example.xml
      var request = GXmlHttp.create();
      request.open("GET", "data.php", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = request.responseXML;
          // obtain the array of markers and loop through it
		  var areas = xmlDoc.documentElement.getElementsByTagName("areas");

		// start building table
		sidebar_html += "<hr/><br/><table width=\"100%\">";
		var cell_tally = 0;
		var max_rows = 3;

		  for (var j = 0; j < areas.length; j++) {
		  	var area_name = areas[j].getAttribute("name");
			var area_lat = parseFloat(areas[j].getAttribute("lat"));
			var area_long = parseFloat(areas[j].getAttribute("long"));
			var area_zoom = parseFloat(areas[j].getAttribute("GZoom"));
			
			if (cell_tally < max_rows) { sidebar_html += "</tr>"; cell_tally = 0; }
	  		sidebar_html += "<tr><td colspan=" + max_rows + " class='cell_line'><a href=\"javascript:areaShift("+ area_lat +","+ area_long +","+ area_zoom +");\"><strong>" + area_name + "</strong></a></td></tr><tr>";
		  
			// var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			var markers = areas[j].getElementsByTagName("marker"); 
		  
			  for (var i = 0; i < markers.length; i++) {
				// obtain the attribues of each marker
				var lat = parseFloat(markers[i].getAttribute("lat"));
				var lng = parseFloat(markers[i].getAttribute("lng"));
				var point = new GLatLng(lat,lng);
				var html = markers[i].getAttribute("html");
				var label = markers[i].getAttribute("label");
				// === read the icon attribute ===
				var icon = markers[i].getAttribute("icon");
				
				// === create the marker, passing the icon string ===
				if (cell_tally < max_rows) {
					sidebar_html += "<td>";
					cell_tally++;
				} else {
					sidebar_html += "</tr><tr><td>";
					cell_tally = 1;
				}
				var marker = createMarker(point,label,html,icon);
				sidebar_html += "</td>";
				
				map.addOverlay(marker);
			  }
			  
			  
			}
			
			 if (cell_tally < max_rows) { sidebar_html += "</tr>"; }
			  sidebar_html += "</table>";
			  
          // put the assembled sidebar_html contents into the sidebar div
          document.getElementById("sidebar").innerHTML = sidebar_html;
		  //DumperAlert(an);
        }
      }
      request.send(null);
	  
	  //static markers for landmarks, Old Trafford etc.
	  var uPoint = new GLatLng(53.46304495619439,-2.2913172841072083);
	  var label = "The Theatre of Dreams - Home to Manchester United.";
	  var uHtml = "The Theatre of Dreams<br/>Home to Manchester United.<br/><a href='http://www.manutd.com' target='_blank'>Website</a>";
	  var uIcon = "manutd";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);
	  
	  var uPoint = new GLatLng(53.48314268211716,-2.1999263763427734);
	  var label = "City of Manchester Stadium - Home to Manchester City.";
	  var uHtml = "Eastlands Arena<br/>Home to Manchester City.<br/><a href='http://www.mcfc.co.uk' target='_blank'>Website</a>";
	  var uIcon = "mancity";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);
	  
	  var uPoint = new GLatLng(53.48418969330358,-2.2445154190063477);
	  var label = "Harvey Nichols";
	  var uHtml = "Harvey Nichols<br/><a href='http://www.harveynichols.com/output/Page144.asp' target='_blank'>Website</a>";
	  var uIcon = "";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);

	  var uPoint = new GLatLng(53.488173206973485,-2.243978977203369);
	  var label = "MEN Arena";
	  var uHtml = "MEN Arena<br/><a href='http://www.men-arena.com' target='_blank'>Website</a>";
	  var uIcon = "men";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);
	  
	  var uPoint = new GLatLng(53.47655357975259,-2.246553897857666);
	  var label = "Manchester Central (formerly G-MEX)";
	  var uHtml = "Manchester Central (formerly G-MEX)<br/><a href='http://www.gmex-micc.co.uk' target='_blank'>Website</a>";
	  var uIcon = "gmex";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);

	  var uPoint = new GLatLng(53.47940770611113,-2.24470853805542);
	  var label = "Manchester Town Hall";
	  var uHtml = "Manchester Town Hall<br/>Albert Square<br>Manchester";
	  var uIcon = "thall";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);
	  
	  var uPoint = new GLatLng(53.48420246157317,-2.243088483810425);
	  var label = "Manchester Wheel";
	  var uHtml = "Manchester Wheel<br/>Exchange Square<br>Manchester";
	  var uIcon = "wheel";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);
	  
	  var uPoint = new GLatLng(53.47175157381823, -2.299318313598633);
	  var label = "Media City (proposed location)";
	  var uHtml = "Media City (proposed location)";
	  var uIcon = "";
	  var utdMarker = createMarker(uPoint,label,uHtml,uIcon);
	  map.addOverlay(utdMarker);
	  
	  //Section below adds outline and overlay to the propsed media City site at Salford Quays
	        var pts = [
				new GLatLng(53.47371841854568, -2.303030490875244),
				 new GLatLng(53.473756732797554, -2.302365303039551),
				 new GLatLng(53.473846132584065,  -2.3012495040893555),
				 new GLatLng(53.47447192581629, -2.2985029220581055),
				 new GLatLng(53.47411433081358, -2.298309803009033),
				 new GLatLng(53.47420372984675, -2.2978591918945312),
				 new GLatLng(53.47457409526362, -2.29813814163208),
				 new GLatLng(53.475136022826874, -2.295563220977783),
				 new GLatLng(53.47343744630832, -2.2941040992736816),
				 new GLatLng(53.473092614201796, -2.2950053215026855),
				 new GLatLng(53.4726966924019, -2.2957992553710938),
				 new GLatLng(53.472070872991246, -2.2960996627807617),
				 new GLatLng(53.471406728013186, -2.2988462448120117),
				 new GLatLng(53.471419500130004, -2.2992753982543945),
				 new GLatLng(53.4707042556688, -2.300112247467041),
				 new GLatLng(53.4696952295933, -2.30088472366333),
				 new GLatLng(53.4707042556688,  -2.3047900199890137),
				 new GLatLng(53.471623853476416, -2.3024940490722656),
				 new GLatLng(53.47097247375429, -2.3006057739257812),
				 new GLatLng(53.47170048572763, -2.2998762130737305),
				 new GLatLng(53.47328418794024, -2.303481101989746),
				 new GLatLng(53.47371841854568, -2.303030490875244)];
		  var polygon = new GPolygon(pts, null, 2, 0.7, "#ffffff", 0.5 );
		  map.addOverlay(polygon);
		}

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
	
    //]]>