var myBulk=0;
(function($){
	$.fn.loadImage = function(options){
		var defaults = {
			src: "none",
			bulk: 1,
			onLoadFinish: null
		};
		var options = $.extend(defaults, options);
		return this.each(function(){
			var me = this;	
			var img = new Image();
			$(img).attr("src",options.src).load(function(){
				myBulk++;
				if (myBulk == options.bulk) {
					if (options.onLoadFinish) options.onLoadFinish(me);
					myBulk = 0;
				}
			
			}).appendTo(this);
		});
	};
})(jQuery);

jQuery(function(){
	
	$("#wrapper").addClass("jsactive");
	
	// Gallery
	$('#content .images .medium a').not(".description a").fancybox({
		imageScale:true,
		overlayOpacity: 0.6,
		padding: 8
	});
	$('#content .entry a.map').fancybox({
		overlayOpacity: 0.6,
		padding: 8,
		frameWidth: 950,
		frameHeight: 600
	});  
	
	$("#content .images .thumbnail a").click(function() {
		if (!$(this).hasClass("active")) {
			var thisHref = $(this).attr("href");
			var thisRel = $(this).attr("rel");
			var imagewrapper = $("#content .images .medium a");
			imagewrapper.height(imagewrapper.height()).attr("href",thisRel);
			
			var newText = $(this).siblings("div.description").html();
			$("#content .images .medium div.description").html(newText);
			$("#content .images .medium img").animate({opacity:0 }, 500, function() {
				var actualImg = $(this);
				var img = new Image();
				$(img).attr("src",thisHref).load(function(){
					
					$(this).css("opacity",0).appendTo("#content .images .medium a");
					actualImg.remove();
					var newImageHeight = $("#content .images .medium img").height();
					imagewrapper.animate({ height:newImageHeight },200);
					$(img).animate({ opacity:1 },500);
				});
			});
			
			
			$("#content .images .thumbnail a").removeClass("active");
			$(this).addClass("active");
		}
		return false;
		
	});

	// open booking in iframe with fancybox
	// onclick="javascript: window.open('."'https://secure.obehotel.com:8443/Buscador/reservar/?b=ext&h=543&referer=mulhacen&pagina=www.obehotel.com', '', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=550'".'); return false"

	
	$(".reserva-online a").fancybox({
		'width'				: 950,
		'height'			: '80%',
		'autoScale'			: false,
		'type'				: 'iframe'
	});
		
	
	// RESIZE Intro---------------------------------------------------------------------------------
	function initWindowSize() {
			var windowHeight = $(window).height();
			if (windowHeight < 800) {
				$("#pre-wrapper").height(20);
			} else {
				$("#pre-wrapper").height(100);
			}
	}
	$(window).resize(function(){ initWindowSize(); });
	initWindowSize();
	
	

	
});