addEvent(window,'load',inicializarEventos,false);
addEvent(window,'unload',finalizarEventos,false);

var map;
var ver=new Array();
function inicializarEventos()
{
  inicializarMenu('menu');
  var op=document.getElementById('map');
  alto=screen.height-200;
  op.style.height=alto+'px';

  var op1=document.getElementById('columna2');
  op1.style.height=alto+'px';



  if (GBrowserIsCompatible()) 
  {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(-36.491973, -64.291992), 5);
    map.setMapType(G_NORMAL_MAP);
	
    map.addControl(new GSmallMapControl()); 
    map.addControl(new GMapTypeControl());


    GEvent.addListener(map,"zoomend",function(oldLevel, newLevel) {
	  for(var x=0;x<indice;x++)
	    if (marcas[x].zoomvisible<=newLevel)
	      marcas[x].show();
		else
	      marcas[x].hide();  
	});


        var latlng = new GLatLng(-31.352611, -64.173374);

        var icon = new GIcon();
        icon.image = 'ver.png';
        icon.iconSize = new GSize(50, 25);
        icon.iconAnchor = new GPoint(16, 16);
        icon.infoWindowAnchor = new GPoint(25, 7);

        opts = { 
          "icon": icon,
          "clickable": true,
          "title": "Mostrar Diarios y Periódicos del País",
          "labelText": "VER",
          "labelOffset": new GSize(-6, -10)
        };
        ver[1] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[1], "click", function() {
		  ver[1].hide();
          actualizarMapa(-31.316101,-61.479492,5,1);
        });
        map.addOverlay(ver[1]);

        latlng = new GLatLng(-33.460661, -71.388474);
        ver[18] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[18], "click", function() {
		  ver[18].hide();
          actualizarMapa(-33.321349,-73.037109,5,18);
        });
        map.addOverlay(ver[18]);

        latlng = new GLatLng(-32.65036, -56.131897);
        ver[2] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[2], "click", function() {
		  ver[2].hide();
          actualizarMapa(-32.814978,-55.228271,7,2);
        });
        map.addOverlay(ver[2]);

        latlng = new GLatLng(-23.245132, -58.097076);
        ver[4] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[4], "click", function() {
		  ver[4].hide();
          actualizarMapa(-23.785345,-57.106934,6,4);
        });
        map.addOverlay(ver[4]);

        latlng = new GLatLng(-16.793366, -64.776077);
        ver[3] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[3], "click", function() {
		  ver[3].hide();
          actualizarMapa(-15.156974,-64.335937,6,3);
        });
        map.addOverlay(ver[3]);

        latlng = new GLatLng(-9.985081, -76.068649);
        ver[5] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[5], "click", function() {
		  ver[5].hide();
          actualizarMapa(-8.754795,-74.179687,5,5);
        });
        map.addOverlay(ver[5]);

        latlng = new GLatLng(-1.24419, -78.6022);
        ver[6] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[6], "click", function() {
		  ver[6].hide();
          actualizarMapa(-1.58183,-78.475342,7,6);
        });
        map.addOverlay(ver[6]);


        latlng = new GLatLng(3.840961,-74.803848);
        ver[7] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[7], "click", function() {
		  ver[7].hide();
          actualizarMapa(6.337137,-73.806152,6,7);
        });
        map.addOverlay(ver[7]);


        latlng = new GLatLng(7.866103,-65.592499);
        ver[8] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[8], "click", function() {
		  ver[8].hide();
          actualizarMapa(7.318882,-64.907227,6,8);
        });
        map.addOverlay(ver[8]);


        latlng = new GLatLng(9.308487,-79.051781);
        ver[9] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[9], "click", function() {
		  ver[9].hide();
          actualizarMapa(8.559294,-80.447388,8,9);
        });
        map.addOverlay(ver[9]);


        latlng = new GLatLng(9.931991,-84.073219);
        ver[10] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[10], "click", function() {
		  ver[10].hide();
          actualizarMapa(9.914744,-83.930054,8,10);
        });
        map.addOverlay(ver[10]);


        latlng = new GLatLng(12.928945,-85.112457);
        ver[11] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[11], "click", function() {
		  ver[11].hide();
          actualizarMapa(12.57601,-84.973755,8,11);
        });
        map.addOverlay(ver[11]);


        latlng = new GLatLng(13.658664,-89.679337);
        ver[12] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[12], "click", function() {
		  ver[12].hide();
          actualizarMapa(13.774066,-88.871155,9,12);
        });
        map.addOverlay(ver[12]);

        latlng = new GLatLng(15.506288,-86.993523);
        ver[13] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[13], "click", function() {
		  ver[13].hide();
          actualizarMapa(14.684568,-86.764526,8,13);
        });
        map.addOverlay(ver[13]);

        latlng = new GLatLng(15.582695,-90.32753);
        ver[14] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[14], "click", function() {
		  ver[14].hide();
          actualizarMapa(15.395432,-90.241699,8,14);
        });
        map.addOverlay(ver[14]);

        latlng = new GLatLng(25.554832,-103.468552);
        ver[15] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[15], "click", function() {
		  ver[15].hide();
          actualizarMapa(23.725012,-99.272461,5,15);
        });
        map.addOverlay(ver[15]);


        latlng = new GLatLng(21.923619,-79.563332);
        ver[16] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[16], "click", function() {
		  ver[16].hide();
          actualizarMapa(21.412162,-79.94751,7,16);
        });
        map.addOverlay(ver[16]);


        latlng = new GLatLng(19.503489,-71.345901);
        ver[17] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[17], "click", function() {
		  ver[17].hide();
          actualizarMapa(18.823117,-69.98291,8,17);
        });
        map.addOverlay(ver[17]);

        latlng = new GLatLng(42.595807,-5.583115);
        ver[19] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[19], "click", function() {
		  ver[19].hide();
          actualizarMapa(40.463666,-4.240723,6,19);
        });
        map.addOverlay(ver[19]);


        latlng = new GLatLng(18.361694,-67.107496);
        ver[20] = new LabeledMarker(latlng, opts);
        GEvent.addListener(ver[20], "click", function() {
		  ver[20].hide();
          actualizarMapa(17.976121,-65.901489,9,20);
        });
        map.addOverlay(ver[20]);



    actualizarNuevo();
  }
  
}


