var map;
var request;
var icons = new Array();
icons["red"] = new GIcon(); 
icons["red"].image = "/themes/Skycarlo/static/img/apple.png"; 
icons["red"].shadow = "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_shadow.png"; 
icons["red"].iconSize = new GSize(24, 24); 
icons["red"].shadowSize = new GSize(22, 20); 
icons["red"].iconAnchor = new GPoint(6, 20); 
icons["red"].infoWindowAnchor = new GPoint(5, 1); 
icons["red"].imageMap = [4,0,0,4,0,7,3,11,4,19,7,19,8,11,11,7,11,4,7,0]; 
icons["red"].transparent = "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_transparent.png";

icons["green"] = new GIcon(); 
icons["green"].image = "/themes/Skycarlo/static/img/android.png"; 
icons["green"].shadow = "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_shadow.png"; 
icons["green"].iconSize = new GSize(24, 24); 
icons["green"].shadowSize = new GSize(22, 20); 
icons["green"].iconAnchor = new GPoint(6, 20); 
icons["green"].infoWindowAnchor = new GPoint(5, 1); 
icons["green"].imageMap = [4,0,0,4,0,7,3,11,4,19,7,19,8,11,11,7,11,4,7,0]; 
icons["green"].transparent = "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_transparent.png";

function get_icon(iconColor) {
   if ((typeof(iconColor)=="undefined") || (iconColor==null)) { 
      iconColor = "red"; 
   }
   if (!icons[iconColor]) {
      icons[iconColor] = new GIcon(icons["red"]);
      icons[iconColor].image = "http://labs.google.com/ridefinder/images/mm_20_"+ iconColor +".png";
   } 
   return icons[iconColor];
}

function createMarker(point,name,html,iconStr) {
	html = '<div style="white-space:nowrap;">' + html + '</div>';
	var marker = new GMarker(point);
	if (iconStr) {
		marker = new GMarker(point, get_icon(iconStr));
	}
	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}


function makeMap() {
    if (GBrowserIsCompatible()) {
      // resize the map
      var m = document.getElementById("map");
      m.style.height = "400px";
      m.style.width = "640px";

      // create the map
      map = new GMap(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(40.413496,-3.691406), 5);
      //40.413496,-3.691406
    } else {
      
    }
   getXMLfile();
}

function getXMLfile() {
	request = GXmlHttp.create();
	filename = "/markers.xml"
	request.open("GET", filename, true);
	request.onreadystatechange = processXMLfile;
	
	request.send(null);
	return false;
}

function processXMLfile() {
  if (request.readyState == 4) {
    if ((request.status == 200) || (request.status == 304)) {
       var xmlDoc = request.responseXML;
       if (xmlDoc.documentElement) {
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");

          map.clearOverlays();
          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 GPoint(lng,lat);
            var point = new GLatLng(lat,lng);
            var html = markers[i].getAttribute("html");
            var label = markers[i].getAttribute("label");
            var icon = markers[i].getAttribute("icon");
            // create the marker
            var marker = createMarker(point,label,html,icon);
            map.addOverlay(marker);
          }
       } else {
          alert("invalid xml file:"+filename);
       }
    } else {
     alert("file not found:"+filename);
    }
  }
}
