/*****CONTATO*****/
function addSubmitContatoEvent() {
	$("#frm_contato").submit(function () {
		submitContato();
		return false;
	});
}
function submitContato() {
	$("#frm_contato fieldset .msg_sucesso").remove();
	$("#frm_contato fieldset .msg_erro").remove();
	$("#frm_contato fieldset").append("<p class=\"c_loading\">enviando...</p>");
	$("#frm_contato input.bt_enviar").attr("disabled","disabled");
	$("#frm_contato input.bt_enviar").addClass("disabled");
	
	var params = $('#frm_contato').serialize();
	
	$.getJSON(URL_SUBMIT_CONTATO,
				params,
				function(retorno) {
					if (retorno.err_nome)
						$("#err_nome").html(decodeText(retorno.err_nome));
					else
						$('#err_nome').html("");
					if (retorno.err_email)
						$('#err_email').html(decodeText(retorno.err_email));
					else
						$('#err_email').html("");
					if (retorno.err_assunto)
						$('#err_assunto').html(decodeText(retorno.err_assunto));
					else
						$('#err_assunto').html("");
					if (retorno.err_msg)
						$('#err_msg').html(decodeText(retorno.err_msg));
					else
						$('#err_msg').html("");
						
					if (retorno.msg_erro)
						$("#frm_contato fieldset").append('<div class="msg_erro alert">' + decodeText(retorno.msg_erro) + '<a href="#" class="bt_fechar" onclick="$(\'div.alert\').remove();return false;">fechar</a></div>');
					else
						$("#frm_contato fieldset .msg_erro").remove();
					if (retorno.msg_sucesso) {
						$("#id_nome").val("");
						$("#id_fone").val("");
						$("#id_email").val("");
						$("#id_assunto").val("");
						$("#id_msg").val("");
						$("#frm_contato fieldset").prepend('<div class="msg_sucesso alert">' + decodeText(retorno.msg_sucesso) + '<a href="#" class="bt_fechar" onclick="$(\'div.alert\').remove();return false;">fechar</a></div>');
					}
					else {
						$("#frm_contato fieldset .msg_sucesso").remove();
					}
					$("#frm_contato fieldset p.c_loading").remove();
					$("#frm_contato input.bt_enviar").removeAttr("disabled");
					$("#frm_contato input.bt_enviar").removeClass("disabled");
					$("#frm_contato input.bt_enviar").focus();
				});
}

/*****FIM CONTATO*****/
/*****reserva*****/
function addSubmitReservaEvent() {
	$("#frm_reserva").submit(function () {
		submitReserva();
		return false;
	});
}
function submitReserva() {
	$("#frm_reserva fieldset .msg_sucesso").remove();
	$("#frm_reserva fieldset .msg_erro").remove();
	$("#frm_reserva fieldset").append("<p class=\"c_loading\">enviando...</p>");
	$("#frm_reserva input.bt_enviar").attr("disabled","disabled");
	$("#frm_reserva input.bt_enviar").addClass("disabled");
	
	var params = $('#frm_reserva').serialize();
	
	$.getJSON(URL_SUBMIT_RESERVA,
				params,
				function(retorno) {
					if (retorno.err_nome)
						$("#err_nome").html(decodeText(retorno.err_nome));
					else
						$('#err_nome').html("");
					if (retorno.err_cidade)
						$("#err_cidade").html(decodeText(retorno.err_cidade));
					else
						$('#err_cidade').html("");
					if (retorno.err_estado)
						$("#err_estado").html(decodeText(retorno.err_estado));
					else
						$('#err_estado').html("");
					if (retorno.err_fone)
						$("#err_fone").html(decodeText(retorno.err_fone));
					else
						$('#err_fone').html("");
					if (retorno.err_cep)
						$("#err_cep").html(decodeText(retorno.err_cep));
					else
						$('#err_cep').html("");
					if (retorno.err_email)
						$('#err_email').html(decodeText(retorno.err_email));
					else
						$('#err_email').html("");
					if (retorno.err_data_inicio)
						$("#err_data_inicio").html(decodeText(retorno.err_data_inicio));
					else
						$('#err_data_inicio').html("");
					if (retorno.err_data_fim)
						$("#err_data_fim").html(decodeText(retorno.err_data_fim));
					else
						$('#err_data_fim').html("");
					if (retorno.err_acomodacao)
						$('#err_acomodacao').html(decodeText(retorno.err_acomodacao));
					else
						$('#err_acomodacao').html("");
					if (retorno.err_numero_pessoas)
						$('#err_numero_pessoas').html(decodeText(retorno.err_numero_pessoas));
					else
						$('#err_numero_pessoas').html("");
					if (retorno.err_msg)
						$('#err_msg').html(decodeText(retorno.err_msg));
					else
						$('#err_msg').html("");
						
					if (retorno.msg_erro)
						$("#frm_reserva fieldset").append('<div class="msg_erro alert">' + decodeText(retorno.msg_erro) + '<a href="#" class="bt_fechar" onclick="$(\'div.alert\').remove();return false;">fechar</a></div>');
					else
						$("#frm_reserva fieldset .msg_erro").remove();
					if (retorno.msg_sucesso) {
						$("#id_nome").val("");
						$("#id_cidade").val("");
						$("#id_estado").val("");
						$("#id_cep").val("");
						$("#id_fone").val("");
						$("#id_ddd").val("");
						$("#id_email").val("");
						$("#id_data_inicio").val("");
						$("#id_data_fim").val("");
						$("#id_numero_pessoas").val("");
						$("#id_acomodacao").val("");
						$("#id_msg").val("");
						$("#frm_reserva fieldset").prepend('<div class="msg_sucesso alert"><h6>' + decodeText(retorno.msg_sucesso) + '</h6><a href="#" class="bt_fechar" onclick="$(\'div.alert\').remove();return false;">fechar</a></div>');
					}
					else {
						$("#frm_reserva fieldset .msg_sucesso").remove();
					}
					$("#frm_reserva fieldset p.c_loading").remove();
					$("#frm_reserva input.bt_enviar").removeAttr("disabled");
					$("#frm_reserva input.bt_enviar").removeClass("disabled");
					$("#frm_reserva input.bt_enviar").focus();
					
					
				});
}

