//<![CDATA[
var mymap         = null;
var mymap2		  = null;
var mymarker      = null;
var isCompatible  = GBrowserIsCompatible(); 

var defaultZoom   = 13;
var defaultMarker = 0;
var defaultType   = 0;

/**
 * Creates a Google Map on the page.
 * @author Josh Macdonald
 * @param  sID = ID of the container
 * @param  lLat = Latitude of the map centre
 * @param  lLng = Longitude of the map centre
 * @param  lZoom = The zoom level of the map
 * @param  bMarker = Add a marker at the centre? 0=no, 1+=yes
 * @param  bType = Add a control to change map type? 0=no, 1+=yes
 */ 
function MakeGMap(sID, lLat, lLng, lZoom, bMarker, bType) 
{
   if (typeof lZoom   == 'undefined' ) lZoom   = defaultZoom;
   if (typeof bMarker == 'undefined' ) bMarker = defaultMarker;
   if (typeof bType   == 'undefined' ) bType   = defaultType;

   if (isCompatible) {
     mymap   = new GMap2(document.getElementById(sID), G_NORMAL_MAP);
     var mypoint = new GLatLng(lLat, lLng);
     mymap.setCenter(mypoint, lZoom);

     if(bMarker > 0){
       mymarker = new GMarker(mypoint);
       mymap.addOverlay(mymarker);
     }

     if(bType > 0) mymap.addControl(new GMapTypeControl());
     mymap.enableDoubleClickZoom();
     mymap.addControl(new GSmallZoomControl());
   }
}

/**
 * Creates a Google Map on the page and displays weather from an XML file.
 * @author Andrew Hennessy
 * @param  sID = ID of the container
 * @param  lLat = Latitude of the map centre
 * @param  lLng = Longitude of the map centre
 * @param  lZoom = The zoom level of the map
 * @param  bType = Add a control to change map type? 0=no, 1+=yes
 * @param  sXmlPath = Path to the weather xml file
 * @param  sMarkerTagName = Tag name of the marker nodes
 * @param  sLatAttr = Name of the latitude attribute
 * @param  sLngAttr = Name of the longitude attribute
 * @param  sLocAttr = Name of the attribute with location information
 * @param  sTempAttr = Name of the attribute with the temperature
 * @param  sDescAttr = Name of the attribute with description information
 * @param  sIconAttr = Name of the attribute with the path to the icon
 */ 
function MakeWeatherGMap(sID, lLat, lLng, lZoom, bType, sXmlPath, sMarkerTagName, sLatAttr, sLngAttr, sLocAttr, 
						 sTempAttr, sDescAttr, sIconAttr) 
{
   if (typeof lZoom   == 'undefined' ) lZoom   = defaultZoom;
   if (typeof bType   == 'undefined' ) bType   = defaultType;
   if (typeof sMarkerTagName == 'undefined') sMarkerTagName = 'marker';
   if (typeof sLatAttr  == 'undefined' ) sLatAttr  = 'lat';
   if (typeof sLngAttr  == 'undefined' ) sLngAttr  = 'long';
   if (typeof sLocAttr  == 'undefined' ) sLocAttr  = 'location';
   if (typeof sTempAttr == 'undefined' ) sTempAttr = 'temperature';
   if (typeof sDescAttr == 'undefined' ) sDescAttr = 'description';
   if (typeof sIconAttr == 'undefined' ) sIconAttr = 'image_path';
   
   if (isCompatible) {
	  var map = new GMap2(document.getElementById(sID));
	  map.setMapType(G_SATELLITE_MAP);
	  map.setCenter(new GLatLng(lLat, lLng), lZoom);
      if(bType > 0) map.addControl(new GMapTypeControl());
      
	  GDownloadUrl(sXmlPath, function(data, responseCode) {
		  if (responseCode == 200) {
			  var xml = GXml.parse(data);
			  var markers = xml.documentElement.getElementsByTagName(sMarkerTagName);
			  for (var i = 0; i < markers.length; i++) {
				var location = markers[i].getAttribute(sLocAttr);
				var temp     = markers[i].getAttribute(sTempAttr);
				var descr    = markers[i].getAttribute(sDescAttr);
				var iconpath = markers[i].getAttribute(sIconAttr);
				var point    = new GLatLng(parseFloat(markers[i].getAttribute(sLatAttr)),
										   parseFloat(markers[i].getAttribute(sLngAttr)));
				var marker = createWeatherMarker(point, location, temp, descr, iconpath, 45, 45);
				map.addOverlay(marker);
			  }
		  }
		  else if (responseCode == -1) {
			  alert("Data request timed out. Please try again later.");
		  }
		  else {
			  alert("Data request resulted in error. Please try again later.");
		  }
	  });
   }
}

/**
 * Creates a weather marker to be displayed on the map.
 * @author Andrew Hennessy
 * @param  point        = the location of the marker
 * @param  location     = the name of the location
 * @param  temp         = the forecast temperature
 * @param  description  = the description of the forecast
 * @param  iconpath     = the path to the weather icon
 * @param  iconheight   = the height of the weather icon
 * @param  iconwidth    = the width of the weather icon
 * @return newly created Marker object
 */ 
function createWeatherMarker(point, location, temp, description, iconpath, iconheight, iconwidth) 
{
	var icon = createWeatherIcon(iconpath,45,45);
	opts = {
            "icon": icon,
            "clickable": true,
            "labelText": "<strong>" + location + "<br />" + temp + "&deg;</strong>",
            "labelClass": "markerLabel",
            "labelOffset": new GSize(28, 0)
    };
	var marker = new LabeledMarker(point,opts);
	
	var html = "";
	html += '<div class="MapInfoWindow">';
	html += '<p><strong>' + location + '</strong><br />';
	html += '<img src="' + iconpath + '" height="' + iconheight + '" width="' + iconwidth + '" alt="' + location + '" class="MapInfoWindowPic" />';
	html += '<div class="MapInfoWindowTemp">' + temp + '</div>';
	html += '<p class="smaller">' + description + "</p>";
	html += '</div>';
	/*
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	*/
	return marker;
}

/**
 * Creates an icon to be displayed on the map.
 * @author Andrew Hennessy
 * @param  imagepath = the path of the image
 * @param  height    = image height
 * @param  width     = image width
 * @return newly created Icon object
 */ 
function createWeatherIcon(imagepath, height, width) 
{
	var icon = new GIcon(); 
	icon.image = imagepath;
	icon.iconSize = new GSize(width, height);
	icon.iconAnchor = new GPoint(16, 16);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

//]]>
