﻿// Entra dades.js
var map;
var geocoder;
var deselectCurrent = function() {};
var gmarkers = [];
var i = 0;
var center = new GLatLng(40.497092,-3.515625); //41.705728515237524, 1.7631689453125);
var zoom = 5;
var lastmarker;
var clickedpoint;
var pnts=new Array();
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

var MapaVeure = function () {
	Mapa.init();
	Mapa.llegeixXML();
}

var iconBlue = new GIcon(); 
	iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
	iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	iconBlue.iconSize = new GSize(12, 20);
	iconBlue.shadowSize = new GSize(22, 20);
	iconBlue.iconAnchor = new GPoint(6, 20);
	iconBlue.infoWindowAnchor = new GPoint(5, 1);

var iconRed = new GIcon(); 
	iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
	iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	iconRed.iconSize = new GSize(12, 20);
	iconRed.shadowSize = new GSize(22, 20);
	iconRed.iconAnchor = new GPoint(6, 20);
	iconRed.infoWindowAnchor = new GPoint(5, 1);

var iconGreen = new GIcon(); 
	iconGreen.image = 'http://labs.google.com/ridefinder/images/mm_20_green.png';
	iconGreen.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	iconGreen.iconSize = new GSize(12, 20);
	iconGreen.shadowSize = new GSize(22, 20);
	iconGreen.iconAnchor = new GPoint(6, 20);
	iconGreen.infoWindowAnchor = new GPoint(5, 1);	
	
var iconYellow = new GIcon(); 
	iconYellow.image = 'http://labs.google.com/ridefinder/images/mm_20_yellow.png';
	iconYellow.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	iconYellow.iconSize = new GSize(12, 20);
	iconYellow.shadowSize = new GSize(22, 20);
	iconYellow.iconAnchor = new GPoint(6, 20);
	iconYellow.infoWindowAnchor = new GPoint(5, 1);	
	
var customIcons = [];
	customIcons["1"] = iconRed;
	customIcons["2"] = iconBlue;
	customIcons["3"] = iconGreen;
	customIcons["4"] = iconYellow;	
//var markerGroups = { "1": [], "2": [], "3": [], "4": []};	

// La funció CatControl dona format als controls de les capes visibles

function CatControl() {}

CatControl.prototype = new GControl();

CatControl.prototype.initialize = function(map) {
	var container = document.createElement("div");

	// botons
	var mapbtn = document.createElement("div");
	mapbtn.appendChild(document.createTextNode("Mapa"));
	this.setButtonStyle_(mapbtn);
	GEvent.addDomListener(mapbtn, "click", function() {
    	map.setMapType(G_NORMAL_MAP);
	});
	
	var satelitbtn = document.createElement("div");
	satelitbtn.appendChild(document.createTextNode("Satèl·lit"));
	this.setButtonStyle_(satelitbtn);
	GEvent.addDomListener(satelitbtn, "click", function() {
    	map.setMapType(G_SATELLITE_MAP);
		//map.setMapType(G_HYBRID_MAP);
	});
	
	var fisicbtn = document.createElement("div");
	fisicbtn.appendChild(document.createTextNode("Relleu"));
	this.setButtonStyle_(fisicbtn);
	GEvent.addDomListener(fisicbtn, "click", function() {
    	map.setMapType(G_PHYSICAL_MAP);
	});

	var zoomAll = document.createElement("div");
	//zoomAll.appendChild(document.createTextNode("Reinicia<br>prova"));
	zoomAll.innerHTML = "Àmbit<br>inicial";
	this.setButtonStyle2_(zoomAll);
	GEvent.addDomListener(zoomAll, "click", function() {
    	Mapa.zoomFull();
	});
	
	container.appendChild(mapbtn);
	container.appendChild(fisicbtn);
	container.appendChild(satelitbtn);
	container.appendChild(zoomAll);

	map.getContainer().appendChild(container);
	return container;
}

CatControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

CatControl.prototype.setButtonStyle_ = function(button) {
  // button.style.textDecoration = "underline";
  button.style.color = "#000";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "1px";
  button.style.textAlign = "center";
  button.style.width = "5em";
  button.style.cursor = "pointer";
}

CatControl.prototype.setButtonStyle2_ = function(button) {
  // button.style.textDecoration = "underline";
  button.style.color = "#000";
  button.style.backgroundColor = "#CCCFBF";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "1px";
  button.style.textAlign = "center";
  button.style.width = "5em";
  button.style.cursor = "pointer";
}

