﻿// Tira o bug de background em links no IE
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(r) {}

(function($){
// Redimensiona as legendas de acordo com suas respectivas fotos (obs.: tem que ser no window.load por causa de bug no Chrome) 
$(window).load(function(){
	$('.legenda').each(function(){
		$(this).css('width', $(this).siblings('img').width());
	});
});

$(function(){
	if (!$.browser.mobile){
		$('select').sb({
			//fixedWidth: true
		});
	}
	
	$('.datePicker').datepicker({
		showOn: 'both',
		buttonImage: '/img/datePickerIcon.png',
		buttonImageOnly: true,
		dateFormat: 'dd/mm/yy',
		dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
		monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
		showAnim: 'slideDown'
	});
	
	$('.datePicker2').datepicker({
		showOn: 'both',
		buttonImage: '/img/datePickerIcon.png',
		buttonImageOnly: true,
		dateFormat: 'dd/mm/yy',
		dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
		monthNames: ['janeiro','fevereiro','março','abril','maio','junho','julho','agosto','setembro','outubro','novembro','dezembro'],
		showAnim: 'slideDown'
	});
	
	$('.cform').find('textarea').TextAreaExpander(72);
	
	$('.vitrine ul').cycle({
		// Botões invertidos propositalmente, quando o rev é ativado, eles ficam ao contrário
		next: '.controlesVitrine .prev',
		prev: '.controlesVitrine .next',
		fx: 'scrollHorz',
		easing: 'easeInOutCubic',
		rev: 1,
		pause: true,
		timeout: 10e3
	});
	
	// Box de newsletter do rodapé
	$('.boxNewsLetter').css({
		'bottom': '85px',
		'opacity': 0
	});
	
	$('.btAssineNews').click(function(e){
		e.preventDefault();
		
		if ($(this).hasClass('news_ativo')){
			$('.boxNewsLetter').animate({
				'bottom': '85px',
				'opacity': 0
			}, 200, function(){
				$('.boxNewsLetter').css('visibility', 'hidden');	
			});
			
			$(this).removeClass('news_ativo')
		
		} else {
			$('.boxNewsLetter').css('visibility', 'visible');
			$('.boxNewsLetter').animate({
				'bottom': '55px',
				'opacity': 1				
			}, 200);
			
			$(this).addClass('news_ativo');
		}
	});
	
	$('input[placeholder]').placeholder();
	
	$('.btRegulamento').fancybox({
		overlayColor: '#000',
		overlayOpacity: '0.8',
		centerOnScroll: true
	});
	
	// Galeria
	var galeriaW = 0;
	$('.galeriaPost').find('li').each(function(){
		galeriaW += $(this).outerWidth(true);
	});
	
	$('.galeriaPost').rolagem({
		orientation: 'H',
		slide: 'ul',
		start: $('.galeriaPost').find('.ativo').index()
	});
	
	$('.galeriaPost').find('ul').width(galeriaW);
	
	// Hover menu dos hotéis
	$('.no-csstransitions .menuHoteis li').not('.ativo').find('a').hover(function(){
		$(this).stop(true, true).animate({
			backgroundPositionY: 0
		}, 200);
	}, function(){
		$(this).stop(true, true).animate({
			backgroundPositionY: -400
		});
	});
	
	// Botão faça sua reserva
	$('.btFacaSuaReserva').click(function(e){
		e.preventDefault();
		$('body,html').animate({
			scrollTop: 0
		});
	});
	
	// Tabela de reservas
	$('.tabZebra').find('tr:odd').addClass('odd');
	
	// Botões do Como Chegar
	$('.btsComoChegar').find('a').click(function(e){
		e.preventDefault();
		
		$(this).parent().siblings('.ativo').removeClass('ativo');
		$(this).parent().addClass('ativo');
		
		// temporario
		$('#mapaComoChegar').find('img').fadeOut(250).delay(1000).fadeIn(250);
	});
	
	// Links tratamentos Spas
	$('.linksTratamento').find('li:odd').addClass('odd');
	
	$('.linksTratamento li').find('a').each(function(){
		var $a = $(this),
			data = $a.data(),
			img = (data.img) ? '<img src="'+ data.img +'" />' : '',
			$html = $('<div class="boxInfoTratamento"><div>'+ img +'<p>'+ data.texto +'</p></div> <span></span> </div>');
		
		$a.hover(function(){
			$('.boxInfoTratamento').filter(':animated').stop(true, true);
			$html.hide();
			$(this).append($html.stop(true, true).fadeIn());
		}, function(){
			$html.stop(true, true).fadeOut(function(){
				$html.remove();
			});
		});
	});
		
	// histórico
	var $HistoricoLi = $('.listaHistorico').find('li');
	$HistoricoLi.find('a').each(function(){
		var testImg = /src=\w+/gi;
		var $a = $(this),
			data = $a.data(),
			img = data.img && testImg.test(data.img) ? '<img src="'+ data.img +'" />' : '',
			$html = $('<div class="boxListaHistorico"><div>'+ img +'<p>'+ data.texto +'</p></div> <span></span> </div>');
		
		$a.hover(function(){
			$('.boxListaHistorico').filter(':animated').stop(true, true);
			$html.hide();
			$(this).append($html.stop(true, true).fadeIn("fast"));
		}, function(){
			$html.stop(true, true).fadeOut(function(){
				$html.remove();
			});
		});
	});
	$HistoricoLi.width(100 / $HistoricoLi.length + '%');
	
	// Cam ao vivo
	var elm = $('#camAgora');
	var src = elm.attr('src');

	function updateCam(){
		elm.attr('src', src + '?' + new Date().getTime());
		setTimeout(updateCam, 2500); 
	}
	
	updateCam();
	
	// Botão de ajuda
	$('.btHelp').click(function(e){
		e.preventDefault();
		
		if ($('.boxHelp').length == 0){
			var $me = $(this);
			var data = $me.data('popup');
			var interno = $('<div />').css({
				padding: 10,
				border: '1px solid #CCC'	
			}).append(data);
			
			var d = $('<div />').append(interno).append('<b/>').addClass('boxHelp');
			
			$('body').prepend(d);
			
			d.css({
				top: $me.offset().top - d.outerHeight() - 12,
				left: $me.offset().left - (d.outerWidth() / 2) + ($me.width() / 2)
			});
		} else {
			$('.boxHelp').remove();
		}
		
		$(this).focusout(function(){
			$('.boxHelp').remove();	
		});
		
	});
	
	// Twitter - rodapé
	$.twitter('plazahoteis', 1, function(d){
		$('.boxTwitter').find('p').html(d[0].text);
	});
	
	// Modal da lateral
	$('.galeriaLateral').find('a[href$=".jpg"], a[href$=".png"], a[href$=".jpeg"], a[href$=".gif"]').fancybox({
		overlayColor: '#000',
		overlayOpacity: .8,
		padding: 4,
		centerOnScroll: true,
		titlePosition: 'over',
		onComplete: function(){
			var $title = $('#fancybox-title');
			
			if (!$title.text().replace(/\s/, '')){
				$title.remove();
			} else {
				$title.hide();
				
				$('#fancybox-wrap').hover(function(){
					$title.animate({
						'height': 'show'
					}, 300);
				}, function(){
					$title.animate({
						'height': 'hide'
					}, 300);
				});
			}
		}
	});
	
	$('input.disabled').click(function(e){
		e.preventDefault();
	});
});

	// abre menu colapsível
	$(function(){
		function fixIEReflow(){ document.body.className = document.body.className; }
		
		$('.boxPrevisao .btColap').click(function() {
			var $h4 = $(this).siblings('h4'),
				$parent = $(this).parent();
	
			// Como o IE7 não dá um reflow nos items da galeria, temos que usar essa função, para força-lo
			// 13ms por que é o mesmo framerate das animações do jQuery
			var timer = !!document.all && setInterval(fixIEReflow, 13);
			
			if (!$parent.hasClass('boxPrevisaoMenuAtivo')){
				$h4.stop(true, true).fadeIn();
				$parent
					.addClass('boxPrevisaoMenuAtivo')
					.siblings('.boxInterno').stop(1, 1).slideDown(400, 'easeOutBack', function(){
						clearInterval(timer);
					});
			} else {
				$h4.stop(1, 1).fadeOut();
				$parent
					.removeClass('boxPrevisaoMenuAtivo')
					.siblings('.boxInterno').stop(1, 1).slideUp(400, 'easeOutBack', function(){
						clearInterval(timer);
					});
			}
		
			return false;
		});
	});

}(jQuery));
