var timer;
var scrollPos = 0;
var animando = false;

$(document).ready(function(){
	initFancyFotos();
	
	$("#tabs").tabs({
		show : function(event, ui){
			if(ui.panel.id == "tabMapa") mostrarMapa();
		}
	});
	
    $("a.link_capes").click(function(){
    	$("div.capesFitxa").hide();
    	$("a.link_capes_active").removeClass("link_capes_active").addClass("link_capes");
    	$(this).addClass("link_capes_active");
    	$("#" + $(this).attr("mostrar")).show();
    	
    	if($(this).attr("mostrar") == "capa4"){
    		mostrarMapa();
    	}
    });
    
    $(document).scrollTop(0);
    
    var scrollItem = "#ofertas div.scrollableItems div.scrollableItem"; 
    scrollItems = $(scrollItem).length;
    itemWidth = parseInt($(scrollItem).css("width")) 
    			+ parseInt($(scrollItem).css("margin-left")) 
    			+ parseInt($(scrollItem).css("margin-right"))
    			+ parseInt($(scrollItem).css("padding-left")) 
    			+ parseInt($(scrollItem).css("padding-right"));
    
    $("#ofertas").mouseover(function(){
    	$(document).stopTime();
    }).mouseout(function(){
    	startTime();
    });
    
    $("#ofertas div.scrollInd a").click(function(){
    	idScreenShot = parseInt($(this).attr("idScreenShot"));
    	gotoItem(idScreenShot);
    });
    
    startTime();
    
    $("#btnFotoDreta").click(function(){
		if(!animando){
			animando = true;
			
			var posFinal = $("#fotoSlideCont").position().left - $("#fotoSlide").width();
    		var positionMax = $("#fotoSlide").width() - $("#fotoSlideCont").width();
    		
    		if(posFinal < positionMax){
    			$("#fotoSlideCont table tr td").clone().insertAfter("#fotoSlideCont table tr td:last");
    			//Si hemos llegado a este punto, clonamos el contenido y lo situamos a la izquierda
    			posFinal = $("#fotoSlideCont").position().left - $("#fotoSlide").width();
    			initFancyFotos();
    		}
    		$("#fotoSlideCont").animate({
				left: posFinal
			}, 1000, function(){
				animando = false;
			});
		}
	});
    	
	$("#btnFotoEsquerra").click(function(){
		if(!animando){
			animando = true;
			
			var posFinal = $("#fotoSlideCont").position().left + $("#fotoSlide").width();
    		var positionMax = 0;
    		
    		if(posFinal > positionMax){
    			anchoAnadido = $("#fotoSlideCont").width();
    			//Si hemos llegado a este punto, clonamos el contenido y lo situamos a la derechaç
    			$("#fotoSlideCont table tr td").clone().insertBefore("#fotoSlideCont table tr td:first")
    			nuevaPos = $("#fotoSlideCont").position().left - anchoAnadido;
    			$("#fotoSlideCont").css("left", nuevaPos + "px"); 
    			
    			//Volvemos a asignar el maximo final 
    			posFinal = $("#fotoSlideCont").position().left + $("#fotoSlide").width();
    			initFancyFotos();
    		}
    		
    		$("#fotoSlideCont").animate({
				left: posFinal
			}, 1000, function(){
				animando = false;
			});  
		}
	})
});

initFancyFotos = function(){
	$("a.jsFancyFotos").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});
}

cambiarFotos = function(){
	intercambiar = false;
	posFoto = $("#ImgFotoHome").position().left;
	
	$("#ImgFotoHome").css("left", $("#ImgFotoHome2").position().left + "px");
	$("#ImgFotoHome2").css("left", posFoto + "px");
}

function startTime(){
	$(document).everyTime(7000, function(i) {
		moveScroll();
	});
}

function moveScroll(){
	scrollPos++;
	if(scrollPos == scrollItems){
		scrollPos = 0;
	}
	gotoItem(scrollPos);
}

function gotoItem(position){
	posLeft = position * itemWidth;
	
	$("#ofertas div.scrollableItems").animate({
		left : 0 - posLeft
	}, 750);
	
	$("#ofertas div.scrollInd a").attr("class", "scrollControl");
	$("#ofertas div.scrollInd a#ind" + position).attr("class", "scrollControlSel");
	
	scrollPos = position;
} 

function mostrarMapa(){
	//Si la capa donde creamos el mapa a mano existe...
	if($("#googleMaps").length != 0){
		if (GBrowserIsCompatible()) {
			gMap = new GMap2(document.getElementById("googleMaps"));
			
			var mapControl = new GMapTypeControl();
			gMap.addControl(mapControl);
			gMap.addControl(new GLargeMapControl());
		}

		var point = new GLatLng($("#googleMaps").attr("longitud"),$("#googleMaps").attr("latitud"));
		var options = {};
		var marca = new GMarker(point, options);
		var zoomLevel = 14;
		gMap.addOverlay(marca);	
		gMap.setCenter(point, zoomLevel);
	}
	if($("#googleMapsAjax").length != 0){
		$("#googleMapsAjax").load("/" + idioma + "/mapa?id=" + $("#googleMapsAjax").attr("idHotel"));
	}
}
