
var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var ii=0;


function tohere(im) {
	gmarkers[im].openInfoWindowHtml(to_htmls[im]);
}
function fromhere(im) {
	gmarkers[im].openInfoWindowHtml(from_htmls[im]);
}


function initHotelGmap(){

	$(window).unload(GUnload);

	if (GBrowserIsCompatible()) {

		function createMarker(point, address, nombre, marca, isMain, url, tieneRuta) {

			var icon = new GIcon();
			icon.image = "/design/fiesta/images/ico/gmap_" + marca + ".png";
			icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.iconSize = new GSize(33, 55);
			icon.shadowSize = new GSize(34, 49);
			icon.iconAnchor = new GPoint(15, 51);
			icon.infoWindowAnchor = new GPoint(35, 0);

			var marker = new GMarker(point, icon);
			var im = ii;
			
			function translate(obj) {
				var url = window.location.href;
				var nohttp = url.split('//')[1];
				var hostPort = nohttp.split('/')[2];
				var text = '';
				
				if(obj=='rute') {
					if(hostPort=='es') text = 'Ruta';
					if(hostPort=='en') text = 'Rute';
					if(hostPort=='de') text = 'Strecke';
				}
				
				if(obj=='toHere') {
					if(hostPort=='es') text = 'Hasta aqu&iacute;';
					if(hostPort=='en') text = 'To here';
					if(hostPort=='de') text = 'Anreise';
				}
				
				if(obj=='fromHere') {
					if(hostPort=='es') text = 'Desde aqu&iacute;';
					if(hostPort=='en') text = 'From here';
					if(hostPort=='de') text = 'Abreise';
				}
				
				if(obj=='to') {
					if(hostPort=='es') text = 'Hasta';
					if(hostPort=='en') text = 'To';
					if(hostPort=='de') text = 'Bis';
				}
				
				if(obj=='from') {
					if(hostPort=='es') text = 'Desde';
					if(hostPort=='en') text = 'From';
					if(hostPort=='de') text = 'Von';
				}
				
				if(obj=='go') {
					if(hostPort=='es') text = 'IR';
					if(hostPort=='en') text = 'GO';
					if(hostPort=='de') text = 'LOS';
				}
				
				return text;
			}

			nombre = '<b class="txto_'+ marca +'">'+ nombre +'</b>';
			if(!isMain) nombre +='<br><br><a href="'+ url +'">Ver hotel</a>';

			if(tieneRuta){
				// The info window version with the "to here" form open
				to_htmls[im] = nombre + '<br><br>'+translate('rute')+': <b>'+translate('toHere')+'</b> - <a href="javascript:fromhere(' + im + ')">'+translate('fromHere')+'</a>' +
				'<form class="directions" action="http://maps.google.es/maps" method="get" target="_blank">' +
				'<br><br>'+translate('from')+': <input type="text" SIZE=20 MAXLENGTH=60 name="saddr" id="saddr" value="" />' +
				'<INPUT value="'+translate('go')+'" TYPE="SUBMIT"><input type="hidden" name="daddr" value="' + address + '" /><form>';

				// The info window version with the "to here" form open
				from_htmls[im] = nombre + '<br><br>'+translate('rute')+': <a href="javascript:tohere(' + im + ')">'+translate('toHere')+'</a> - <b>'+translate('fromHere')+'</b>' +
				'<form class="directions" action="http://maps.google.es/maps" method="get"" target="_blank">' +
				'<br><br>'+translate('to')+': <input type="text" SIZE=20 MAXLENGTH=60 name="daddr" id="daddr" value="" />' +
				'<INPUT value="'+translate('go')+'" TYPE="SUBMIT"><input type="hidden" name="saddr" value="' + address + '" /><form>';

				// The inactive version of the direction info
				var html = nombre + '<br><br>'+translate('rute')+': <a href="javascript:tohere(' + im + ')">'+translate('toHere')+'</a> - <a href="javascript:fromhere(' + im + ')">'+translate('fromHere')+'</a>';
			
			}else{
				to_htmls[im] = nombre;
				from_htmls[im] = nombre;
				var html = nombre;
			}
			
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			});

			gmarkers[im] = marker;
			htmls[im] = html;
			ii++;
			return marker;
		}


		//HALLAMOS EL ID DEL HOTEL PRINCIPAL
		var main_loc_id = 0;
		for(var i=0; i<gLocations.length; i++){
			if(gLocations[i]['isMain'] == true){
				main_loc_id = i;
			}
		}


		// Display the map, with some controls and set the initial location
		map = new GMap2(document.getElementById("googlemap"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.addControl(new GOverviewMapControl());


		//centramos el mapa en el marcador del hotel que estamos visualizando
		var centerPoint = new GLatLng(parseFloat(gLocations[main_loc_id]['latitud_centro']), parseFloat(gLocations[main_loc_id]['longitud_centro']));
		map.setCenter(centerPoint, parseInt(gLocations[main_loc_id]['zoom']));


		//pintamos los marcadores de los hoteles de la zona
		for(var i=0; i<gLocations.length; i++){
			var loc = gLocations[i];
			var myPoint = new GLatLng(loc['latitud'], loc['longitud']);
			var myMarker = createMarker(myPoint, loc['address'], loc['name'], loc['marca'].toLowerCase(), loc['isMain'], loc['url'], loc['tieneRutas'] );
			map.addOverlay(myMarker);

			//abrimos el globo de información del hotel que estamos visualizando
			if(i==main_loc_id) myMarker.openInfoWindowHtml(htmls[main_loc_id]);
		}


		//usamos el tipo de mapa definido en el gestor
		switch(gLocations[main_loc_id]['tipoMapa']){
			case "0":
				map.setMapType(G_NORMAL_MAP);break;
			case "1":
				map.setMapType(G_SATELLITE_MAP);break;
			case "2":
				map.setMapType(G_HYBRID_MAP);break;
		}



	} else {
	  //alert("Sorry, the Google Maps API is not compatible with this browser");
	}


}
