/* File: googlemap.js
 * Author: Dani Lupión of Gestión y Máketing en Internet
 * Last Modification: 16/02/2009
 *
 * Description: Generación de mapas de Google Maps
 */

/**
 * Parametros permitidos para el control de GoogleMap
 *
 * ---- Posicionamiento del Mapa ----
 * MAP_LAT: float (obligatorio) Latitud del centro del mapa
 * MAP_LON: float (obligatorio) Longitud del centro del mapa
 * MAP_ZOOM: int (obligatorio) Zoom del mapa
 * MAP_TYPE: string [NORMAL | HYBRID | PHYSICAL | SATELLITE | GOOGLE_EARTH](opcional) Tipo de mapa por defecto
 *
 * ---- Id's de inicialización del mapa ----
 * MAP_ID: string (obligatorio) Id del elemento que contiene al mapa
 * DIRECTIONS_ID: string (obligatorio si se muestran direcciones) Id del elemento que contiene las indicaciones
 * 
 * ---- Controles de GoogleMap ----
 * TYPE_CONTROL: boolean (opcional) Control para el tipo de mapa.
 * TYPE_COMBO_CONTROL: boolean (opcional) Control para el tipo de mapa con desplegable.
 * LARGE_MAP_CONTROL: boolean (opcional) Control de movimiento y zoom grande.
 * SMALL_MAP_CONTROL: boolean (opcional) Control de movimiento y zoom pequeño.
 * ZOOM_MAP_CONTROL: boolean (opcional) Control de zoom muy pequeno
 * OVERVIEW_CONTROL: boolean (opcional) Control de zona vista en el mapa.
 * LAYER_CONTROL: boolean (opcional) Control different layers.
 * DISABLE_MAP_DRAGGIN: boolean (opcional) Deshabilita el arrastre del mapa.
 *
 * ---- Colecicones y opciones sobre colecciones del mapa ----
 * MAP_MARKERS: array (opcional) Marcadores que contendrá el mapa
 * MAP_TRANSITIONS: array (opcional) Transiciones que realizará el mapa
 * MAP_TRANSITIONS_TIMEOUT: int (obligatorio si se establecen transiciones) Timeout por defecto de cada transicion
 *
 * ---- Marcadores de mapa ----
 * MARKER_LAT: float (obligatorio para cada marcador) Latitud del marcador
 * MARKER_LON: float (obligatorio para cada marcador) Longitud del marcador
 * MARKER_ICON: string (opcional) Ruta al icono del marcador
 * MARKER_ICON_WIDTH: int (opcional) Anchura del icono
 * MARKER_ICON_HEIGHT: int (opcional) Altura del icono
 * MARKER_DRAGGABLE: boolean (opcional) Establece si el marcador es arrastable
 * MARKER_UPDATE_WITH_MAP: boolean (opcional) Establce si el marcador se mueve
 *                         con el mapa (de momento solo tiene sentido si está posicionado en el centro del mapa
 * MARKER_DRAGGABLE_FUNCTION: function(float latitud, float longitud) (opcional)
 *                            Establece la función a ser llamada cuando un marcador termina de arrastraser
 * MARKER_CLICK_TEXT: string (opcional) Texto o html a mostrar en la ventana de información al pinchar sobre el marcador
 * MARKER_INIT_TEXT: string (opcional) Texto o html que aparecerá inicialmente en ventanda de información al cargar el marcador
 * 
 * ---- Parametors de tansiciones ----
 * LAT: float Latitud de la posicion de la transicion
 * LON: float Longitud de la posicion de la transicion
 * ZOOM: int Zoom de la transicion
 * TIMEOUT: int Timeout de la transicion
 * 
 * ----- Parámetros de capas ------
 * ENABLE_STREET_VIEW_LAYER: boolean (opcional) Añade el control de street view al control de capas
 * ENABLE_WIKIPEDIA_LAYER: boolean (opcional) Añade el control de wikipedia al control de capas						  
 * ENABLE_PANORAMIO_LAYER: boolean (opcional) Añade el control de panoramio al control de capas,
 * ENABLE_YOUTUBE_LAYER: boolean (opcional) Añade el control de youtube al control de capas
 * SHOW_STREET_VIEW_LAYER: boolean (opcional) Muestra por defecto la capa de street view
 * SHOW_WIKIPEDIA_LAYER: boolean (opcional) Muestra por defecto la capa de wikipedia						  
 * SHOW_PANORAMIO_LAYER: boolean (opcional) Muestra por defecto la capa de panoramio
 * SHOW_YOUTUBE_LAYER: boolean (opcional) Añade el control de youtube al control de capas 
 */

