// JavaScript Document
function mail_pie()
{
	if(!document.getElementById('correo_pie')) return false;
	var correo_consulta_pie="<a href=\"mailto:webmaster@bilbaofotos.com\" class=\"enlacenounder\">webmaster@bilbaofotos.com</a> ";
	document.getElementById('correo_pie').innerHTML=correo_consulta_pie;
}


function externalLinks(){
 if (!document.getElementsByTagName) return;
   var anchors = document.getElementsByTagName("a");
   for (var i=0; i<anchors.length; i++){
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
      anchor.target = "_blank";
     }
   }
}
/*
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}
*/


///////////////
function loadmap() {
      if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());		map.setCenter(new GLatLng(43.25547101376527, -2.924208641052246), 16, G_HYBRID_MAP);

				var icono3 = new GIcon();
		icono3.image = "http://www.bilbaofotos.com/images/icon3.png";
		icono3.iconSize = new GSize(20, 34);
		icono3.iconAnchor = new GPoint(10, 34);
		var marker = new GMarker(new GLatLng(43.25547101376527, -2.924208641052246), icono3);
        map.addOverlay(marker);
		
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();	
		makeRequest_fotos(southWest.lng(), northEast.lng(), southWest.lat(), northEast.lat());
		
		GEvent.addListener(map, "moveend", function() {
			var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();	
			makeRequest_fotos(southWest.lng(), northEast.lng(), southWest.lat(), northEast.lat());	
			});
				
      }


////////////////
function makeRequest_fotos(minlongitud, maxlongitud, minlatitud, maxlatitud) {

	var http_request = false;
	var minlongitud = minlongitud;
	var maxlongitud = maxlongitud;
	var minlatitud = minlatitud;
	var maxlatitud = maxlatitud;
	
	var url = "http://www.bilbaofotos.com/javascript/fotos_zoom.php?mapa=foto&minlongitud="+minlongitud+"&maxlongitud="+maxlongitud+"&minlatitud="+minlatitud+"&maxlatitud="+maxlatitud;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		//alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { 
		// do the thing

		alertContents_foto_zoom(http_request); 
	};
	
	http_request.open('GET', url, true);
	http_request.send(null);
	

}

////////////////
function alertContents_foto_zoom(http_request) {
	
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
				
			var icono_s = new GIcon();
			icono_s.image = "http://www.bilbaofotos.com/images/icon2.png";
			icono_s.iconSize = new GSize(20, 34);
			icono_s.iconAnchor = new GPoint(10, 34);
			
			
			var xmldoc = http_request.responseXML;			
			var xml_resultados = xmldoc.getElementsByTagName('resultados').item(0);
			resultados= xml_resultados.firstChild.data;			
			
			
			function createMarker(icono, url_foto_total, long, lat) {
			  var marker = new GMarker(new GLatLng(lat, long),icono);
			  GEvent.addListener(marker, "click", function() {
			  			    location.replace("http://www.bilbaofotos.com/"+url_foto_total+"/");
			  			  });
			  return marker;
			}
			
			for(i=0; i<(resultados-1); i++)
				{
				
				var xml_info_foto_inmu = xmldoc.getElementsByTagName('url_foto_total').item(i);
				var url_foto_total= xml_info_foto_inmu.firstChild.data;
		
				var xml_longitud = xmldoc.getElementsByTagName('longitud').item(i);
				var longitud= xml_longitud.firstChild.data;
				
				var xml_latitud = xmldoc.getElementsByTagName('latitud').item(i);
				var latitud= xml_latitud.firstChild.data;							
				
				map.addOverlay(createMarker(icono_s, url_foto_total, longitud, latitud));
	
				}

				var icono3 = new GIcon();
		icono3.image = "http://www.bilbaofotos.com/images/icon3.png";
		icono3.iconSize = new GSize(20, 34);
		icono3.iconAnchor = new GPoint(10, 34);
		var marker = new GMarker(new GLatLng(43.25547101376527, -2.924208641052246), icono3);
        map.addOverlay(marker);
					
		} else {
			alert('Ha habido un problema con la solicitud. Inténtalo más tarde');
		}
	}

}
}
////////

window.onload = function() {
  loadmap();
  mail_pie();
  externalLinks();
  //correctPNG();
};


