// JavaScript Document

    //<![CDATA[
	var map = null;
    var geocoder = null;
	var markerArray = [];

    var iconWindsurf = new GIcon(); 
    iconWindsurf.image = 'http://www.totalwindsurf.co.uk/images/map/windsurfing.png';
    iconWindsurf.iconSize = new GSize(32, 32);
    iconWindsurf.iconAnchor = new GPoint(6, 20);
    iconWindsurf.infoWindowAnchor = new GPoint(25, 0);
	
	var iconSurf = new GIcon(); 
    iconSurf.image = 'http://www.totalwindsurf.co.uk/images/map/surfing.png';
    iconSurf.iconSize = new GSize(32, 32);
    iconSurf.iconAnchor = new GPoint(6, 20);
    iconSurf.infoWindowAnchor = new GPoint(25, 0);
	
	var iconKite = new GIcon(); 
    iconKite.image = 'http://www.totalwindsurf.co.uk/images/map/kitesurfing.png';
    iconKite.iconSize = new GSize(32, 32);
    iconKite.iconAnchor = new GPoint(6, 20);
    iconKite.infoWindowAnchor = new GPoint(25, 0);
	
	var iconKit = new GIcon(); 
    iconKit.image = 'http://www.totalwindsurf.co.uk/images/map/kitsupplier.png';
    iconKit.iconSize = new GSize(32, 32);
    iconKit.iconAnchor = new GPoint(6, 20);
    iconKit.infoWindowAnchor = new GPoint(25, 0);
	
	var iconTraining = new GIcon(); 
    iconTraining.image = 'http://www.totalwindsurf.co.uk/images/map/training.png';
    iconTraining.iconSize = new GSize(32, 32);
    iconTraining.iconAnchor = new GPoint(6, 20);
    iconTraining.infoWindowAnchor = new GPoint(25, 0);

    var customIcons = [];
    customIcons["Training Centre"] = iconTraining;
    customIcons["Windsurfing Location"] = iconWindsurf;
	customIcons["Kit Supplier"] = iconKit;
	customIcons["Surfing Location"] = iconSurf;
	customIcons["Kitesurfing Location"] = iconKite;
	
	

    function load() {
	if (GBrowserIsCompatible()) {
		/*var mapOptions = {
            googleBarOptions : {
              style : "new",
              adsOptions : {
                client: "pub-9315652752922615",
                channel: "4604678128",
                adsafe: "high",
                language: "en"
              }
            }
          }*/
    	map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl3D());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(26.43122806450644, 13.359375), 2);
		map.setMapType(G_HYBRID_MAP);
		geocoder = new GClientGeocoder();
		//map.enableGoogleBar();

        GDownloadUrl("home/mapxml", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("markers");
		  for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
			var id = markers[i].getAttribute("id");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, name, address, type, id);
			markerArray.push(marker); 
			//map.addOverlay(marker);
          }
		  var mc = new MarkerClusterer(map, markerArray);
        });
      }
    }

    function createMarker(point, name, address, type, id) {
      var marker = new GMarker(point, customIcons[type]);
      GEvent.addListener(marker, 'click', function(point) {
			if(point){
		geocoder.getLocations(point, function(addresses) {
			address = addresses.Placemark[0];
			var html = "<div class='info'><strong>" + name + "</strong> </div><em>" + type + "</em><br/>" + address.address + " <br /><br /><a href='location/"+ id +"/'><strong>View this location...</strong></a><br /><br />";
        marker.openInfoWindowHtml(html);
		
	
	 });
		map.setCenter(point); // center the marker on map 
         map.setZoom(map.getZoom()+2); // zoom on click marker 
			}

	});
      return marker;
    }
	
		// addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to find that location.");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
		map.setZoom(12);
		map.setCenter(point);
        marker.openInfoWindowHtml(place.address);
      }
    }

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation() {
      var address = document.locsearch.q.value;
      geocoder.getLocations(address, addAddressToMap);
    }
	
    //]]>