/**
 * Constructor
 * @param params Array con los parametros para inicializar el mapa
 */
function GoogleMap(params) {
	if (GBrowserIsCompatible()) {
		var self = this;
   		this.params = params;   		
		this.googleGeocoder = new GClientGeocoder();
   		this.googleMap = new GMap2(document.getElementById(params['MAP_ID']));
   		this.googleMap.self = this;
   		this.googleMap.addMapType(G_PHYSICAL_MAP);
   		this.googleMap.addMapType(G_SATELLITE_3D_MAP); 
   		
   		// Controles
   		if(params['TYPE_CONTROL']){
   			this.googleMap.addControl(new GMapTypeControl()); 		
   		}else if(params['TYPECOMBO_CONTROL']){
   			this.googleMap.addControl(new GMenuMapTypeControl());
   		}
   		if(params['LARGE_MAP_CONTROL']){
   			this.googleMap.addControl(new GLargeMapControl());   			
   		}else if(params['SMALL_MAP_CONTROL']){
   			this.googleMap.addControl(new GSmallMapControl());
   		}else if(params['ZOOM_MAP_CONTROL']){
			this.googleMap.addControl(new GSmallZoomControl());
		}
   		if(params['OVERVIEW_CONTROL']){
   			this.googleMap.addControl(new GOverviewMapControl());
   		}
   		// Layers Control
   		if (params['LAYER_CONTROL']) {
   			this.googleMap.addControl(new LayerControl());
   		}
   		
   		// Tipo por defecto
   		if(params['MAP_TYPE']) {
   			switch (params['MAP_TYPE']) {
   			case 'NORMAL':
   				this.googleMap.setMapType(G_NORMAL_MAP);
   				break;
   			case 'PHYSICAL':
   				this.googleMap.setMapType(G_PHYSICAL_MAP);
   				break;
   			case 'SATELLITE':
   				this.googleMap.setMapType(G_SATELLITE_MAP);
   				break;
   			case 'HYBRID':
   				this.googleMap.setMapType(G_HYBRID_MAP);
   				break;
   			case 'GOOGLE_EARTH':
   				this.googleMap.setMapType(G_SATELLITE_3D_MAP);
   				break;
   			default:
   				this.googleMap.setMapType(G_NORMAL_MAP);
				break;
   			}
   		}   		
   		
   		// Posicionamiento
   		this.centerPoint = new GLatLng(params['MAP_LAT'], params['MAP_LON'])
   		this.initZoom = params['MAP_ZOOM'];
	    this.googleMap.setCenter(this.centerPoint, new Number(this.initZoom).valueOf());	    
	    this.googleMap.enableContinuousZoom();
	    if(params['ENABLE_WHEEL_ZOOM']){
	    	this.googleMap.enableScrollWheelZoom();
	    }
	    
	    // Markers
	    if(params['MAP_MARKERS']){
	    	if(params['MAP_MARKERS'].length > 0){	    		
	    		this.markers = new Array();
	    	}
	    	this.addMarkers(params['MAP_MARKERS']);
	    }
	    
	    if(params['DISABLE_MAP_DRAGGING']){
	    	this.googleMap.disableDragging();
	    }
	    
	    // Directions
   		if(params['DIRECTIONS_ID']){
			this.googleDirections = new GDirections(this.googleMap, document.getElementById(params['DIRECTIONS_ID']));			
			GEvent.addListener(this.googleDirections, "error", handleGeocodeErrors);			
		}
   		
   		// Transiciones
   		if(params['MAP_TRANSITIONS']){
   			this.mapTransitionsTimeout = params['MAP_TRANSITIONS_TIMEOUT'];
   			this.transitions = params['MAP_TRANSITIONS']
   			this.startTransition();
   		}   		
   		
   		// Street View
   		if (params['ENABLE_STREET_VIEW_LAYER'] || params['SHOW_STREET_VIEW_LAYER']) {   			
   			// Street View Marker
   			var guyIcon = new GIcon(G_DEFAULT_ICON);
   			guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
   			guyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
   			guyIcon.imageMap = [
   		        26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
   		        16,20, 16,14, 19,13, 22,8
   		    ];
   			guyIcon.iconSize = new GSize(49, 52);
   			guyIcon.iconAnchor = new GPoint(25, 35);
   			guyIcon.infoWindowAnchor = new GPoint(25, 5);

   			this.streetViewMarker = new GMarker(this.googleMap.getCenter(), {icon: guyIcon, draggable: true});   			
   			GEvent.addListener(this.streetViewMarker, "click", function() {
   															   		self.openPanoramaBubble(self);
   															   });
   			GEvent.addListener(this.streetViewMarker, "dragend", function() {
			   														self.openPanoramaBubble(self);
   																  });
   			GEvent.addListener(this.streetViewMarker, "dragstart", function() {
																	self.closePanoramaBubble(self);
				  												  });
   			// Street View Client
   			this.streetViewClient = new GStreetviewClient();
   			this.streetViewLayer = new GStreetviewOverlay();
   		}
   		if (params['SHOW_STREET_VIEW_LAYER']) {			
			this.showStreetViewLayer();
   		}
   		
   		// Wikipedia
   		if (params['ENABLE_WIKIPEDIA_LAYER'] || params['SHOW_WIKIPEDIA_LAYER']) {
   			this.wikipediaLayer = new GLayer("org.wikipedia.es");   			
   		}
   		if (params['SHOW_WIKIPEDIA_LAYER']) {
   			this.showWikipediaLayer();
   		}
   		
   		// Panoramio
   		if (params['ENABLE_PANORAMIO_LAYER'] || params['SHOW_PANORAMIO_LAYER']) {
   			this.panoramioLayer = new GLayer("com.panoramio.all");   			
   		}
   		if (params['SHOW_PANORAMIO_LAYER']) {
   			this.googleMap.addOverlay(this.panoramioLayer);
   		}
   		
   		// Youtube
   		if (params['ENABLE_YOUTUBE_LAYER'] || params['SHOW_YOUTUBE_LAYER']) {
   			this.youtubeLayer = new GLayer("com.youtube.all");   			
   		}
   		if (params['SHOW_YOUTUBE_LAYER']) {
   			this.googleMap.addOverlay(this.youtubeLayer);
   		}  
    }
}

