/*--------------------------- Class --------------------------- */
//
// --------------- Detail del fichero ---------------
// - Nombre del fichero : Mod_GoogleMaps.js
// - Fecha de creacin : 14/12/2006
// - Fecha de modificacin : 18/12/2006 (CF : Fin de fichero )
// - Version : 1.0.0
// --------------------------------------------------
//
// -------------- Detail de los autors --------------
// - Nombre del autor : Lemarinel Sbastien
// - Puesto : Encaragdo del desarollo web
// - Empresa : Barcelona Urban Hotel 
// --------------------------------------------------
//
// ----------- Informacion sobre el modulo -----------
// - Nombre : Mod_GoogleMaps()
// - Compatible : Javascript
// - Descripcin : Esto modulo permite de crear una nueva mapa con el API de Google Maps 
// - Ficheros ligados :
// 		-> Fichero de estylo CSS
//		-> EWindow.js : Fichero para hacer Window Anchor
//
// - Lista de las fonctiones :
// 		-> NewMap(Target,Tipo_Map) : Create una nueva mapa virgen
//		-> function Define_Icon(Path) : Create un icono nuevo
//		-> createMarker(map,point,html,icon) : Create una windowanchor 
//		-> AddPoint(map,Adress,window_info,center,zoom,icon) : Add un nuevo punto con el icon y una windowanchor
// --------------------------------------------------
//
// -------- Informacion sobre las Funciones ---------
//
// - Nombre : NewMap(Target,Tipo_Map,Minimap)
// - Descripcin :Creacion de una nueva mapa
// - Parmetro : 
// 		-> Target : Id del elemeto HTML (DIV) donde la mapa debe se fijar
// 		-> Tipo_Map : Tipo de la mapa :G_MAP_TYPE , G_SATELLITE_TYPE, G_HYBRID_TYPE
// 		-> Minimap : ( Valñor true o false), pone una mini mapa
// - Retorno : Fija la mapa en el elemento HTML 
//____________________________________________________
//
// - Nombre : function Define_Icon(Path)
// - Descripcin :Creacion de una nuevo icono
// - Parmetro : 
// 		-> Path : direccion de la imagen del icono
// - Retorno : el resultado de la creacion del icono

// 		-> Zoom_Level : Nivel de zoom sobre la mapa entre 0 y 15
//		-> Longitud : [Opcional] Longitud del target por la primera mapa (por defecto 41.39201 barcelona)
//		-> Latitud : [Opcional] Latitud del target por la primera mapa (por defecto 2.16534 barcelona)
// - Retorno : nada. Fija la mapa en el elemento HTML la mapa
//____________________________________________________
//
// - Nombre : createMarker(map,point,html,icon) 
// - Descripcin : Create una windowanchor 
// - Parmetro : 
// 		-> map : current mapa
// 		-> point : punto para placar el marker
// 		-> html : codigo html de la pentalla 
// 		-> icon : icono
// - Retorno : el resultado de la creacion del marker
//____________________________________________________
//
// - Nombre :   AddPoint(map,Adress,window_info,center,zoom,icon){
// - Descripcin :Creacion de un nuevo estilo por el icono
// - Parmetro : 
// 		-> map : current mapa
//		-> Adress : Direccion en el formato : "Calle , Cuidad"
//		-> window_info : Contenido del punto anadido
//		-> center : Centra la mapa sobre este punto (valor : Yes o No)
//		-> zoom : nivel de zoom
//		-> icon : icono
// - Retorno : nada. 
//____________________________________________________
// --------------------------------------------------
/*--------------------------- Class --------------------------- */

