/**
 * @author Insignia Studio
 */

jQuery.isMaps = function(target,opciones_user){
	return this.init(target,opciones_user)
}


jQuery.isMaps.prototype.init = function(target,opciones_user){
	// Funcion auxiliar
	var aux = this;
	
	// Opciones por defecto
	aux.opciones_default = {
		autor	: 'InsigniaStudio',
		ver		: '0.1',
		
		direccion	:'barcelona muntaner 244',
		zoom		:13,
		tipoMapa	:G_NORMAL_MAP,
		tipoControl	:false,
		extra:false
		
	}
	
	aux.opciones = jQuery.extend(aux.opciones_default,opciones_user);
	
	
	aux.map = new GMap2(document.getElementById(target));
	
	// Añadimos los controles
	if(typeof(aux.opciones.tipoControl) == 'function'){
		aux.map.addControl(new aux.opciones.tipoControl());
	}
	

	// Definimos el tipo de mapa que queremos ver
	aux.map.setMapType(aux.opciones.tipoMapa);
	
	// Loacalizacion por direccion
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
	aux.opciones.direccion,
	function(point) {
	  if (!point) {
	    alert(address + " no encontrado");
	  } else {
	    aux.map.setCenter(point, aux.opciones.zoom); // Centramos el punto en el mapa y le damos el zoom
	  }
	}
	);
	
	if(typeof(aux.opciones.extra) == 'function'){
		aux.opciones.extra();
	}
		
}

jQuery.isMaps.prototype.addMarker = function(direccion,urlIcono,desc){
		aux = this;
		//Finestra Amb la info
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(
		direccion,
		function(point) {
		  if (!point) {
		    alert(address + " no encontrado");
		  } else {
		  	// Actuamos dependiendo si hay icono o no
			if(urlIcono){
				var icono = new GIcon();
        		icono.image = urlIcono;
				icono.iconAnchor = new GPoint(0, 27);
				var marker = new GMarker(point,icono);
				aux.map.addOverlay(marker);
				if (desc) {
					icono.infoWindowAnchor = new GPoint(54, 27);
					var infoTabs = [
		  			new GInfoWindowTab("IS", desc )
					];
			
					GEvent.addListener(marker, "click", function(){
					marker.openInfoWindowTabsHtml(infoTabs);
					});
					marker.openInfoWindowTabsHtml(infoTabs);
				}
				
			}else{
				aux.map.addOverlay(new GMarker(point));	
			}
		  }
		});
	
	
	return aux;
}
