/******************************* NO BORRAR LO COMENTADO *******************************/
function load_map(destino,permalink_padre,nombre_padre) {

	if (document.getElementById) {

		var map = new google.maps.Map(document.getElementById('map'), {
			zoom: 1,
			center: new google.maps.LatLng(18.479609,21.09375),
			mapTypeId: google.maps.MapTypeId.TERRAIN,
			mapTypeControl: true,
			navigationControl: true,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
				// style: google.maps.NavigationControlStyle.ZOOM_PAN,
				position: google.maps.ControlPosition.TOP_RIGHT
				// ,style: google.maps.NavigationControlStyle.ZOOM_PAN
			}//,
			// scaleControl: true,
			// disableDoubleClickZoom: true,
			// draggable: false,
			// scrollwheel: false,
			// streetViewControl: true,
			// draggableCursor: 'move',
			// draggableCursor: 'img/myCursor.png',
			// draggingCursor: 'move',
		});

		/*
			map.setOptions({
				center: new google.maps.LatLng(40.6891, -74.0445),
				zoom: 17,
				mapTypeId: google.maps.MapTypeId.SATELLITE
			});
			map.getZoom();
			map.setZoom(12);
			map.getCenter();
			map.setCenter(new google.maps.LatLng(lat,lng));
			map.getMapTypeId();
			map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
			
		*/

		if (destino == "0") {
			url_markers = "/ajax/ofertas-viajes/get_markers_mapa.htm";
			$("#breadcrumb_destino").html("");
		} else {
			url_markers = "/ajax/ofertas-viajes/get_markers_mapa/" + destino + ".htm";
			$("#breadcrumb_destino").html(nombre_padre);
		}

		var bounds = new google.maps.LatLngBounds();

		// sacamos los markers del XML
		jQuery.get(url_markers, {}, function(data) {
			var places = [];
			i = 0;
			jQuery(data).find("marker").each(function() {
				var marker = jQuery(this);
				var id = marker.attr("id");
				var name = marker.attr("name");
				var permalink  = marker.attr("permalink");
				var lat = parseFloat(marker.attr("lat"));
				var lng = parseFloat(marker.attr("lng"));

				places[i] = new google.maps.LatLng(lat, lng);
				bounds.extend(places[i]);
				
				var marker = new google.maps.Marker({
					position: places[i],
					title: name,
					map: map,
					icon: 'http://www.aavv.com/common/pi/global/images/icon_plus_16.gif'
				});

				google.maps.event.addListener(marker, 'click', function() {
					if (destino == "0") {
						$('ul.listado_categorias_destino').css('right','4567px');
						$('ul.listado_destinos_destino').css('right','4567px');
						load_map(id,permalink,name);
					} else {
						window.location = "/ofertas-viajes/destinos/" + permalink_padre + "/" + permalink + '.htm';
					}
				});
				i = i + 1;

			});

			map.fitBounds(bounds);
			/*
			google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
				var bounds = map.getBounds();
			});
			*/
		});
	}
}

function init() {

	// bloque_buscador_ofertas_por_categoria
	$('a.categoria').click(function() {
		$('ul.listado_categorias_destino').css('display','none');
		$('ul.listado_destinos_destino').css('display','none');
		$('a.categoria_selected').attr('class','categoria');
		$('a.destino_selected').attr('class','destino');
		$(this).attr('class','categoria_selected');
		$(this).next('ul.listado_categorias_destino').css('display','block');
		return false;
	});
	$('a.listado_categorias_destino_close').click(function() {
		$('a.categoria_selected').attr('class','categoria');
		$('ul.listado_categorias_destino').css('display','none');
	});

	// bloque_buscador_ofertas_por_destino
	$('a.destino').click(function() {
		$('ul.listado_categorias_destino').css('display','none');
		$('ul.listado_destinos_destino').css('display','none');
		$('a.destino_selected').attr('class','destino');
		$('a.categoria_selected').attr('class','categoria');
		$(this).attr('class','destino_selected');
		$(this).next('ul.listado_destinos_destino').css('display','block');
		return false;
	});
	$('a.listado_destinos_destino_close').click(function() {
		$('a.destino_selected').attr('class','destino');
		$('ul.listado_destinos_destino').css('display','none');
	});

	load_map("0","","");

}
window.onload = init;

