$(document).ready(function(){
	$('.fix').ifixpng();
	$('a, #btnEnviar').click(function(){
		$(this).blur();
	});
	$('.atvInclusas').find('h2:eq(1)').css({ marginTop: '20px' });
	$('#flashIntro').flash({ src: '/swf/prehome.swf', width: 934, height: 550, wmode: 'transparent' });
	$('#flashHome.pt').flash({ src: '/swf/flash_home_pt.swf', width: 690, height: 417, wmode: 'transparent' });
	$('#flashHome.en').flash({ src: '/swf/flash_home_en.swf', width: 690, height: 417, wmode: 'transparent' });
	$('#flashMapa').flash({ src: '/swf/mapa.swf', width: 500, height: 251, wmode: 'transparent' });
	$('#boxTxt, .boxTbTxt').jScrollPane({ scrollbarWidth: 7, scrollbarMargin: 10, showArrows: true });
	$('#clima').html('<img id="imgLoading" src="/img/loading.gif" alt="Carregando" />').animate({opacity: 1.0}, 1000, function(){
		$.ajax({
			url: "/code/geraTempo.php",
			cache: true,
			success: function(html){
				$('#clima').hide();
				$('#clima').html(html).fadeIn();
			}
		});
	});
	
	$('.listScrollGaleria').jcarousel({ vertical: true, scroll: 3 });
	
	$('.listScrollGaleria li a').click(function(){
	
		if($('.listScrollGaleria.resort').length)
			$local = 'resort';
		else if($('.listScrollGaleria.gastronomia').length)
			$local = 'gastronomia';
		else
			$local = 'experiencias';
		
		if($('#scrollGaleria.en').length){
			$txtClique = 'click to enlarge';
			$altCarregando = 'loading';
			$erro = 'An error occurred in loading the image';
		}
		else if($('#scrollGaleria.es').length){
			$txtClique = 'haga click para ampliar';
			$altCarregando = 'carga';
			$erro = 'Se ha producido un error en la carga de la imagen';
		}
		else if($('#scrollGaleria.pt').length){
			$txtClique = 'clique para ampliar';
			$altCarregando = 'carregando';
			$erro = 'Ocorreu um erro no carregamento da imagem';
		}
		else{
			$txtClique = 'clique para ampliar';
			$altCarregando = 'carregando';
			$erro = 'Ocorreu um erro no carregamento da imagem';
		}
		
		$(this).addClass('hover');
		$rel = $(this).attr('rel');

		$('#imgBig').html('<img id="imgLoading" src="/img/loading.gif" alt="' + $altCarregando + '" />').animate({opacity: 1.0}, 1000, function(){
			$img = new Image();
			$($img).load(function(){
		   		$(this).css('display', 'none');
		       	$('#imgBig').html('<a href="#" rel="' + $rel + '"><span>' + $txtClique + '</span></a>');
				$('#imgBig a').append(this);
				$(this).fadeIn();
		    }).error(function () {
		      	$('#imgBig').append('<p>' + $erro + '</p>');
		    }).attr('src', '/img/gallery/' + $local + '/big/' + $rel + '.jpg').height('315px').width('474px');
			
		});
		
		return false;
	});
	
	$('.thumbsTbItacare li img').mouseover(function(){
		$(this).fadeTo('fast', 1.0);
	});
	$('.thumbsTbItacare li img').mouseout(function(){
		$(this).fadeTo('fast', 0.7);
	});
	
	$('.listScrollGaleria li a').mouseover(function(){
		$(this).find('img').fadeTo('fast', 1.0);
	});	
	$('.listScrollGaleria li a').mouseout(function(){
		$(this).find('img').fadeTo('fast', 0.7);
	});
	$('#imgBig a').live("mouseover", function(){
		$(this).find('span').show();
	});
	$('#imgBig a').live("mouseout", function(){
		$(this).find('span').hide();
	});
	
	$('#imgBig a').live("click", function(){
		$rel = $(this).attr('rel');
		
		$boxTop = '46px';			
		$boxLeft = '28px';
		
		$wapWidth = $('body').width() < $('#container').width() ? $('#container').width() : $('body').width();
		$wapHeight = $('body').height() < $('#container').height() ? $('#container').height() : $('body').height();
		
		$boxWidth = 710;
		$boxHeight = 510;
		
		$distanceH = parseInt(($wapWidth - $boxWidth) / 2) + 'px';
		$distanceV = parseInt(($wapHeight - $boxHeight) / 2) + 'px';
		
		$('.lightbox').css({ width: $boxWidth + 'px', height: $boxHeight + 'px' });
		$('.box').css({ width: '656px', height: '437px' })
		$('.lightbox, .box').hide();
		$('.lightbox').css({ top: $distanceV, left: $distanceH }).append('<img id="imgLoading" src="/img/loading.gif" alt="Carregando" />');
		$('.box').css({ top: $boxTop, left: $boxLeft });
		$('.lightboxWrapper').css({ width: $wapWidth, height: $wapHeight });
		$('.lightboxWrapper').show();
		$lightbox = setTimeout("$('.lightbox, .box').fadeIn('slow')", 500);
		
		if($('.listScrollGaleria.resort').length)
			$local = 'resort';
		else if($('.listScrollGaleria.gastronomia').length)
			$local = 'gastronomia';
		else
			$local = 'experiencias';
		
		$('.box').animate({opacity: 1.0}, 500, function(){
			$img = new Image();
			$($img).load(function(){
		   		$(this).css('display', 'none');
		       	$('.box').html(this);
				$(this).fadeIn();
				$('.lightbox #imgloading').hide();
				$('.box img').css({ padding: '0', border: '1px solid #546880' });
		    }).error(function(){
		      	$('.box').html('<p>Ocorreu um erro no carregamento da imagem</p>');
		    }).attr('src', '/img/gallery/' + $local + '/full/' + $rel + '.jpg').width('650px').height('431px');
			
		});
		
	});
	
	$('.btnGast, .btnPraias, .btnListExp').click(function(){
		$(this).addClass('hover');
		$rel = $(this).attr('rel');
		
		// Padding top e left do lightbox em relação ao conteúdo;
		if($('.lightbox.itacare').length || $('.lightbox.exp').length)
			$boxTop = '60px';
		else
			$boxTop = '36px';
			
		$boxLeft = '45px';
	
		// Pega o tamanho do Wrapper do lightbox. Dessa forma, ele pega o tamanho total do container caso o usuário use uma definição menor ou igual a 1024x768; 
		$wapWidth = $('body').width() < $('#container').width() ? $('#container').width() : $('body').width();
		$wapHeight = $('body').height() < $('#container').height() ? $('#container').height() : $('body').height();
				
		// Pega o tamanho do lightbox;
		$boxWidth = 534;
		$boxHeight = 398;
		
		// Valor numérico do rel, usado nas páginas em que existe navagação entre os lightbox;
		$nav = parseInt($rel.split('x')[1]);
		
		// Calcula a distância vertical e horizontal entre o lightbox e as extremidades da página;
		$distanceH = parseInt(($wapWidth - $boxWidth) / 2) + 'px';
		$distanceV = parseInt(($wapHeight - $boxHeight) / 2) + 'px';
				
		$('.lightbox').css({ width: $boxWidth + 'px' });
		$('.lightbox, .box').hide();
		$('.lightbox').css({ top: $distanceV, left: $distanceH });
		$('.' + $rel).css({ top: $boxTop, left: $boxLeft });
		$('.lightboxWrapper').css({ width: $wapWidth, height: $wapHeight });
		$('.lightboxWrapper').show();
		$lightbox = setTimeout("$('.lightbox, .' + $rel).fadeIn('slow')", 500);
		
		$('#btnNavLeft').attr('rel', $nav - 1);
		$('#btnNavRight').attr('rel', $nav + 1);
		
		//Seta o número total de lightbox que possuem navegação em cada página;
		
		$('.btnNav').each(function(){
			if($(this).attr('rel') < 1 || $(this).attr('rel') > 9)
				$(this).hide();
			else
				$(this).show();
		});
		
		return false;
	});
		
	$('a.btnListExpAtv').click(function(){
		$(this).addClass('hover');
		$rel = $(this).attr('rel');
		
		$boxTop = '52px';
		$boxLeft = '38px';
		
		$wapWidth = $('body').width() < $('#container').width() ? $('#container').width() : $('body').width();
		$wapHeight = $('body').height() < $('#container').height() ? $('#container').height() : $('body').height();
		
		$boxWidth = 670;
		$boxHeight = 398;
		
		$distanceH = parseInt(($wapWidth - $boxWidth) / 2) + 'px';
		$distanceV = parseInt(($wapHeight - $boxHeight) / 2) + 'px';
			
		$('.lightbox').css({ width: $boxWidth + 'px' });
		$('#btnNavLeft, #btnNavRight').hide();
		$('.lightbox, .box').hide();
		$('.lightbox').css({ top: $distanceV, left: $distanceH });
		$('.' + $rel).css({ top: $boxTop, left: $boxLeft });
		$('.lightboxWrapper').css({ width: $wapWidth, height: $wapHeight });
		$('.lightboxWrapper').show();
		$lightbox = setTimeout("$('.lightbox, .' + $rel).fadeIn('slow')", 500);
		
		return false;
	});
	
	$('.btnFechar').click(function(){
		$('.btnGast, .btnListExpAtv, .btnListExp').removeClass('hover');
		$('.lightboxWrapper').hide();
		$('.lightbox').fadeOut('slow');
		$('.box').css({ top: '-9999px', left: '-9999px' });
		$('.btnNav').hide();
		if($(this).hasClass('galeria'))
			$('.box').html('');
		return false;
	});
		
	$('.btnNav').click(function(){
		$anterior = $('#btnNavLeft').attr('rel');
		$proximo = $('#btnNavRight').attr('rel');
		
		// Padding top e left do lightbox em relação ao conteúdo;
		if($('.lightbox.itacare').length || $('.lightbox.exp').length)
			$boxTop = '60px';
		else
			$boxTop = '36px';
			
		$boxLeft = '45px';
		
		$('.box').hide();
		if($(this).attr('id') == 'btnNavLeft'){
			$('.box' + $anterior).css({ top: $boxTop, left: $boxLeft }).fadeIn();
			$anterior--;
			$proximo--;
		}
		else{
			$('.box' + $proximo).css({ top: $boxTop, left: $boxLeft }).fadeIn();
			$anterior++;
			$proximo++;
		}
		$('#btnNavLeft').attr('rel', $anterior);
		$('#btnNavRight').attr('rel', $proximo);
				
		//Seta o número total de lightbox que possuem navegação em cada página;
		if($('.lightbox.itacare').length)
			$max = 14;
		else if($('.lightbox.exp').length)
			$max = 10;
		
		$('.btnNav').each(function(){
			if($(this).attr('rel') < 1 || $(this).attr('rel') > $max)
				$(this).hide();
			else
				$(this).show();
		});
	});
	
	$('#inputNome').focus();
	
	$('#formContato').validate({ errorContainer: $(".msgErro span"),
		submitHandler: function() {
			$.ajax({
				type: 'post',
				data: $('#formContato').serialize(),
				dataType: 'json',
				url: '/code/EnviaForm.php',
				success: function(json) {
					if(json.msg == "OK"){
						$('#formContato').hide();
						$('.boxSucesso').fadeIn('slow');
					}else{
						alert("Erro. Mensagem não enviada.");
					}
				}
			});
		}
	});
	$('#formCadastre').validate({ errorContainer: $(".msgError"), errorLabelContainer: $(".msgError"),
		submitHandler: function() {
			$.ajax({
				type: 'post',
				data: $('#formCadastre').serialize(),
				dataType: 'json',
				url: '/code/EnviaFormCadastre.php',
				success: function(json) {
					if(json.msg == "OK"){
						$('.boxFidelidade').hide();
						$('.msgSuccess').fadeIn();
						$('#boxTxt').jScrollPane({ scrollbarWidth: 7, scrollbarMargin: 10, showArrows: true });
					}else{
						alert("Erro. Mensagem não enviada.");
					}
				}
			});
		}
	});
	
	$("input.tel").mask("(99)9999-9999");
	$("input.nasc").mask("99/99/9999");
	$("input.cpf").mask("999.999.999-99");
	
	$('#btnVoltar').click(function(){
		$('.boxSucesso').hide();
		$('#formContato input[type="text"], #formContato textarea').val('');
		$('#formContato').fadeIn('slow');
		$('#inputNome').focus();
		return false;
	});

});

function abreLightbox(box){
	$praia = box;
	
	if($praia == 'atvInclusas'){
		$boxTop = '52px';
		$boxLeft = '38px';
		
		$boxWidth = 670;
		$boxHeight = 398;
	}
	else{
		$boxTop = '60px';
		$boxLeft = '45px';
		
		$boxWidth = 534;
		$boxHeight = 398;
	}
	
	$wapWidth = $('body').width() < $('#container').width() ? $('#container').width() : $('body').width();
	$wapHeight = $('body').height() < $('#container').height() ? $('#container').height() : $('body').height();
		
	$distanceH = parseInt(($wapWidth - $boxWidth) / 2) + 'px';
	$distanceV = parseInt(($wapHeight - $boxHeight) / 2) + 'px';
		
	$('.lightbox').css({ width: $boxWidth + 'px' });
	$('.lightbox, .box').hide();
	$('.lightbox').css({ top: $distanceV, left: $distanceH });
	$('.' + $praia).css({ top: $boxTop, left: $boxLeft });
	$('.lightboxWrapper').css({ width: $wapWidth, height: $wapHeight });
	$('.lightboxWrapper').show();
	$lightbox = setTimeout("$('.lightbox, .' + $praia).fadeIn('slow')", 500);
}