function gebi(element_id){
	return document.getElementById(element_id);
}

function google_map(id, onclick){
	var c_lat; // Latitud del centro del mapa
	var c_lng; // Longitud del centro del mapa
	var zoom_level; // Nivel de zoom del centro
	this.div = id;
	this.base='';
	this.selected_listing_id=null; // Marker Seleccionado
	this.is_map_ready=null; // Variable que indica si el mapa está listo
	this.listings={}; // Lista que engloba todos los Markers
	this.markers={};
	this.onclick = onclick;
	
	
	this.set_center = function(lat, lng){
		this.c_lat = lat;
		this.c_lng = lng;
		
		if(this.map.isLoaded()){
			this.map.setCenter(new GLatLng(lat, lng), this.zoom_level);	
		}
	}
	
	this.set_zoom = function(zoom){
		this.zoom_level = zoom;	
		
		if(this.map.isLoaded()){
			this.map.setZoom(zoom);	
		}
	}
	
	// Función de Inicialización
	this.init=function(){
		//this.tooltip=gebi('main_map_tooltip');
		this.assign_icons_to_markers();
		this.create_map();
		this.centre_map_on_listings();
		//this.map.savePosition();
		//this.initial_map_type_name=this.map.getCurrentMapType().getName();
		this.draw_markers();
		//this.draw_clusters();
		//this.add_all_map_types();
		//this.switch_to_map_type('london_tube');
		//this.selected_map_type=lmap.map.getCurrentMapType().getName();
		this.add_map_controls();
		this.is_map_ready=1;
		//document.forms.search.elements.q.focus();
	};
	
	// Crear el Mapa
	this.create_map=function(){
		var map=new GMap2(gebi(this.div)); //mapa en tag "map"
		this.map=map;
	};
	
	// Añadir los controles al Mapa
	this.add_map_controls=function(){
		this.map.addControl(new GLargeMapControl()); //Tenia puesto Small en vez de Large
		this.map.addControl(new GMapTypeControl());
	};
	
	// Centrar el Mapa
	this.centre_map_on_listings=function(){
		var c_lat = this.c_lat;
		var c_lng = this.c_lng;
		var zoom_level = this.zoom_level;
		
		this.map.setCenter(new GLatLng(c_lat,c_lng),zoom_level);
	};
	

	// Seleccionar un Marker
	this.select_listing=function(id){
		if(!this.is_map_ready){
			return;
		}
		
		if(this.selected_listing_id){
			this.deselect_listing();
		}
		this.selected_listing_id=id;
	};

	// Deseleccionar un Marker
	this.deselect_listing=function(){
		var id=this.selected_listing_id;
		this.selected_listing_id=null;
	};
	
	// Crear un Marker
	this.create_marker=function(item){
		var marker=new GMarker(new GLatLng(item.lat,item.lng),item.icon);
		//marker.tooltip=item.tooltip;
		marker.id=item.id;
		var me=this;
		if(item.href!=""){
			GEvent.addListener(marker,"mouseover",function(){
				me.select_listing(item.id);
			});
		
			if(this.onclick){
				GEvent.addListener(marker,"click",function(){
					me.go_to_promo(item.href);
				});
			}
		}
		this.markers[item.id]=marker;
		this.map.addOverlay(marker);
	};
	
	// Dibujar todos los Markers definidos
	this.draw_markers=function(){
		for(id in this.listings){
			this.create_marker(this.listings[id]);
		}
	};

	// Añadir un elemento a la lista
	this.add_listing=function(item){
		this.listings[item.id]=item;
	};


	// Asignar iconos a los Markers
	this.assign_icons_to_markers=function(){
		for(id in this.listings){
			l=this.listings[id];
			var icon=this.get_icon(l.icon+'_pin');
			l.icon=icon;
		}
	};

	// Tipos de iconos
	this.icons_data={
		"default_pin":{
			select:'',
			image:G_DEFAULT_ICON.image,
			shadow:G_DEFAULT_ICON.shadow,
			iconSize:G_DEFAULT_ICON.iconSize,
			shadowSize:G_DEFAULT_ICON.shadowSize,
			iconAnchor:G_DEFAULT_ICON.iconAnchor,
			infoWindowAnchor:G_DEFAULT_ICON.infoWindowAnchor,
			transparent:G_DEFAULT_ICON.transparent,
			imageMap:G_DEFAULT_ICON.imageMap			
		},
		"normal_pin":{
			based_on:"default_pin"
		}
	};
	// Crear el icono
	this.get_icon=function(icon_name){
		var icon_data=this.icons_data[icon_name];
		var default_data=this.icons_data[icon_data.based_on]||icon_data;

		var icon=new GIcon();
		for(var field in default_data){
			icon[field]=icon_data[field]||default_data[field];
		}
		return icon;
	};	
}