/*
 * Script de Belenario por Alberto Hernández
 */

	var side_bar_html;
	var gmarkers;
	var minzooms;
	var currentOpenedMarker;
	var map;
	var mgr;
	var mgrOptions;
	var gicons;
	var i;
   
      // A function to create the marker and set up the event window
      function createMarker(point,name,html,icontype,minzoom) {
        // === create a marker with the requested icon ===
        var marker = new GMarker(point, gicons[icontype]);
		var zoomIn = 10;
		if (icontype == "belenp" || icontype == "belenp2009" || icontype == "belenp2010") zoomIn = 10; // Los del foro belenista no están con tanta precisión!
		GEvent.addListener(marker, "click", function() {
			if(icontype == "belenp" || icontype == "belenp2009" || icontype == "belenp2010" || map.getZoom() < 7) {
				map.setZoom(zoomIn);
				map.panTo(marker.getPoint());
			}
		  currentOpenedMarker = marker;
		  marker.openInfoWindowHtml(html);
		});

        // save the info we need to use later for the side_bar
        gmarkers[icontype][i[icontype]] = marker;
		minzooms[icontype][i[icontype]] = minzoom;
        // add a line to the side_bar html
        // sidebar disabled side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
        i[icontype] = i[icontype]+1;
        return marker;
      }


	  function centrar(lugar){
		var zoomlevel;
		var lng;
		var lat;
	  	if (lugar=="peninsula"){
			zoomlevel = 6;
			lng = 39.907787;
			lat = -3.859741;
		}else if(lugar=="es"){
			zoomlevel = 5;
			lng = 36.207787;
			lat = -5.859741;
		}else if(lugar=="europa"){
			zoomlevel = 4;
			lng = 39.907787;
			lat = -3.859741;		
		}else if(lugar=="america"){
			zoomlevel = 6;
			lng = 39.907787;
			lat = -3.859741;		
		}else if(lugar=="sudamerica"){
			zoomlevel = 3;
			lat = -61.25;
			lng = -22.899741;	
		}else if(lugar=="centroamerica"){
			zoomlevel = 3;
			lng = 17.97873309555617;
			lat = -94.21875;		
		}else if(lugar=="norteamerica"){
			zoomlevel = 3;
			lng = 50.17689812200107;
			lat = -103.53515625;		
		}else if(lugar=="mundo"){
			zoomlevel = 2;
			lng = 21.907787;
			lat = -13.859741;		
		}
	    map.setCenter(new GLatLng( lng,lat), zoomlevel);
	  }

	  // Generates Navidad Digital info window
	  function createInfoWindow(label, img, desc, url, type){
	  	if (type == "belen"){
	  	  	return "<div class=\"infowindow\"> <a style=\"text-decoration:none; border:none\" href=\"" 
		        + url 
				+ "\"  onClick=\"javascript: pageTracker._trackPageview('/enlaces/belenario/" + url.substring(7) + "');\" target=\"_blank\"> <div style=\"float:left; width:100px\"><img src=\"" 
				+ img 
				+ "\" border=0 alt=\"miniatura\" width=95 height=71 /></div> <div style=\"float:left; width:200px\"><b>"
				+ label 
				+ "</b><br>"
				+ desc 
				+ "<br> <br><img src=\"estilos/icons/navidaddigital.png\" border=0 alt=\"logo\" width=139 height=12/> </div> </a> </div>";
		}else{
		  if (img != ""){
	  	  	return "<div class=\"infowindow\"> <a style=\"text-decoration:none; border:none\" href=\"" 
		        + url 
				+ "\"  onClick=\"javascript: pageTracker._trackPageview('/enlaces/belenario/" + url.substring(7) + "');\" target=\"_blank\"> <div style=\"float:left; width:100px\"><img src=\"" 
				+ img 
				+ "\" border=0 alt=\"miniatura\" width=95 height=71 /></div> <div style=\"float:left; width:200px\"><b>"
				+ label 
				+ "</b><br>"
				+ desc 
				+ "</div> </a> </div>";
		  }
		
	  	  	return "<div class=\"infowindow\" style=\"width:300px\"> <a style=\"text-decoration:none; border:none\" href=\"" 
		        + url 
				+ "\"  onClick=\"javascript: pageTracker._trackPageview('/enlaces/belenario/" + url.substring(7) + "');\" target=\"_blank\"> <b>"
				+ label 
				+ "</b><br>"
				+ desc 
				+ "</a> </div>";		
		}
				
				
	   }

	 function setMarkers(type){
		if (currentOpenedMarker != null) currentOpenedMarker.closeInfoWindow();
		currentOpenedMarker = null;
	   mgr.clearMarkers();
	   for (var j = 0; j < i[type]; j++){
	     mgr.addMarker(gmarkers[type][j], minzooms[type][j]);
		}
	 }
	
    function MostrarMapa()
    {
	  if (GBrowserIsCompatible()) {
      // this variable will collect the html which will eventualkly be placed in the side_bar
      side_bar_html = "";
    
      // arrays to hold copies of the markers and html used by the side_bar
      // because the function closure trick doesnt work there
      gmarkers = [];
      minzooms = [];
	  
	  gmarkers["belen"] = [];
  	  gmarkers["belenp"] = [];
	  gmarkers["belenp2009"] = [];
	  gmarkers["belenp2010"] = [];
	  
      minzooms["belen"] = [];
      minzooms["belenp"] = [];	 
	  minzooms["belenp2009"] = [];	 
	  minzooms["belenp2010"] = [];	 
	  
	  currentOpenedMarker = null;
	  
      i = [];
	  i["belen"] = 0;
	  i["belenp"] = 0;
	  i["belenp2009"] = 0;
	  i["belenp2010"] = 0;
      
      // === Create an associative array of GIcons() ===
      gicons = [];
      gicons["belen"]  = new GIcon(G_DEFAULT_ICON, "estilos/icons/belen-casa.png");
   	  gicons["belen"].iconSize = new GSize(32,32);
      gicons["belenp"] = new GIcon(G_DEFAULT_ICON, "estilos/icons/belenp-icon-2008.png");
	  gicons["belenp"].iconSize = new GSize(32,32);   
	  gicons["belenp2009"] = new GIcon(G_DEFAULT_ICON, "estilos/icons/belenp-icon-2009.png");
	  gicons["belenp2009"].iconSize = new GSize(32,32);  
	  gicons["belenp2010"] = new GIcon(G_DEFAULT_ICON, "estilos/icons/belenp-icon-2010.png");
	  gicons["belenp2010"].iconSize = new GSize(32,32);  
	  
      // create the map
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
	  map.enableScrollWheelZoom();
	  map.enableDoubleClickZoom();
      map.setCenter(new GLatLng(21.907787, -13.859741), 2);
      map.addControl(new GLargeMapControl());
      map.addControl(new GOverviewMapControl());
  
 	  mgrOptions = { borderPadding: 0, maxZoom: 17, trackMarkers: false };
      mgr = new MarkerManager(map, mgrOptions);
      // Read the data from markers.xml
      var request = GXmlHttp.create();
      request.open("GET", "markers.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
		  
          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 GLatLng(lat,lng);
            var label = markers[i].getAttribute("label");
            // === read the icontype attribute ===
            var icontype = markers[i].getAttribute("icontype");

            var html = markers[i].getAttribute("html");
			if (html == null || html == ""){
				html = createInfoWindow(label, markers[i].getAttribute("img"), markers[i].getAttribute("desc"), markers[i].getAttribute("url"), icontype);
			}
            var minZoom = parseInt(markers[i].getAttribute("z"));

            // === create the marker, passing the icontype string ===
            var marker = createMarker(point,label,html,icontype,minZoom);
			mgr.addMarker(marker, minZoom);
          }
          // put the assembled side_bar_html contents into the side_bar div
          document.getElementById("side_bar").innerHTML = side_bar_html;
        }
      }
      request.send(null);
    }

    else {
      alert("Los mapas de esta p&aacute;gina no son compatibles con este navegador");
    }

    }