//function Class_GoogleMaps(){

    var map = null;
    var geocoder = null;
	var Zoom_Level_Old=null;
	var Old_Position = null ;

	/* ------------------------------------------------------------------ */
	function NewMap(Target,Tipo_Map,Add_MiniMap){						
			var Longitud="";
			var Latitud="";
			var Zoom_Level=1;
			
			geocoder = new GClientGeocoder();			

		// Inicializacion de las variables
			//Punto por defecto : Barcelona
			if((Longitud=="")&&(Latitud=="")){
				Longitud=41.39201;
				Latitud=2.16534;
			}

		if (GBrowserIsCompatible()) {
			map = new GMap2(Target);
			map.enableContinuousZoom();
			map.setCenter(new GLatLng(Longitud,Latitud), Zoom_Level);
			map.setMapType(Tipo_Map);
			if(Add_MiniMap==true){
				map.addControl(new GOverviewMapControl());
			}
			map.enableDoubleClickZoom();
			return map;
		}	
	}
	/* ------------------------------------------------------------------ */
	
	
	
	/* ------------------------------------------------------------------ */
	function Define_Icon(Path){
		if (GBrowserIsCompatible()){
			var icon = new GIcon();
			icon.image = Path;
			icon.iconSize = new GSize(20, 40);
			icon.iconAnchor = new GPoint(10, 40);
			icon.infoWindowAnchor = new GPoint(15, 1);
			
			return icon;
		}
	}
	/* ------------------------------------------------------------------ */
	function createMarker(map,point,html,icon) {
		var marker = new GMarker(point,icon);
		// ========== Open the EWindow instead of a Google Info Window ==========
		GEvent.addListener(marker, "click", function() {
			ew.openOnMarker(marker,html);
			map.panTo(point);
			//map.setCenter(point, 12);
		});
		return marker;
	}

	/* ------------------------------------------------------------------ */
	function AddPoint(map,Adress,window_info,center,zoom,icon){
		var geocoder = new GClientGeocoder();

		// Create an EWindow
		ew = new EWindow(map,E_STYLE_7);      
		map.addOverlay(ew);
		
		geocoder.getLatLng(Adress,
			function(point) {
				if (!point) {
					//alert(Adress + " not found");
				} else {
					if(center=="Yes"){
						map.setCenter(point, zoom);
					}
      				var marker = createMarker(map,point,window_info,icon)
      				map.addOverlay(marker);


				}
			}
		);
	GEvent.addListener(map, "click", function(marker,point) {
        if (point) {
          ew.hide();
        }
      });
	}

	/* ------------------------------------------------------------------ */
	function Center_Point(address,Zoom_Level) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					if(map.getZoom()!=Zoom_Level){

						GEvent.addListener(map, "moveend", function() {
								if(map.getZoom()<Zoom_Level){
									//alert(map.getZoom()+"<"+Zoom_Level);
									map.zoomIn(point,false,true);
								}else if(map.getZoom()>Zoom_Level){
									//alert(map.getZoom()+">"+Zoom_Level);
									//alert(map.getZoom()+"<"+Zoom_Level);
									map.zoomOut(Old_Position,true);
								}else if(map.getZoom()==Zoom_Level){
									//alert(map.getZoom()+"="+Zoom_Level);
									GEvent.clearInstanceListeners(map);
									map.panTo(point);
									Old_Position=point;
								}
						});
						if(map.getZoom()<Zoom_Level){
							map.zoomIn(point,false,true);
							Old_Position=point;
						}else if(map.getZoom()>Zoom_Level){
							//alert(map.getZoom()+">"+Zoom_Level);
							map.zoomOut(Old_Position,true);
						}
						
					}else{
						map.panTo(point);
					}

				}
			}
	  	);
	}

	/* ------------------------------------------------------------------ */
      // ========== Close the EWindow if theres a map click ==========

	/* ------------------------------------------------------------------ */
	function AddControl(map){
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GControl());
		
	}
	/* ------------------------------------------------------------------ */
//}

/*-------------------------------------------------- Modificaciones ------------------------------------------------- 
//
// -----------------------------------------------------------------------------------------------------------------
// |         Quien          |   Fecha    |  Version |                                Detail          
// -----------------------------------------------------------------------------------------------------------------
// | Lemarinel Sébastien    | 15/12/2006 |  V 0.1.0 | Preparación del fichero y creaciòn del script
// | Lemarinel Sébastien    | 18/12/2006 |  V 1.0.0 |  Primer version del script que fonctiona
//
//
-------------------------------------------------- Modificaciones ------------------------------------------------- */