//<![CDATA[
var v_marcadors=new Array();
var v_marcadors_dades=new Array();
var map = null;
var opcions = null;
// Manteniments i cercadors
var marcador_cerca = null;
var zoom_defecte = null;
var opc_marcador_cerca = null;
// Icones
var url_icona_dades = null;
var url_icona_afegir = null;
var ample_icona = 20;
var alt_icona = 28;
var desp_ample_icona = 4;
var desp_alt_icona = alt_icona +1;

// Control
var control_pos_x = 0;
var control_pos_y = 0;
var control_posicio = G_ANCHOR_TOP_RIGHT;
var control_html = "";

// Objecte del control generic
function Control_Generic () {
}

Control_Generic.prototype = new GControl();

Control_Generic.prototype.initialize = function(map) {
  var container = document.createElement("div");
  //var cercaDiv = document.createElement("div");
  //container.appendChild(cercaDiv);
  container.innerHTML=control_html;
  map.getContainer().appendChild(container);
  return container;
}

Control_Generic.prototype.getDefaultPosition = function() {
	return new GControlPosition(control_posicio, new GSize(control_pos_x, control_pos_y));
	}

// Funcions privades
function afegir_marcador (latitud, longitud, contingut, opcions_marcador, manteniment) {
	var marcador =new GMarker(new GLatLng(latitud, longitud) , opcions_marcador);
	if (manteniment==1) {
		GEvent.addListener(marcador, "click", function() {
			actualitzar_coordenades (latitud, longitud)
			marcador.openInfoWindowHtml(contingut);
			} );
	} else {
		GEvent.addListener(marcador, "click", function() {
			marcador.openInfoWindowHtml(contingut);
			} );
	}
	map.addOverlay(marcador);
	v_marcadors.push(marcador);
}

function actualitzar_coordenades (latitud, longitud) {
	document.getElementById('p_latitud').value = latitud;
	document.getElementById('p_longitud').value = longitud;
}

function crear_marcador_cerca (coordenades, zoom, search, draggable, opcions) {
	
	marcador_cerca = new GMarker(coordenades, opcions);
	map.addOverlay(marcador_cerca);
	actualitzar_coordenades(coordenades.lat(), coordenades.lng())
	map.panTo(coordenades);
	if (search==1) { 
		map.setCenter(coordenades, zoom);
	}
	if (draggable==1) {
		GEvent.addListener(marcador_cerca, "dragend", function () { actualitzar_coordenades(marcador_cerca.getLatLng().lat(), marcador_cerca.getLatLng().lng())});
	}
}

function esborrar_marcador_cerca () {
	if (marcador_cerca!=null) {
		map.removeOverlay (marcador_cerca);
	}
}

function cercar_lloc (nom_lloc) {
  if (nom_lloc.length > 0) {
    var localitzador = new GClientGeocoder();
    localitzador.getLatLng(nom_lloc,
      function(lloc) {
        if (!lloc) {
          alert('Lloc no trobat ( "' + nom_lloc + '" )');
		  actualitzar_coordenades (null, null);
		  esborrar_marcador_cerca ();
        } else {
			esborrar_marcador_cerca ();
			crear_marcador_cerca (lloc, zoom_defecte, 1, 1, opc_marcador_cerca);
        }
      } );
  }
}

function inicialitzacio_presentacio () {
	return 0;
}

function inicialitzacio_manteniment() {
	// Icona de nou lloc
	var icona_cerca = new GIcon (G_DEFAULT_ICON, url_icona_afegir);
	icona_cerca.iconSize=new GSize(ample_icona, alt_icona);
    icona_cerca.shadowSize=new GSize(ample_icona, alt_icona);
	icona_cerca.iconAnchor = new GPoint(desp_ample_icona, desp_alt_icona);
	opc_marcador_cerca = { icon:icona_cerca, draggable:true };

	GEvent.addListener (
		map, 
		"click", 
		function(overlay, lloc) {
			if (lloc) {
				esborrar_marcador_cerca ();
				crear_marcador_cerca (lloc, zoom_defecte, 0, 1, opc_marcador_cerca);
				}
			}
		);
}

function inicialitzar_mapa (mode_mapa, tipus_mapa, zoom_defecte, latitud_centre, longitud_centre) {
	if (document.getElementById("mapa")!=null) { 
		if (!GBrowserIsCompatible()) {
			alert('Navegador no compatible amb Google Maps');
			return;
		} else {
			map = new GMap2(document.getElementById("mapa"));
			// Barra de Zoom
			map.addControl(new GLargeMapControl());
			// Triem el tipus de mapa 
			map.setMapType(tipus_mapa);
			//Centrar el mapa 
			map.setCenter(new GLatLng(latitud_centre, longitud_centre), zoom_defecte);
			//Zoom amb la roda del ratolí
			map.enableScrollWheelZoom();

			switch (mode_mapa) {
			case "MANTENIMENT":
				inicialitzacio_manteniment();
				break;
			case "PRESENTACIO":
				inicialitzacio_presentacio();
				break;
			default:
				alert("Paràmetre incorrecte: " + mode_mapa);
			}
		}
	}
}

//]]>
