/*
*  Google Maps.
*/

// Variables generales

//Mapa
var map = null;
var gMarker = null;
//Puntos
var puntos = [];

//anadirPunto(latitud, longitud, zoom, titulo, descripcion, imagen)
function cargaMapaEspecifico(capa, latitud, longitud, zoom, titulo, descripcion, imagen) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(capa);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(latitud, longitud), zoom);
		map.clearOverlays();
		anadirPunto(latitud, longitud, zoom, titulo, descripcion, imagen);
	}
}


function cargaMapaGeneral(capa, latitud, longitud, zoom) {
	map = new GMap2(capa);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(latitud, longitud), zoom);
	map.clearOverlays();
	//Añadir puntos en el mapa
	anadirPuntos();
}

function crearMarker(latitud, longitud, zoom, titulo, descripcion, imagen){
      //alert("crearMarker: " + latitud + ", " + longitud + ", " + zoom + ", " + titulo + ", " + descripcion + ", " + imagen);
      var icono = new GIcon();
      icono.image = imagen;
      icono.iconSize = new GSize(29, 21);
      icono.shadowSize = new GSize(29, 21);
      icono.iconAnchor = new GPoint(6, 20);
      icono.infoWindowAnchor = new GPoint(5, 1);    
      var punto= new GLatLng(latitud, longitud);
      var markerOptions = null;
      if(titulo != ''){
      	markerOptions = {icon:icono, title: titulo};
      }else{
      	markerOptions = {icon:icono};
      }
      
      var marker= new GMarker(punto, markerOptions);
      // var descripcion1= descripcion + "<p><a href='#'>Pincha aqu&iacute;</a></p>";
      
      if(descripcion != ''){
        GEvent.addListener(marker, "click", function() {
	   marker.openInfoWindowHtml(descripcion); } 
        );
       
      }
      return marker;
}

function anadirPunto(latitud, longitud, zoom, titulo, descripcion, imagen){
	var marker = crearMarker(latitud, longitud, zoom, titulo, descripcion, imagen);
	map.addOverlay(marker);
	return marker;
}


function CustomGetTileUrl(a,b,c) {	     

	this.myStyles="";
       var width= 256;
	var height= 256;
	var lULP = new GPoint(a.x*width,(a.y+1)*height);
	var lLRP = new GPoint((a.x+1)*width,a.y*height);

	var lUL = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP,b,c);
	var lLR = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP,b,c);
		
	var lBbox=lUL.x+","+lUL.y+","+lLR.x+","+lLR.y;
	var lSRS="EPSG:4326";
	var lURL=this.myBaseURL;
	lURL+="&REQUEST=GetMap";
	lURL+="&SERVICE=WMS";
	lURL+="&VERSION=1.1.1";
	lURL+="&LAYERS="+this.myLayers;
	lURL+="&STYLES="+this.myStyles;
	lURL+="&FORMAT="+this.myFormat;
	//lURL+="&BGCOLOR=0xFFFFFF";
	lURL+="&TRANSPARENT=FALSE";
	lURL+="&SRS="+lSRS;
	lURL+="&BBOX="+lBbox;
	lURL+="&WIDTH=" + width;
	lURL+="&HEIGHT=" + height;
	lURL+="&reaspect=false";
       //document.write(lURL + "<br/>");
	return lURL;


}

function ZoomMapTo(num) {
	map.setZoom(num);
}
/******  TRestricter  **********************************************/

// Constructor
TRestricter = function (map) {
  this.map = map;
}

// Función que activa la limitación del desplazamiento entre la esquina inferior izquierda
// y la esquina superior derecha
TRestricter.prototype.restrict = function (sw, ne) {
  this.map._allowedBounds = new GLatLngBounds(sw, ne);
  GEvent.addListener(this.map, 'move', this.checkBounds);
}

// Función que desactiva la limitación del desplazamiento
TRestricter.prototype.unrestrict = function () {
  this.map._allowedBounds = null;
}

// Listener encargado de comprobar el desplazamiento
TRestricter.prototype.checkBounds = function() {
  if (!this._allowedBounds || this._allowedBounds.contains(this.getCenter())) return;
  var x = Math.min(Math.max(this.getCenter().lng(), this._allowedBounds.getSouthWest().lng()), this._allowedBounds.getNorthEast().lng());
  var y = Math.min(Math.max(this.getCenter().lat(), this._allowedBounds.getSouthWest().lat()), this._allowedBounds.getNorthEast().lat());
  this.setCenter(new GLatLng(y,x));
}

// Establece los límites de zoom del mapa
TRestricter.prototype.zoomLevels = function (min, max) {
  var array = this.map.getMapTypes() || [];
  for (var i=0; i<array.length; i++) {
    array[i].getMinimumResolution = function () { return min };
    array[i].getMaximumResolution = function () { return max };
  }
}  

