// Google Map interactivo desarrollado por VMG Studio - http://www.vmg-studio.com | info@vmg-studio.com
// Copyright 2010 VMG Studio - Todos los derechos reservados

var map = null;

function load() {
	if(!document.getElementById("googlemap"))
		return false;

	if (GBrowserIsCompatible()) {
	
	  var i = 0;

	  map = new GMap2(document.getElementById("googlemap"));

	  map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());
	  map.setMapType(G_NORMAL_MAP);
	  map.setCenter(new GLatLng(-34.51393, -58.52515),16);

	var edif1 = new EInsert(new GLatLng(-34.51589,-58.52603), "images/map/icon/edif-1.png", new GSize(51,59), 16);
	map.addOverlay(edif1);

	var edif2 = new EInsert(new GLatLng(-34.51592,-58.52522), "images/map/icon/edif-2.png", new GSize(23,29), 16);
	map.addOverlay(edif2);

	var edif3 = new EInsert(new GLatLng(-34.51537,-58.52560), "images/map/icon/edif-3.png", new GSize(20,21), 16);
	map.addOverlay(edif3);

	var calle = new EInsert(new GLatLng(-34.51552113584643,-58.525028228759766), "images/map/calle-Sgto-Cabral.png", new GSize(44,66), 16);
	map.addOverlay(calle);

	  var request = GXmlHttp.create();

	  request.open("GET", "xml/pbureau.xml", true);
	  request.onreadystatechange = function() {
		if (request.readyState == 4) {
		  var xmlDoc = request.responseXML;

		  var markers = xmlDoc.documentElement.getElementsByTagName("marker");
		  
		  for (var i = 0; i < markers.length; i++) {

			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 direccion = markers[i].getAttribute("direccion");
			var barrio = markers[i].getAttribute("barrio");
			var ciudad = markers[i].getAttribute("ciudad");
			var bus1 = markers[i].getAttribute("bus1");
			var bus2 = markers[i].getAttribute("bus2");
			var bus3 = markers[i].getAttribute("bus3");
			var bus4 = markers[i].getAttribute("bus4");
			var icontype = markers[i].getAttribute("icontype");

			var marker = createMarker(point,html,direccion,barrio,ciudad,bus1,bus2,bus3,bus4,icontype);
			map.addOverlay(marker);
		  }

          // ========= Now process the polylines ===========
          var lines = xmlDoc.documentElement.getElementsByTagName("line");
          // read each line
          for (var a = 0; a < lines.length; a++) {
            // get any line attributes
            var colour = lines[a].getAttribute("colour");
            var width  = parseFloat(lines[a].getAttribute("width"));
            // read each point on that line
            var points = lines[a].getElementsByTagName("point");
            var pts = [];
            for (var i = 0; i < points.length; i++) {
               pts[i] = new GLatLng(parseFloat(points[i].getAttribute("lat")),
                                   parseFloat(points[i].getAttribute("lng")));
            }
            map.addOverlay(new GPolyline(pts,colour,width));
          }
          // ================================================    
		}
	  }

	  request.send(null);


// iconos
  var gicons = [];
  gicons["bus"] = new GIcon(G_DEFAULT_ICON, "images/map/icon/icono-colectivo.png");
  gicons["bus"].iconSize = new GSize(17.0, 17.0);
  gicons["bus"].shadow = "images/map/icon/shadow-farmacity.png";
  gicons["bus"].shadowSize = new GSize(17.0, 17.0);
  gicons["bus"].iconAnchor = new GPoint(9,17);
  gicons["bus"].imageMap = [14,0,15,1,16,2,16,3,16,4,16,5,16,6,16,7,16,8,16,9,16,10,16,11,16,12,16,13,16,14,15,15,14,16,2,16,1,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,1,1,2,0];

  
  gicons["local"] = new GIcon(G_DEFAULT_ICON, "images/map/icon/Panamerican-Bureau.png");
  gicons["local"].iconSize = new GSize(123.0, 24.0);
  gicons["local"].shadow = "images/map/icon/shadow-Panamerican-Bureau.png";
  gicons["local"].shadowSize = new GSize(136.0, 24.0);
  gicons["local"].iconAnchor = new GPoint(123,24);
  gicons["local"].imageMap = [121,0,122,1,122,2,122,3,122,4,122,5,122,6,122,7,122,8,122,9,122,10,122,11,122,12,122,13,122,14,122,15,122,16,122,17,122,18,121,19,112,20,109,21,109,22,109,23,107,23,106,22,106,21,103,20,1,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0];
  
  gicons["estser"] = new GIcon(G_DEFAULT_ICON, "images/map/icon/estacion-servicio.png");
  gicons["estser"].iconSize = new GSize(15.0, 16.0);
  gicons["estser"].shadow = "images/map/icon/shadow-farmacity.png";
  gicons["estser"].shadowSize = new GSize(31.0, 24.0);
 
  gicons["marcador"] = new GIcon(G_DEFAULT_ICON, "images/map/icon/marcador.png");
  gicons["marcador"].iconSize = new GSize(10.0, 13.0);
  gicons["marcador"].shadow = "images/map/icon/shadow-marcador.png";
  gicons["marcador"].shadowSize = new GSize(17.0, 13.0);
  gicons["marcador"].iconAnchor = new GPoint(5,13); 
  gicons["marcador"].imageMap = [7,0,8,1,9,2,9,3,9,4,9,5,9,6,9,7,9,8,8,9,6,10,6,11,5,12,4,12,3,11,3,10,1,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,1,1,2,0];

// area manzan
  var pts = [new GLatLng(-34.515587, -58.526598), new GLatLng(-34.515010, -58.525393), new GLatLng(-34.516022, -58.524709), new GLatLng(-34.516588, -58.525927), new GLatLng(-34.515587, -58.526598)];
  var polygon = new GPolygon(pts, null, 0, 0.0, "#000000", 0.0 );
  map.addOverlay(polygon);

// area Edif 2
  var pts = [new GLatLng(-34.515795,-58.525458), new GLatLng(-34.516166,-58.525206), new GLatLng(-34.516064, -58.524975), new GLatLng(-34.515699,-58.525230), new GLatLng(-34.515688,-58.525297), new GLatLng(-34.515753,-58.525442), new GLatLng(-34.515795,-58.525458)];
  var polygon = new GPolygon(pts, null, 0, 0.0, "#000000", 0.0 );
  map.addOverlay(polygon);
  
// area Edif 3
  var pts = [new GLatLng(-34.515326,-58.525766), new GLatLng(-34.515220,-58.525533), new GLatLng(-34.515436,-58.525391), new GLatLng(-34.515470,-58.525402), new GLatLng(-34.515547,-58.525570), new GLatLng(-34.515543,-58.525621), new GLatLng(-34.515326,-58.525766)];
  var polygon = new GPolygon(pts, null, 0, 0.0, "#000000", 0.0 );
  map.addOverlay(polygon);
  
//  ======== Add a map overview ==========
      map.addControl(new GOverviewMapControl(new GSize(100,100)));

//  ======== A function to adjust the positioning of the overview ========
      function positionOverview(x,y) {
        var omap=document.getElementById("map_overview");
        omap.style.left = x+"px";
        omap.style.top = y+"px";
        
// == restyling ==
        omap.firstChild.style.border = "1px solid gray";

        omap.firstChild.firstChild.style.left="3px";
        omap.firstChild.firstChild.style.top="2px";
        omap.firstChild.firstChild.style.width="90px";
        omap.firstChild.firstChild.style.height="90px";
      }

//  ======== Cause the overview to be positioned AFTER IE sets its initial position ======== 
      setTimeout("positionOverview(558,254)",1);

	  function createMarker(point,html,direccion,barrio,ciudad,bus1,bus2,bus3,bus4,icontype) {
 
		var markerhtml = "";
		if (html != "") markerhtml += "<span class=\"logo\">" + "<img src=" + html + " + >" + "</span>" + "<br>";
		if (direccion != "") markerhtml += "<strong>" + direccion + "</strong><br>";
		if (barrio != "") markerhtml += barrio + "<br>";
		if (ciudad != "") markerhtml += ciudad + "<br>";
		if (bus1 != "") markerhtml += "<span class=\"busicon\">" + "<img src=" + bus1 + " + >" + "</span>";
		if (bus2 != "") markerhtml += "<span class=\"busicon\">" + "<img src=" + bus2 + " + >" + "</span>";
		if (bus3 != "") markerhtml += "<span class=\"busicon\">" + "<img src=" + bus3 + " + >" + "</span>";
		if (bus4 != "") markerhtml += "<span class=\"busicon\">" + "<img src=" + bus4 + " + >" + "</span>";

		var marker = new GMarker(point, gicons[icontype]);

		GEvent.addListener(marker, "click", function() {
		  marker.openInfoWindowHtml(markerhtml);
		});
		i++;

		return marker;
	  }

	}

	else {
	  alert("Lamentablemente, la aplicación Google no es compatible con su navegador");
	}
	
}
