var xml_file = "/mapas/locations.xml";
// Eliminamos el refresco automático
//var refresh = 300; // In seconds

// Sitio Central: Sevilla
// var my_site = new GPoint (-5.983330, 37.383300);
// Lo desviamos un poco para que no salga encima de la ciudad
var my_site = new GPoint (-6, 37.38);
var cpoint = new GPoint (2.646418, 39.618251);

var hqpoint;
var baseicon;
var hqicon;
var map;
var hqmarker;
var map;


window.onload=drawMap;

function drawMap ()
{
  hqpoint = my_site;
  baseicon = new GIcon ();
  baseicon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  baseicon.iconSize = new GSize (12, 20);
  baseicon.shadowSize = new GSize (12, 20);
  baseicon.iconAnchor = new GPoint (12, 20);
  baseicon.infoWindowAnchor = new GPoint (5, 1);

  var hqicon = new GIcon (baseicon);
  hqicon.image = "http://labs.google.com/ridefinder/images/mm_20_white.png";

  // Center the map
  map = new GMap (document.getElementById ("map"));
  map.addControl (new GLargeMapControl ());
  map.addControl (new GMapTypeControl ());
  map.centerAndZoom (cpoint, 15);


  hqmarker = new GMarker (hqpoint, hqicon);
  var hqhtml = "<small><b>http://www.guadalinex.org/<br />Guadalinex se hace aquí</b><br>";
  GEvent.addListener (hqmarker, "click", function () {
		      hqmarker.openInfoWindowHtml (hqhtml);
		      }
  );
  load ();
}

function cz (p, z)
{
  map.centerAndZoom (p, z);
}

// Creates a marker whose info window displays the given number
function createMarker (point, ip, city, country)
{
  var icon = new GIcon (baseicon);
  if (ip < 25)
    color = 'green';
  else if (ip < 500)
    color = 'blue';
  else if (ip < 2000)
    color = 'yellow';
  else if (ip < 5000)
    color = 'orange';
  else if (ip < 20000)
    color = 'red';
  else
    color = 'purple';
  icon.image =
    "http://labs.google.com/ridefinder/images/mm_20_" + color + ".png";

  var marker = new GMarker (point, icon);

  // Show this marker's index in the info window when it is clicked
  var msg = "<small><b>Accesos:</b> " + ip + "<br/>";
  msg = msg + "<b>Ciudad:</b> " + city + "<br/>";
  msg = msg + "<b>País:</b> " + country;
  msg = msg + "</small>";

  GEvent.addListener (marker, "click", function ()
		      {
		      marker.openInfoWindowHtml (msg);
		      }
  );

  return marker;
}

function load ()
{
  // Download the data in map.xml and load it on the map.
  var request = GXmlHttp.create ();
  var time;
  request.open ("GET", xml_file, true);
  request.onreadystatechange = function ()
  {
    if (request.readyState == 4)
      {
	map.clearOverlays ();
	map.addOverlay (hqmarker);
	var xmlDoc = request.responseXML;
	var markers = xmlDoc.documentElement.getElementsByTagName ("marker");
	for (var i = 0; i < markers.length; i++)
	  {
	    var point =
	      new GPoint (parseFloat (markers[i].getAttribute ("lng")),
			  parseFloat (markers[i].getAttribute ("lat")));
	    var marker = createMarker (point, markers[i].getAttribute ("ip"),
				       markers[i].getAttribute ("city"),
				       markers[i].getAttribute ("country"));
	    map.addOverlay (marker);
	  }
      }
  }
  //hqmarker.openInfoWindowHtml(hqhtml);
  request.send (null);
  if((element = document.getElementById("clock"))) {
  	time = dat_time();
  	element.innerHTML=time;
  }
//  window.setTimeout ("load()", refresh*1000);
}

function dat_time() {
   var d = new Date();
   var hour = d.getHours();
   var min = d.getMinutes();
   var sec = d.getSeconds();
   if (hour < 10) hour = "0" + hour;
   if (min < 10) min = "0" + min;
   if (sec < 10) sec = "0" + sec;
   var t = hour + ":" + min + ":" + sec;
   return t;
}


