///*** 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)";
			});
}

///*** GERAIS ***///
function decodeText(txt) {
	txt = txt.replace(/\+/g," ");
	return unescape(txt);
}

function hideSobrepostos() {
	$("embed").hide();
}
function showSobrepostos() {
	$("embed").show();
}
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);
}

// MAILING
function cadastraMailing() {
	$("#frm_mailing div[class*='alert']").remove();
	$("#frm_mailing").append("<p class=\"loading\">enviando...</p>");
	$("#frm_mailing input.botao").attr("disabled","disabled");
	$("#frm_mailing input.botao").addClass("disabled");
	var email_mailing = $("#email_mailing").val();
	var url_post = $("#frm_mailing").attr("action") + "Ajax";
	$.post(url_post,
				{ email: email_mailing },
				function(data) {
					$("#frm_mailing > p.loading").remove();
					$("#frm_mailing input.botao").removeAttr("disabled");
					$("#frm_mailing input.botao").removeClass("disabled");
					$("#frm_mailing input.botao").focus();
					if (data.search(/sucesso/i) != -1)
						$("#email_mailing").val("");
					$("#frm_mailing").append(data);
					$("#frm_mailing div[class*='alert']").fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
				});
	return false;
}
function limpaPreencheCampoEmail() {
	var msg_default = "Digite aqui seu e-mail";
	$("#email_mailing").focus(function() {
		if ($(this).val() == msg_default)
			$(this).val("");
	}).blur(function() {
		if ($(this).val() == "")
			$(this).val(msg_default);
	});
}

/*****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();
	var url_submit = $('#frm_contato').attr("action") + "Ajax";
	
	$.getJSON(url_submit,
				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">' + decodeText(retorno.msg_erro) + '</div>');
					else
						$("#frm_contato fieldset .msg_erro").remove();
					if (retorno.msg_sucesso) {
						$("#id_nome").val("");
						$("#id_email").val("");
						$("#id_assunto").val("");
						$("#id_msg").val("");
						$("#frm_contato fieldset").append('<div class="msg_sucesso">' + decodeText(retorno.msg_sucesso) + '</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*****/


/***** VÍDEOS *****/
function loadVideo(nVideo,autoPlay) {
	if (typeof videos != "undefined" && $('#videoSwf').length > 0) {
		var codeVideo = videos[nVideo];
		var width = 380;
		var height = 305;
		
		var html_video = '<object width="' + width + '" height="' + height + '">' +
							'<param name="movie" value="http://www.youtube.com/v/' + codeVideo + '&hl=pt-br&fs=1&autoplay=' + autoPlay + '"></param>' +
							'<param name="allowFullScreen" value="true"></param>' +
							'<param name="allowscriptaccess" value="always"></param>' +
							'<embed src="http://www.youtube.com/v/' + codeVideo + '&hl=pt-br&fs=1&autoplay=' + autoPlay + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed>' +
						'</object>';
		
		$('#videoSwf').html(html_video);
		
		//$("#apresentacaoViaAventura .nav a[class *= 'video']").removeClass('ativo');
		//$("#apresentacaoViaAventura .nav a.video" + nVideo).addClass('ativo');
		/*$('#videoSwf').html("");
	    $('#videoSwf').flash({
	        src:ABS_URL_PLAYER_VIDEO_SWF,
	        width:361,
	        height:305,
	        wmode:'transparent',
	        flashvars: {
	    		urlVideo: urlVideo,
	    		urlSkin: ABS_URL_SKIN_PLAYER_VIDEO_SWF,
	    		playAuto: autoPlay }
	    });*/
	}
}

function inicializaVideo() {
	loadVideo(1,0);
    $(".lista_videos a").click(function() {
    		var str_link = $(this).attr("href");
    		var pi = str_link.search(/#v/);
			var nVideo = str_link.substr(pi+2);
			loadVideo(nVideo,1);
			return false;
        });
}
/***** FIM VÍDEOS *****/


/***** Troca de páginas *****/
function carregaDinamica() {
	$("#header ul li[class!='blog'] a").click(function() {
		$("#secao_dinamica").html('<p class="loading">Carregando...</p>');
		$("#header ul li a").removeClass("ativo");
		$(this).addClass("ativo");
		var params = "";
		var url_page = links_paginas[$(this).parent().attr("class")];
		$.get(url_page,
					params,
					function(retorno) {
						$("#secao_dinamica p.loading").fadeOut(300, function() {
							$("#secao_dinamica").html(retorno);
							initDinamica();
						});
					});
		
		return false;
	});
}


/*** INICIALIZACOES ***/

function rolaSecaoDinamica() {
	$(function() {
		/*if (!$.browser.msie) {
			setTimeout(function() {
				$("#nav_pag li a").fadeOut(1000);
				},2000);
		
			$("#nav_pag li").mouseover(function(event) {
				$(this).find("a").show();
					},3000).mouseout(function(event) {
						$(this).find("a").hide();
					},3000);
		}*/
		
		$('#secao_dinamica .scroll').serialScroll({
			items:'li',
			prev:'#nav_pag li.p a',
			next:'#nav_pag li.n a',
			start:0, //as we are centering it, start at the 2nd
			duration:300,
			force:true,
			stop:true,
			cycle:true,
			easing:'easeOutQuad',
			lock:false
		});
	});
}

function addEventsSidebar() {
	$("#sidebar a.agenda").click(function() {
		$("#sidebar .box[id!='boxAgenda']").slideUp(50);
		$("#boxAgenda").slideToggle(100);
		return false;
	});
	$("#sidebar a.contato").click(function() {
		$("#sidebar .box[id!='frm_contato']").slideUp(50);
		$("#frm_contato").slideToggle(100);
		return false;
	});
	$("#sidebar a.imprensa").click(function() {
		$("#sidebar .box[id!='boxImprensa']").slideUp(50);
		$("#boxImprensa").slideToggle(100);
		return false;
	});
}
function initDinamica() {
	rolaSecaoDinamica();
	inicializaVideo();
}

function init() {
	createExternalLinks();
	$("#frm_mailing").submit(cadastraMailing);
	addSubmitContatoEvent();
	addEventsSidebar();
	carregaDinamica();
	initDinamica();
}

$(document).ready(function() {
	init();
});
///*** FIM LINKS EXTERNOS ***///