/* widgets */
function cargarWidgets() {
	$('[class*=widget:]').each(function() {
		var parts = this.className.split(' ');
		var widget = '';
		for (var i = 0; i < parts.length; i++) {
			var pos = parts[i].indexOf('widget:');
			if (pos == 0) {
				widget = parts[i].substring(pos + 7);
				break;
			}
		}
		cargarWidget(this, widget);
	});
}

function cargarWidget(divWidget, widget) {
		var url = 'http://localhost/HUSA_Princesa/ajax-widget.php';
		var $elDiv = $(divWidget);
		$.get(url, {"widget" : widget}, function(data, textStatus) {
			$elDiv.html(data);
		}, 'html');
}

/* fancybox para galerias */
function extendLinks(scope) {
	var elScope = scope || document,
		$a = $('a', scope);
	// adicion del atributo target
	$a.filter('[rel*=external]').attr('target', '_blank');
	// fancybox 
	$a.filter(function(){
		var $t=$(this);
		return ($t.hasClass('fancylink') && !$t.data('fancyAsignado'));
	}).data('fancyAsignado', true).fancybox({
		titlePosition:'inside',
		onComplete: function() {
			var $title = $('#fancybox-title');
			if (/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test($title.text())) {
				$title.html(['<a class="FertigoProRegular" href="', $title.text(), '">', txtDescargarAlta , '</a>'].join(''));
			}
		}
	});
	
	// anyadir a favoritos
	$a.filter('.addFavorites').click( function() {
		title = document.title;
		if (window.sidebar) { // Firefox
			window.sidebar.addPanel(title, window.location.href,"");
		} else if( window.external ) { // IE
			window.external.AddFavorite( window.location.href, title);
		} else {
			alert(txtBrowserNoSoportado);
		}
		return false;
	});
	
	// anyadir a GPS
    $a.filter('.addToGPS, .rSociales').bind('click', function() {
	    if (!this.asignadoFancyBox) {
	        this.asignadoFancybox = true;
	        $(this).fancybox({
	            hideOnContentClick:false,
	            frameWidth: 300,
	            frameHeight: 230,
	            padding: 15,
	            overlayShow: false
	        });
	        $(this).click();
	    }
	    return false;
	});
}

function initSlideshow(scope) {
	var $div = $('div.slideshow', scope || document);
	$div.find('img').not(':first-child').hide();
	function nextImg() {
		$div.each(function() {
			var $img = $div.find('img'),
				$aOcultar = $img.filter(':visible'),
				$aMostrar = $aOcultar.next();
			if (!$aMostrar.is('img')) {
				$aMostrar = $img.eq(0);
			}
			$aMostrar.css({
				display:'',
				position:'absolute',
				zIndex:4
			});
			$aOcultar.fadeOut(1000, function() {
				$aMostrar.css({
					position:'',
					zIndex:''
				});
			});
		});
		setTimeout(nextImg, 5000);
	}
	setTimeout(nextImg, 4000);
}

function initToggleThis(scope) {
	$('strong.toggle', scope).click(function() {
		var $t = $(this).toggleClass('active'),
			$p = $t.siblings('.toggleThis').toggleClass('shown'),
			cache = $p.height(),
			target = $p.css('height', $p.is('.shown') ? 'auto' : '').height();
		$p.height(cache);
		$p.animate({
			height: target + 'px'
		}, {
			duration: Math.abs(target-cache),
			step: function() {
				$('div.toggleThis').each(function() {
					// rerrender blocks for stupid MSIE bug
					this.style.display = 'none';
					this.style.display = 'block';
				});
			}
		});
		if ($t.is('.active')) {
			$t.closest('li').siblings().find('strong.toggle.active').click();
		}
	});
}

function initSlider(scope) {
	var $li = $('ul.slide li', scope),
		nZ  = parseInt($li.find('img:first').css('zIndex'), 10),
		slideMe;
	slideMe = function() {
		var $t = $(this),
			$img = $t.find('img'),
			$current;
		// setting zIndex and classes:
		$img.css('zIndex', nZ-1);
		$current = $img.filter('.active').css('zIndex', nZ+1).removeClass('active');
		($current.next().is('img') ? $current.next() : $img.eq(0)).css('zIndex', nZ).addClass('active');
		// fadeOut and callback
		$current.fadeOut(1000, function() {
			$(this).css('zIndex', nZ-1).show();
		});
		$t.data('timer', setTimeout(function() {
			slideMe.call($t.get(0));
		}, 2000));
	}
	$li.find('img:first-child').addClass('active').nextAll('img').css('zIndex', nZ-1);
	// mouseover
	$li.mouseover(slideMe);
	$li.mouseout(function() {
		clearTimeout($(this).data('timer'));
		$(this).data('timer', false);
	});
}

function prepararEventos(scope) {
	initSlider(scope);
	initToggleThis(scope);
	initSlideshow(scope);
	extendLinks(scope);
}

$(function() {
	// showWidgets es para reviewpro
	if (location.href.indexOf('/reviewpro') >= 0) {
		showWidgets();
	}
	prepararEventos(document);
});


jQuery(function($) {
	$('ul.img-slider').each(function() {
		var $ul = $(this).wrap('<div class="img-slider-wrapper" />'),
			$li = $ul.children(),
			$wrap = $ul.parent().append('<a class="img-slider-prev" style="display:none" /><a class="img-slider-next" />'),
			$a = $wrap.children('a');
		// fixed height and width
		$ul.height($li.eq(0).height()).width($li.length*$ul.width());
		// preparing events for the controls, 
		// click:
		$a.click(function() {
			 // note that negative values lead you to next items
			var $t = $(this),
				n0 = parseInt($ul.css('marginLeft'), 10),
				nDir = $t.is('.img-slider-next') ? -1 : +1,
				nMaxF = 0,
				nMinF = -$ul.width()+$wrap.width(),
				// nF is set between nMinF and nMaxF
				nF = Math.max(Math.min(n0 + nDir * $wrap.width(), nMaxF), nMinF);
			if ($ul.is(':animated')) {
				return false;
			}
			$ul.animate({marginLeft: nF+'px'}, 1000, 'easeInOutQuad');
			$wrap.children('a.img-slider-next')[nF===nMinF?'fadeOut':'fadeIn']();
			$wrap.children('a.img-slider-prev')[nF===nMaxF?'fadeOut':'fadeIn']();
		})
		// hover:
		.hover(function() {
			if (!$ul.is(':animated')) {
				$(this).stop(true, true).animate({opacity:0.8}, 600);	
			}
		}, function() {
			$(this).animate({opacity:0.5}, 600);
		}).mouseout();
	});
});

//Evento para el MenuLateralHotel
$('.MenuLateralHotel > ul > li > a').live('click', function () {
	$(this).parent().addClass('active').siblings().removeClass('active');
});