function actualizarMapa(la,lo,altura,codigopais)
{ 
    map.setCenter(new GLatLng(la,lo));
	x=parseInt(altura);
	map.setZoom(x);
   recuperarObjetos(codigopais);	
}




function marcaPresionada(overley,point)
{
  alert('se presiono');
}

function finalizarEventos()
{
  GUnload();
}


function inicializarMenu(m)
{
  var ob=document.getElementById(m);
  var menu=ob.getElementsByTagName('ul');
  for(f=1;f<menu.length;f++)
  {
    menu[f].style.display='none';
  }
  var enlaces=ob.getElementsByTagName('a');
  for(f=0;f<enlaces.length;f++)
  {
    addEvent(enlaces[f],'click',itemSeleccionado,false);
  }
}
function itemSeleccionado(e)
{
  var enlace;
  if (window.event)
    enlace=window.event.srcElement;
  else
    enlace=e.target;
  var padre=enlace.parentNode;
  var ul=padre.getElementsByTagName('ul');
  if (ul.length>0)
  {
    if (ul[0].style.display=='none')
      ul[0].style.display='block';
    else
      ul[0].style.display='none';
  }
}



function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else  
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

var conexion1;
var datos;
function  recuperarObjetos(codigoLocalidad)
{
  //map.clearOverlays();
  ver[codigoLocalidad].hide();
  conexion1=crearXMLHttpRequest();
  conexion1.onreadystatechange = procesarEventos;
  var aleatorio=Math.random();
  conexion1.open('GET','recuperarobjetos.php?codigolocalidad='+codigoLocalidad+"&aleatorio="+aleatorio, true);
  conexion1.send(null);
}



function procesarEventos()
{
  if(conexion1.readyState == 4)
  {
    
   datos=eval("(" + conexion1.responseText + ")");
    //datos=conexion1.responseText.parseJSON();
	if (datos)
      crearObjetos();
  } 
}

var marcas=new Array();
var indice=0;

function createMarker(localidad,latlng, url,descripcion,zoomvisible) {
      var vecurl=url.split(",");
	  var vecdescripcion=descripcion.split(',');
	  var html='<center><b>'+'<h2>'+localidad+'</h2><br>';
	  var x;
	  for(x=0;x<vecurl.length;x++)
	    html+='<a target="_blank" href="' + vecurl[x] + '">' + vecdescripcion[x] + '</a><br>';
	  html+='</b></center>';	
      var marker = new GMarker(latlng);
	  marker.zoomvisible=zoomvisible;
	  marcas[indice]=marker;
	  indice++;

      GEvent.addListener(marker,"click", function() {
        map.openInfoWindowHtml(latlng, html);
      });
      return marker;
	}


function crearObjetos()
{
	var f;
    for(f=0;f<datos.length;f++)
    { 
      var latlng = new GLatLng(datos[f].latitud,datos[f].longitud);
	  map.addOverlay(createMarker(datos[f].localidad,latlng, datos[f].url,datos[f].descripcion,datos[f].zoomvisible));
	}
    for(var x=0;x<indice;x++)
	  if (marcas[x].zoomvisible<=map.getZoom())
	    marcas[x].show();
	  else
	  {
	    marcas[x].hide();  
      }		

}

/*function crearObjetos()
{
  for(f=0;f<datos.length;f++)
  {
	var punto1 =new GLatLng(datos[f].latitud,datos[f].longitud);
    var marca1 =new GMarker(punto1);
	var descripcion=datos[f].descripcion;
	var url=datos[f].url;
    map.addOverlay(marca1);
    GEvent.addListener(marca1, "click", function() {    
	  var myHtml = '<center><b><a target="_blank" href="'+url+'">'+ descripcion + "</a></b></center>" ;    
	  map.openInfoWindowHtml(punto1, myHtml);  
	  });
  } 
}
*/
function marcaPresionada()
{
  alert('presionado');
}






function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}


