var iconBlue = new GIcon();
iconBlue.image = '/images/maps/mm_20_blue.png';
iconBlue.shadow = '/images/maps/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);

var iconRed = new GIcon();
iconRed.image = '/images/maps/mm_20_red.png';
iconRed.shadow = '/images/maps/mm_20_shadow.png';
iconRed.iconSize = new GSize(12, 20);
iconRed.shadowSize = new GSize(22, 20);
iconRed.iconAnchor = new GPoint(6, 20);
iconRed.infoWindowAnchor = new GPoint(5, 1);

var customIcons = [];
customIcons["blank"] = iconBlue;
customIcons["trail"] = iconRed;



function load(){
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addMapType(G_PHYSICAL_MAP);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    //map.enableScrollWheelZoom();
    map.setCenter(new GLatLng(49.781264058178344, -122.5140380859375), 8);
	map.setMapType(G_PHYSICAL_MAP);
	
	// Regions
	var kml = new GGeoXml("http://www.vancouvertrails.com/trails/maps/regions.kml");
	map.addOverlay(kml);

    GDownloadUrl("/regions/xml-trails.php", function(data) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      var previouslat = "";
      var previouslong = "";
      var html = "";
      var trailcount = 1;

      for (var i = 0; i < markers.length; i++) {
        var name = markers[i].getAttribute("name");
        var address = markers[i].getAttribute("trailurl");
        var shortdesc = markers[i].getAttribute("shortdesc");
        var photo = markers[i].getAttribute("photo");
        var type = markers[i].getAttribute("type");

        var latitude = markers[i].getAttribute("lat");
        var longitude = markers[i].getAttribute("lng");
        if (i < markers.length-1) {
            var nextlatitude = markers[i+1].getAttribute("lat");
            var nextlongitude = markers[i+1].getAttribute("lng");
        } else {
        	// We're at the last trail
        	var nextlatitude = "";
        	var nextlongitude = "";
        }
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));
        html = html+"<div class=\"bubble-small\"><p><a href=\"/trails/" + address + "/\"><img src=\"/images/small/"+photo+"\" border=\"0\" /></a></p><p><strong>" + name + "</strong><br /><a href=\"/trails/" + address + "/\">Read more</a></p></div>";

        if (nextlatitude != latitude && nextlongitude != longitude) {
        	if (trailcount > 1) {
	      		html = "<div id=\"bubble-wrap-small\"><p><strong>"+trailcount+" trails begin from this location</strong></p>"+html+"</div>";
	      	}
        	var marker = createMarker(point, name, address, shortdesc, photo, type, html, trailcount);
       		map.addOverlay(marker);
       		var html = "";
       		trailcount = 1;
        } else {
        	trailcount += 1;
        }
      }
    });
    }

    
}

// Create the HTML for the point
function createHTML (name, trailurl, shortdesc, photo, type) {
	var html = "<div class=\"bubble-small\"><a href=\"/trails/" + trailurl + "/\"><img src=\"/images/small/"+photo+"\" border=\"0\" /></a></p><p><strong>" + name + "</strong><br /><br /><a href=\"/trails/" + trailurl + "/\">Read more</a></p></div>";

}


function createMarker(point, name, trailurl, shortdesc, photo, type, html, trailcount) {
  var marker = new GMarker(point, customIcons[type]);
  //var html = "<div class=\"bubble\"><div class=\"leftcol\"><br /><img src=\"/images/small/"+photo+"\" align=\"left\" /></div><div class=\"rightcol\"><p><strong>" + name + "</strong><br />"+shortdesc+"<br /><br /><a href=\"/trails/" + trailurl + "/\">Read more</a></p></div><div class=\"clear\">&nbsp;</div></div>";
  GEvent.addListener(marker, 'click', function() {
  	marker.openInfoWindowHtml(html);
  });
  return marker;
}