/*****FIM reserva*****/

/*****ROLAGEM SUBMENU ******/
function floatSubmenu() {
	var topInicial = 110;
	var tamDiv = $('#cont-bot').height();
	var tamSubmenu = $('#submenu').height();
	var posWindowScroll = $(document).scrollTop();

	var newPosInicial = parseInt(posWindowScroll);
	var newPosFinal = parseInt(tamDiv);
	var newPosSubmenu = parseInt(tamSubmenu);

	var bottomFinal = newPosFinal - newPosSubmenu;

	if (newPosInicial < topInicial) {
		newPosInicial = topInicial;
	}

	if (newPosInicial <= bottomFinal) {
		$("#submenu").animate({top:(newPosInicial-20) + "px"}, {duration:400,queue:false});
	}
}

/*** MAILING ***/
function cadastraMailing() {
	$("#frm_mailing div[class*='alert']").remove();
	$("#frm_mailing").append("<p class=\"m_loading\">carregando...</p>");
	$("#frm_mailing input.bt_enviar").attr("disabled","disabled");
	$("#frm_mailing input.bt_enviar").addClass("disabled");
	if ($("#email_mailing").val() == "Digite aqui seu e-mail")
		var email_mailing = "";
	else
		var email_mailing = $("#email_mailing").val();
		
	$.post(URL_MAILING,
				{ email_mailing: email_mailing },
				function(data) {
					$("#frm_mailing > p.m_loading").remove();
					$("#frm_mailing input.bt_enviar").removeAttr("disabled");
					$("#frm_mailing input.bt_enviar").removeClass("disabled");
					$("#frm_mailing input.bt_enviar").focus();
					$("#frm_mailing").append(data);
					if ($("#frm_mailing .msg_sucesso").length > 0) {
						$("#email_mailing").val("");
						$("#frm_mailing .msg_sucesso").append('<a href="#">fechar</a>');
						$("#frm_mailing .msg_sucesso a").click(function () {
							$("#frm_mailing .msg_sucesso").remove();
							return false;
						});
						if ($("#email_mailing").val() == "")
							$("#email_mailing").val("Digite aqui seu e-mail");
						
					}
				});
}
/*** FIM MAILING ***/

///*** OVERLAY ***///
function addOverlay() {
	$("body").append("<div id=\"overlay\"></div>");
}

///*** LINKS EXTERNOS ***///
function createExternalLinks() {
	$("a[rel='externo']").attr("target","_blank");
	$("a[rel='externo']").attr("title",function () {
				this.title += " (abrirá em nova janela)";
			});
}

///*** CODIFICAÇÃO DE TEXTOS ***///
function decodeText(txt) {
	txt = txt.replace(/\+/g," ");
	return unescape(txt);
}

///*** SUMIR OBJETOS 
function hideSobrepostos() {
	$("embed").hide();
}
function showSobrepostos() {
	$("embed").show();
}
function excluir(msg) {
	return confirm("Tem certeza que deseja excluir este item '"
		+ msg
		+ "'? Este é um processo irreversível. Clique em OK para confirmar.");
}
function confirma(msg) {
	return confirm("Tem certeza que deseja "
		+ msg
		+ "? Este é um processo irreversível. Clique em OK para confirmar.");
}

