/*! 
 * emaps.js
 */
var mapAPILoaded = false;
var eMap = function(options){
	/*
	 * PRIVATE 	
	 */ 
	var map = null;
	var content = '';
	var defaultsMap = {
		info:'',
		id:"map",
		lat:40.00,
		lng:-4.0,
		zoom:5,
		varname:"emapa",
		/*mapType:G_NORMAL_MAP,*/
		searcheable:false,
		latSearchFieldId:"latitudDondeCrear",
		longSearchFieldId:"longitudDondeCrear",
		zoomSearchFieldId:"zoomDondeCrear",
		api_key:"ABDCEF",
		checkResizeAfterLoad:false,
		mapDraggable:false,
		mapMoveEndFunction:null, 
		markHTMLType:1,
		afterLoadFuncion:null
	};
	
	var optionsEMap = $.extend(defaultsMap,options);
	
	var eventIco = null;
	var dragMarker = null;
	
	/* carga del mapa   */
	function load() {
		if (GBrowserIsCompatible()) {
			eventIco = new GIcon(G_DEFAULT_ICON);
			 var arrowIco = new GIcon(G_DEFAULT_ICON);
			arrowIco.image = "http://www.google.com/mapfiles/arrow.png";
	
			if(optionsEMap.info!='') content = eval('('+optionsEMap.info+')');
	    	map = new GMap2(document.getElementById(optionsEMap.id));
	    	map.setMapType(optionsEMap.mapType); 
	    	//map.setMapType(G_NORMAL_MAP);
	    	//map.addControl(new GSmallMapControl());
			//map.addControl(new GHierarchicalMapTypeControl());
			
	    	
	    	/* si hay contenido	*/
	    	if(content!='' && content.eventos.length>0) {
	    		// set caption
				var newCaption = "";
				if(content.que!="" && content.donde!="") newCaption = getText("emap.caption.events.both",{param1:content.que,param2:content.donde},"");
				else if(content.que!="") newCaption = getText("emap.caption.events.que",{param1:content.que},"");
				else if(content.donde!="") newCaption = getText("emap.caption.events.donde",{param1:content.donde},"");
				else  newCaption = getText("emap.caption.eventsAroudYou","","");
				var newRestoCaption = content.eventos.length+" "+getText("emap.caption.events","","");
				changeCaption(newCaption,newRestoCaption);
				
				// set center
	    		map.setCenter(new GLatLng(content.latitud,content.longitud), content.zoom);
	    		 
	    		// crea marcas 
	    		createMarkers(optionsEMap.markHTMLType); 
	    	} 
	    	
	    	/* si no hay contenido, mostrar un arrow */
	    	else {
	    		var point = new GLatLng(optionsEMap.lat,optionsEMap.lng);
	    		map.setCenter(point, optionsEMap.zoom);
	    		if(defaultsMap.searcheable){ createDragMarker(point); }
	    	}
	    	
	    	if(optionsEMap.mapDraggable && optionsEMap.mapMoveEndFunction!=null){
		    	GEvent.addListener(map, "moveend", optionsEMap.mapMoveEndFunction);
	    	}
	    	
	    	GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
	          	$("#zoomDondeCrear").val(newLevel);
	         });


			if(optionsEMap.checkResizeAfterLoad){
				map.checkResize();
			}
			
			
			// call afterload function
			optionsEMap.afterLoadFuncion();
	  	}
	}

	function fillFields(p){
		$("#"+optionsEMap.latSearchFieldId).val(p.y);
		$("#"+optionsEMap.longSearchFieldId).val(p.x);
		$("#"+optionsEMap.zoomSearchFieldId).val(map.getZoom());
	}
	
	function apiLoader(targetId) {
	   if(mapAPILoaded) {
		  //console.log("api ya cargada para "+optionsEMap.id);
		  load();
	   } 
	   else {
		   //console.log("api no cargada, procedemos a cargarla para:"+optionsEMap.id);
		   var script = document.createElement("script");
		   script.src = "http://maps.google.com/maps?file=api&v=2&async=2&key="+optionsEMap.api_key+"&callback="+optionsEMap.varname+".load";
		   script.type = "text/javascript";
		   if(targetId==undefined || targetId==null ){
		   	  document.body.appendChild(script);
		  	  //console.log("api cargada en:"+optionsEMap.id);
		  	  mapAPILoaded = true;
		  }
		  else{
		  	$("#"+targetId).append($(script)); 
		  }
	  }
	}
	
	
	
	
	
	/**
	 * devolvemos este objeto a la creaci�n 
	 */
	return {
		constructor: function(){
			apiLoader();
		},
		setCenter: function(lat,lng,zoom){
	    	window.setTimeout(function() { 
	    		var point = new GLatLng(lat, lng);
	    		map.setCenter(point,parseInt(zoom));
	    	}, 100);
		},
		getMap:function(){
			return map;
		},
		addOverlay:function(marker){
			map.addOverlay(marker);
		},
		changeCaption: function(name,rest){
			changeCaption(name,rest);
		},
		checkResize: function(){
			map.checkResize();
		},
		load:function(){
			load();
		}
	};
};