GoogleMap.prototype.showStreetViewLayer = function () {
	this.googleMap.addOverlay(this.streetViewLayer);
	this.streetViewMarker.setPoint(this.googleMap.getCenter());
	this.googleMap.addOverlay(this.streetViewMarker);
	this.openPanoramaBubble(this);
}

GoogleMap.prototype.hideStreetViewLayer = function () {
	this.closePanoramaBubble(this)
	this.googleMap.removeOverlay(this.streetViewLayer);	
	this.googleMap.removeOverlay(this.streetViewMarker);	
}

GoogleMap.prototype.showWikipediaLayer = function () {
	this.googleMap.addOverlay(this.wikipediaLayer);
}

GoogleMap.prototype.hideWikipediaLayer = function () {
	this.googleMap.removeOverlay(this.wikipediaLayer);
}

GoogleMap.prototype.showYoutubeLayer = function () {
	this.googleMap.addOverlay(this.youtubeLayer);
}

GoogleMap.prototype.hideYoutubeLayer = function () {
	this.googleMap.removeOverlay(this.youtubeLayer);
}

GoogleMap.prototype.showPanoramioLayer = function () {
	this.googleMap.addOverlay(this.panoramioLayer);
}

GoogleMap.prototype.hidePanoramioLayer = function () {
	this.googleMap.removeOverlay(this.panoramioLayer);
}

/**
 * Pasa a la siguiente transición
 */
GoogleMap.prototype.cicleTransition = function (map, transition) {
	var point = new GLatLng(map.transitions[transition]['LAT'], map.transitions[transition]['LON']);
	map.setMapLocation(point, map.transitions[transition]['ZOOM'].valueOf());
	
	map.nextTransition = (map.nextTransition+1)%map.transitions.length;
	var timeout = 0;
	if(map.transitions[transition]['TIMEOUT']){
		timeout = map.transitions[transition]['TIMEOUT']; 
	} else {
		timeout = map.mapTransitionsTimeout;
	}
	map.transitionTimeout = setTimeout(function(){
			map.cicleTransition(map, map.nextTransition)
		},
		timeout);
}