var Mapa={
	init:function() {
		if (GBrowserIsCompatible()) {
			
			map = new GMap2(document.getElementById("map"));
			
			map.setMapType(G_PHYSICAL_MAP);
        	map.addControl(new GLargeMapControl());
			map.addControl(new CatControl());
			map.setCenter(center, zoom);
			map.enableScrollWheelZoom();
		} else {
			alert("El seu navegador no és compatible amb l'aplicació de mapes");
		}
	},
	llegeixXML:function() {
        GDownloadUrl("/ericons/xml/validats/", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("erico");
          for (var i = 0; i < markers.length; i++) {
            var id = markers[i].getAttribute("id");
            var descr = markers[i].getAttribute("descr");
            var observ = markers[i].getAttribute("obs");
            var tipus = markers[i].getAttribute("tipus");
            var prec = markers[i].getAttribute("prec");
            var cita = markers[i].getAttribute("cita");
            var autor = markers[i].getAttribute("autor");
            var data = markers[i].getAttribute("data");
            var valid = markers[i].getAttribute("valid");
           
           var img1 = markers[i].getAttribute("img1");
           var img2 = markers[i].getAttribute("img2");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("long")));
            var marker = Mapa.createMarker(point, id, descr, observ, tipus, prec, cita, autor, data, valid,img1,img2);
            map.addOverlay(marker);
				
          }
		  	  
        });
    
	
	},
          
	createMarker:function(point, id, descr, observ, tipus, prec, cita, autor, data, valid,img1,img2) {
		var marker = new GMarker(point, customIcons[tipus]);
		pnts.push(marker);
		//markerGroups[tipus].push(marker);
		// Si es fa clic sobre un marker
	  	GEvent.addListener(marker, 'click', function() {
			clickedpoint=marker.getLatLng();
			marker.openInfoWindowHtml(makeWindow2(id,data,autor,tipus,cita,prec,observ,img1,img2));
	  	});
	  	return marker;
	},
	
	//Zoom a resolució correcta
	zoomOn: function() {
		map.closeInfoWindow();
		map.setMapType(G_SATELLITE_MAP);
		map.setCenter(clickedpoint, 16);
		//map.panTo(clickedpoint);
	},
	
	//Zoom a l'àmbit inicial
	zoomFull: function() {
		map.closeInfoWindow();
		map.setCenter(center, zoom);
		map.setMapType(G_PHYSICAL_MAP);
	}
	

} // FI DE LA VARIABLE Mapa

// Les següents funcions retornen l'HTML de les InfoWindows
function makeWindow2(id,data,autor,tipus,cita,prec,observ,img1,img2) {
 
       if ( tipus == 1 ) {
          tipus = 'Indeterminat'; } else
	   if ( tipus == 2 ) {
          tipus = '<em>Erinaceus europaeus</em>'; } else 
       if ( tipus == 3 ) {
          tipus = '<em>Atelerix algirus</em>'; } else 
       if ( tipus == 4 ) {
          tipus = '<em>Atelerix algirus x albiventris</em>'; } else {
          tipus = tipus; }

       if ( cita == 1 ) {
          cita = 'Atropellat'; } else 
       if ( cita == 2 ) {
          cita = 'Viu amb aproximació'; } else 
       if ( cita == 3 ) {
          cita = 'Viu sense aproximació'; } else {
          cita = cita; }		  

       if ( prec == 1 ) {
          prec = 'Exacte'; } else
	   if ( prec == 2 ) {
          prec = 'Aproximat'; } else 
       if ( prec == 3 ) {
          prec = 'Municipi'; } else 
       if ( prec == 4 ) {
          prec = 'Bastant Imprecís'; } else {
          prec = prec; }
   
       if ( observ == null ) { observ = '' }

	   if (! img1 ) {
			img1 = '';
	   } else {
			img1 = "<a href=\"/galanthus_media/" + img1 + "\" rel='lightbox[Galeria]' title='Foto 1'>Foto 1</a>&nbsp;";
	   }
	   if (! img2 ) {
			img2 = '';
	   } else {
			img2 = "<a href=\"/galanthus_media/" + img2 + "\" rel='lightbox[Galeria]' title='Foto 2'>Foto 2</a>&nbsp;";
	   }
		  
	var html = "<div class='Etiqueta'><h4>" + tipus + "</h4><b>Data:</b> " + data + "<br/><b>Autor:</b> " + autor + "<br/><b>Tipus de cita:</b> " + cita + "<br/><b>Precisió:</b> " + prec + "<br/><b>Observacions:</b> " + observ + "<br/><b>Fotografies:</b> " + img1 + img2 +
	"<p class='EtiquetaPeu'><a href='#' onclick='Mapa.zoomOn(); return false;'>Amplia aquesta zona</a> | <a href='#' onclick='Mapa.zoomFull(); return false;'>Àmbit general</a></p></div>";

    return html; 
}