function toggleSB(id_sb, op) {
	$(op).toggleClass("aberto");
	$("#" + id_sb).toggle();
}

function getVarLink(str_link, var_name) {
	var valor = str_link.substring(str_link.indexOf(var_name + "="));
	if (valor.search(/&/) != -1) {
		valor = valor.substring(valor.indexOf(var_name + "="),valor.indexOf("&"));
	}
	eval ("var " + valor);
	return eval(var_name);
}

/*** HOME ***/
function slideShow() {
	//alert(atual);
	atual = (atual >= total_fotos ? 0 : atual);
	var proximo = (atual + 1 >= total_fotos ? 0 : atual+1);
	$("#anima-fotos").append('<img src="' + imagens_rand[proximo] + '" id="anima' + proximo + '" alt="" style="display:none;"  />');
	$("#anima" + proximo).load(function() {
		$("#anima" + atual).fadeOut(1000, function() {
			$("#anima" + atual).remove();
			atual = proximo;
		});
		$("#anima" + proximo).fadeIn(1000);
	});
}
function preLoadRand() {
	for (var i=1; i<=imagens_rand; i++) {
		jQuery("<img>").attr("src", ABS_URL_IMAGE + i + ".jpg");
	}	
}

function initSlideShow() {
	preLoadRand();
	$("#anima-fotos").append('<img src="' + imagens_rand[0] + '" id="anima0" alt="" style="display:none;"  />');
	$("#anima0").load().fadeIn(1000, function() {
		$(this).parent().find(".loading").css("display","none");
		//$("#anima-fotos .loading").remove();
	});
	setInterval("slideShow()",5000);
}

// INICIALIZAÇÕES
$(document).ready(function() {
	createExternalLinks();
	if ($("#anima-fotos").length > 0)
		initSlideShow();
	
	if ($("#b_pousada").length == 1) {
		$(window).scroll(floatSubmenu);
		$("#submenu ul li a").click(function() {
			$("#submenu ul li a").removeClass("ativo");
			$(this).addClass("ativo");
			$.scrollTo($(this).attr("href"),400);
			return false;
		});
	}
	if ($("#b_acomodacoes").length == 1) {
		$(window).scroll(floatSubmenu);
		$("#submenu ul li a").click(function() {
			$("#submenu ul li a").removeClass("ativo");
			$(this).addClass("ativo");
			$.scrollTo($(this).attr("href"),400);;
			return false;
		});
	}
	if ($("#b_como_chegar").length == 1) {
		$(window).scroll(floatSubmenu);
		$("#submenu ul li a").click(function() {
			$("#submenu ul li a").removeClass("ativo");
			$(this).addClass("ativo");
			$.scrollTo($(this).attr("href"),400);;
			return false;
		});
	}
	if ($("#b_tarifas").length == 1) {
		$(window).scroll(floatSubmenu);
		$("#submenu ul li a").click(function() {
			$("#submenu ul li a").removeClass("ativo");
			$(this).addClass("ativo");
			$.scrollTo($(this).attr("href"),400);;
			return false;
		});
		addSubmitReservaEvent();
		//alert($("s"));		
	}
	
	/*** CADASTRO MAILING ***/
	if ($("#frm_mailing").length == 1) {
		$("#email_mailing").focus(function() {
			if($("#email_mailing").attr("value") == "Digite aqui seu e-mail")
				$("#email_mailing").attr("value","");
		});
		$("#email_mailing").blur(function () {
			if($("#email_mailing").attr("value") == "")
				$("#email_mailing").attr("value","Digite aqui seu e-mail");
	    });
	    $("#frm_mailing").submit(function() {
	    	cadastraMailing();
	    	return false;
	    });
	}
	
	/*** COMO CHEGAR - GOOGLE MAPS ***/
	function initializeGoogleMaps() {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("googleMaps"));
			map.setCenter(new GLatLng(-27.148214,-48.481636), 8);
			var point = new GLatLng(-27.148214,-48.481636);
			map.addOverlay(new GMarker(point));
			map.setUIToDefault();
		}
	}
	if ($("#googleMaps").length == 1) {
		initializeGoogleMaps();
		$(window).unload( function () { GUnload(); } );
	}
	
	/*** CONTATO ***/
	if ($("#b_contato").length == 1) {
		$("#frm_contato").ready(function() {
			addSubmitContatoEvent();
		});
	}
	
});
///*** FIM LINKS EXTERNOS ***///