/**
 * Para las transiciones
 */
GoogleMap.prototype.stopTransition = function (){
	clearTimeout(this.transitionTimeout);
	this.transitionPlaying = false;
}

/**
 * Inicia las transiciones
 */
GoogleMap.prototype.startTransition = function (){
	this.transitionPlaying = true;
	this.nextTransition = 0;
	var myself = this;
	this.transitionTimeout = setTimeout(function(){
											myself.cicleTransition(myself, myself.nextTransition)
										},
										this.mapTransitionsTimeout);
}

/**
 * Añade múltiples markers (array) al mapa
 */
GoogleMap.prototype.addMarkers = function (markers){
	var index;
	for(index=0; index<markers.length; index++){	
		this.addMarker(markers[index]);    		
    }
}

/**
 * Añade un marker al mapa
 */
GoogleMap.prototype.addMarker = function (marker){
	var markerPoint = new GLatLng(marker['MARKER_LAT'], marker['MARKER_LON']);
	var markerOptions = new Array();

	// Icono
	if(marker['MARKER_ICON']){
		var baseIcon = new GIcon();			
		baseIcon.iconSize = new GSize(marker['MARKER_ICON_WIDTH'], marker['MARKER_ICON_HEIGHT']);			
		baseIcon.iconAnchor = new GPoint(marker['MARKER_ICON_WIDTH']/2, marker['MARKER_ICON_HEIGHT']/2);
		baseIcon.infoWindowAnchor = new GPoint(marker['MARKER_ICON_WIDTH']/2, 0);
		baseIcon.infoShadowAnchor = new GPoint(marker['MARKER_ICON_WIDTH']/2, 0);
	
		var markerIcon = new GIcon(baseIcon);
		markerIcon.image = marker['MARKER_ICON'];
			
		markerOptions['icon'] = markerIcon;				
	}
	
	// Draggable
	if(marker['MARKER_DRAGGABLE']){
		markerOptions['draggable']  = true;
	}
	
	var googleMarker = new GMarker(markerPoint, markerOptions);
		    	
	// Marker actualizable con el mapa
	if(marker['MARKER_UPDATE_WITH_MAP']){
		if(!this.updatableMarkersWithMap){
			this.updatableMarkersWithMap = new Array();
		}
		this.updatableMarkersWithMap[this.updatableMarkersWithMap.length] = googleMarker;
	}
	
	// Draggable
	if(marker['MARKER_DRAGGABLE']){
		GEvent.addListener(googleMarker, "dragstart", function() {	    																													    												
								this.closeInfoWindow();
						  });				
	}	    	
	
	// Draggable Function
	if(marker['MARKER_DRAGGABLE_FUNCTION']){
		googleMarker.dragEndFunction = marker['MARKER_DRAGGABLE_FUNCTION'];
		GEvent.addListener(googleMarker, "dragend", function() {	    																													    												
								this.dragEndFunction(this.getPoint().lat(), this.getPoint().lng());
						  });
	}
	
	// Texto al hacer click
	if(marker['MARKER_CLICK_TEXT']){    		
		googleMarker.textToShow = marker['MARKER_CLICK_TEXT'] 	    			    		 
		GEvent.addListener(googleMarker, "click", function() {	    																													    												
								this.openInfoWindowHtml(this.textToShow);
						  });
	}	    	
	
	if(!this.markers){
		this.markers = new Array();
	}
	this.markers[this.markers.length] = googleMarker;
	this.googleMap.addOverlay(googleMarker);	
	
	// Texto inicial
	if(marker['MARKER_INIT_TEXT']){
		googleMarker.initText = marker['MARKER_INIT_TEXT'];
		googleMarker.openInfoWindowHtml(googleMarker.initText);
	}
}

/**
 * Establece las direcciones de como llegar de un sitio a otro
 */
GoogleMap.prototype.setFromAddressToGeo = function (fromAddress, latitude, longitude, locale) {	
	this.googleDirections.load("from: "+fromAddress+" to: "+latitude+", "+longitude, { "locale": locale });
}
 
/**
 * Establece las direcciones de como llegar de un sitio a otro
 */
GoogleMap.prototype.setFromGeoToAddress = function (latitude, longitude, toAddress, locale) {	
	this.googleDirections.load("from: "+latitude+", "+longitude+" to: "+toAddress, { "locale": locale });
}

