var C_mapViewer=new Class ({
	idMap: null,

	init: function(_id) {
		this.idMap=_id;
		this.renderMap();
	},
	renderMap: function(){
		ajaxPetition("resources/PHP/API_map.php","command=getMaps",function(ajax){
			var r=eval("("+ajax.responseText+")");
			if (parseInt(r.errorCode)!=0) {alert(r.errorDescription); return;}
			dataMap=r.data[this.idMap];
			this.map=map = new GMap2($_("divMap"), {});
			var center = new GLatLng(parseFloat(dataMap['position'].lat), parseFloat(dataMap['position'].lng));
			map.setCenter(center, parseInt(dataMap['zoom']));
			
			switch(dataMap.GMapType){
				case 'Mapa': map.setMapType(G_NORMAL_MAP);
										break;
				case 'Satélite': map.setMapType(G_SATELLITE_MAP);
										break;
				case 'Híbrido': map.setMapType(G_HYBRID_MAP);
										break;	
				case 'Físico':  map.setMapType(G_PHYSICAL_MAP);
										break;
			}

			if (dataMap['optionType']=="true"){ 
				map.addControl(new GMenuMapTypeControl());
				map.addMapType(G_PHYSICAL_MAP); /*map.addMapType(G_SKY_VISIBLE_MAP); map.addMapType(G_SATELLITE_3D_MAP); map.addMapType(G_MOON_MAP_TYPES); map.addMapType(G_MARS_MAP_TYPES);*/ 
			}
			if (dataMap['optionDrag']=="false"){
				map.disableDragging();
			}
			else map.enableScrollWheelZoom();
				
		/*	if (dataMap['optionLeyend']=="true"){
				var hLeyend = $C("DIV", {".position":"absolute",".right":"10px", ".bottom":"10px", ".background":"URL(resources/images/white50.png)", ".color":"#333333", ".border":"1px solid white", ".fontWeight":"bold", ".fontFamily":"verdana", ".fontSize":"9px"}, $_('map_'+this.idMap));
				$C("DIV", {innerHTML:"<b>Leyenda</b>", ".textAlign":"right", ".color":"black", ".fontSize":"10px"},hLeyend);
				for(var elem in dataMap['leyend']){
					var divLeyend = $C("DIV", {}, hLeyend);
					if (elem=="red") $C("IMG",{src:"resources/images/maps/redMarker.png", title:dataMap['leyend'][elem], align:"absmiddle", ".width":"9px"},divLeyend);
					else $C("IMG",{src:"resources/images/maps/"+elem, title:dataMap['leyend'][elem], align:"absmiddle",".width":"9px"},divLeyend);
							
					$C('SPAN', {innerHTML:dataMap['leyend'][elem]}, divLeyend);
				}
			}*/
			
			$A(dataMap.markers).each(function(elem){
				var marker=null;
				if (elem.color=="red")marker = new GMarker(new GLatLng(parseFloat(elem.lat), parseFloat(elem.lng)), {title:elem.title});
				else{
						var iconMarker = new GIcon(G_DEFAULT_ICON); iconMarker.image = "resources/images/maps/"+elem.color; iconMarker.iconSize = new GSize(23,36);
						var marker = new GMarker(new GLatLng(parseFloat(elem.lat), parseFloat(elem.lng)), {icon:iconMarker, title:elem.title});					
				}
				
				map.addOverlay(marker);		  	
		  	GEvent.addListener(marker, "click", function(){map.openInfoWindow(marker.getLatLng(), elem.text);}.bind(this));
			}.bind(this));
		}.bind(this));
	},
	
	showRoute:function(){
		if (this.gdir) this.gdir.clear();
		else this.gdir = new GDirections(this.map, $_('routeInfo').empty()); 
		this.gdir.load("from: " + $_('inputFrom').value + " to: Calle 2, 10001 Cáceres, Extremadura, España",{ "locale": "es", "travelMode" : G_TRAVEL_MODE_DRIVING });
	}
});

// Ejemplo de uso...
// var mapViewer = new C_mapViewer(id);