/**
 * Reestablece la posición del mapa
 */
GoogleMap.prototype.resetCenter = function (){
	this.setMapLocation(this.centerPoint, this.initZoom);
}
 
/**
 * Notifies a change in the map container
 */
GoogleMap.prototype.notifyResize = function (){
	this.googleMap.checkResize();
}

/**
 * Establece el centro del mapa en una nueva posición/zoom
 */
GoogleMap.prototype.setMapLocation = function (point, zoom){
	if(!zoom){
		zoom = this.googleMap.getZoom(); 
	}
	if(!point){
		point = this.googleMap.getCenter();
	}
	
	this.googleMap.setZoom(zoom)	
	this.googleMap.panTo(point);	
	
	if(this.updatableMarkersWithMap){
		for(var indice=0 ; indice < this.updatableMarkersWithMap.length; indice++){
			this.updatableMarkersWithMap[indice].setLatLng(point);
			if(this.updatableMarkersWithMap[indice].dragEndFunction){
				this.updatableMarkersWithMap[indice].dragEndFunction(this.updatableMarkersWithMap[indice].getPoint().lat(), this.updatableMarkersWithMap[indice].getPoint().lng());
			}
			if(this.updatableMarkersWithMap[indice].initText){
		    	this.updatableMarkersWithMap[indice].openInfoWindowHtml(this.updatableMarkersWithMap[indice].initText);
		    }
		}
	}
}

/**
 * Busca una calle y añade un marcador contenidendo la calle
 */
GoogleMap.prototype.searchStreet = function (address, addMarker, zoom){	
	if(!zoom){
		zoom = this.googleMap.getZoom(); 
	}
	var mapa = this;
	this.googleGeocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " no encontrado");
      } else {      	
        mapa.setMapLocation(point, zoom);
        if(addMarker){
        	var marker = {MARKER_LAT: point.lat(), MARKER_LON: point.lng(), MARKER_CLICK_TEXT: address};
        	mapa.addMarker(marker);
		}
      }
    }
  );
}
 
GoogleMap.prototype.openPanoramaBubble = function (map) {	
	  map.streetViewClient.getNearestPanorama(map.streetViewMarker.getLatLng(), function (panoramaData) {
		  																			map.renderPanorama(map, panoramaData);
	  																			 });
}

GoogleMap.prototype.closePanoramaBubble = function (map) {
	map.streetViewMarker.closeInfoWindow();
}
 
GoogleMap.prototype.renderPanorama = function (map, panoramaData) {
	if (panoramaData.code == GStreetviewClient.ReturnValues.SUCCESS) {		
		var smallNode = document.createElement('div');
		smallNode.style.width = map.params['STREET_VIEW_WIDTH'];
		smallNode.style.height = map.params['STREET_VIEW_HEIGHT'];	  
		smallNode.id = 'pano';
		
		map.streetViewMarker.openInfoWindow(smallNode);
	
		map.StreetViewPanorama = new GStreetviewPanorama(smallNode);
		map.StreetViewPanorama.setLocationAndPOV(panoramaData.location.latlng, null);
		GEvent.addListener(map.StreetViewPanorama, "yawchanged", function (yaw) {
																	map.panoramaYawChanged(map, yaw);
			 													  });
		GEvent.addListener(map.StreetViewPanorama, "initialized", function (streetViewLocation) {
																		map.panoramaLocationChanged(map, streetViewLocation);
			  													  });
		var infoWindow = map.googleMap.getInfoWindow();	  
		GEvent.addListener(map.streetViewMarker, "infowindowbeforeclose", function() {
			map.StreetViewPanorama.remove();
		});
	}
}

GoogleMap.prototype.panoramaYawChanged = function(map, newYaw) {	
	var GUY_NUM_ICONS = 16;
	var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
	if (newYaw < 0) {
		newYaw += 360;
	}
	guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
	guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
	map.streetViewMarker.setImage(guyImageUrl);
}

GoogleMap.prototype.panoramaLocationChanged = function(map, streetViewLocation) {	
	map.streetViewMarker.setPoint(streetViewLocation.latlng);
}

/**
 * Manejador de errores de Geocodificación
 */
function handleGeocodeErrors(googleDirections) {	
	if (googleDirections.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No se encontró la dirección proporcionada en el mapa.");
	else if (googleDirections.getStatus().code == G_GEO_SERVER_ERROR)
	   alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);	   
	else if (googleDirections.getStatus().code == G_GEO_MISSING_QUERY)
	  alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);	     
	else if (googleDirections.getStatus().code == G_GEO_BAD_KEY)
	  alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	else if (googleDirections.getStatus().code == G_GEO_BAD_REQUEST)
	  alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);	    
	else alert("An unknown error occurred.");
}

/** 
 * Control para el overlay de Street View
 */
function LayerControl() {
}

// LayerControl
LayerControl.prototype = new GControl();

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

	var showOverlay = document.createElement("div");
	this.setButtonStyle_(showOverlay);
	container.appendChild(showOverlay);
	
	// Street View
	if (map.self.params['ENABLE_STREET_VIEW_LAYER']) {
		var streetViewControl = document.createElement("div");
		var streetViewCheckBox = document.createElement("input");		
		streetViewCheckBox.type = 'checkbox';
		if (map.self.params['SHOW_STREET_VIEW_LAYER']) {
			streetViewCheckBox.checked = true;
		}		
		streetViewControl.appendChild(streetViewCheckBox);
		streetViewControl.appendChild(document.createTextNode("Street View"));
		showOverlay.appendChild(streetViewControl);
		GEvent.addDomListener(streetViewCheckBox, "change", function() {
																if (streetViewCheckBox.checked) {																				
																	map.self.showStreetViewLayer();			
																} else {
																	map.self.hideStreetViewLayer();
																}
								});
	}
	// Wikipedia
	if (map.self.params['ENABLE_WIKIPEDIA_LAYER']) {
		var wikipediaControl = document.createElement("div");
		var wikipediaCheckBox = document.createElement("input");		
		wikipediaCheckBox.type = 'checkbox';
		if (map.self.params['SHOW_WIKIPEDIA_LAYER']) {
			wikipediaCheckBox.checked = true;
		}		
		wikipediaControl.appendChild(wikipediaCheckBox);
		wikipediaControl.appendChild(document.createTextNode("Wikipedia"));
		showOverlay.appendChild(wikipediaControl);
		GEvent.addDomListener(wikipediaCheckBox, "change", function() {
																if (wikipediaCheckBox.checked) {																				
																	map.self.showWikipediaLayer();			
																} else {
																	map.self.hideWikipediaLayer();
																}
								});
	}
	// Youtube
	if (map.self.params['ENABLE_YOUTUBE_LAYER']) {
		var youtubeControl = document.createElement("div");
		var youtubeCheckBox = document.createElement("input");		
		youtubeCheckBox.type = 'checkbox';
		if (map.self.params['SHOW_YOUTUBE_LAYER']) {
			youtubeCheckBox.checked = true;
		}		
		youtubeControl.appendChild(youtubeCheckBox);
		youtubeControl.appendChild(document.createTextNode("Youtube"));
		showOverlay.appendChild(youtubeControl);
		GEvent.addDomListener(youtubeCheckBox, "change", function() {
																if (youtubeCheckBox.checked) {																				
																	map.self.showYoutubeLayer();			
																} else {
																	map.self.hideYoutubeLayer();
																}
								});
	}
	// Panoramio
	if (map.self.params['ENABLE_PANORAMIO_LAYER']) {
		var panoramioControl = document.createElement("div");
		var panoramioCheckBox = document.createElement("input");		
		panoramioCheckBox.type = 'checkbox';
		if (map.self.params['SHOW_PANORAMIO_LAYER']) {
			panoramioCheckBox.checked = true;
		}		
		panoramioControl.appendChild(panoramioCheckBox);
		panoramioControl.appendChild(document.createTextNode("Panoramio"));
		showOverlay.appendChild(panoramioControl);
		GEvent.addDomListener(panoramioCheckBox, "change", function() {
																if (panoramioCheckBox.checked) {																				
																	map.self.showPanoramioLayer();			
																} else {
																	map.self.hidePanoramioLayer();
																}
								});
	}

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

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

LayerControl.prototype.setButtonStyle_ = function(button) {	
	button.style.backgroundColor = "white";
	button.style.font = "small Arial";
	button.style.border = "1px solid black";
	button.style.padding = "0px";
	button.style.marginBottom = "0px";
	button.style.textAlign = "left";
	button.style.width = "100px";
}

