///*** ECOBAGS ***///
function closePopup() {
	$("#img_ampliada").fadeOut(200, function() {
		$("#overlay").slideUp(300);
	});
}

function addPopupImagem() {
	$("body").append('<div id="img_ampliada"><div class="int"><h2>&nbsp;</h2><a href="#">fechar</a></div></div>');
	$("#img_ampliada").ready(function() {
		$("#img_ampliada .int a").click(function() {
			closePopup();
			return false;
		});
	});
}

///*** 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)";
			});
}

function decodeText(txt) {
	txt = txt.replace(/\+/g," ");
	return unescape(txt);
}

function hideSobrepostos() {
	$("embed").hide();
}
function showSobrepostos() {
	$("embed").show();
}


/*****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_fone)
						$('#err_fone').html(decodeText(retorno.err_fone));
					else
						$('#err_fone').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_fone").val("");
						$("#id_email").val("");
						$("#id_assunto").val("");
						$("#id_msg").val("");
						$("#frm_contato fieldset").prepend('<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*****/

// INICIALIZAÇÃO
$(document).ready(function() {
	createExternalLinks();
	
	/*** ECOBAGS ***/
	if ($("#b_ecobags").length == 1) {
		addPopupImagem();
		addOverlay();
		$("a.ampliar").click(function() {
			$("#img_ampliada, #img_ampliada .int").css("display","none");
	
			$("#overlay").css("height",$(document).height() + "px");
			
			var hwindow = $(window).height();
			var himg_ampliada = $("#img_ampliada").outerHeight();
			
			if (hwindow <= himg_ampliada)
				$("#img_ampliada").css("top",$(document).scrollTop()+15 + "px");
			else
				$("#img_ampliada").css("top",$(document).scrollTop()+((hwindow-himg_ampliada)/2) + "px");
			
			
			
			$("#overlay").slideDown(300, function() {
				$("#img_ampliada").fadeIn(200);
			});
			hideSobrepostos();
			
			var src_img = $(this).attr("href");
			var alt_img = $(this).attr("name");
			
			if ($("#img_ampliada .int img").length == 0)
				$("#img_ampliada .int").append('<img src="' + src_img + '" alt="' + alt_img + '" />');
			else {
				$("#img_ampliada .int img").attr("src",src_img);
				$("#img_ampliada .int img").attr("alt",alt_img);
			}
			$("#img_ampliada .int h2").html(alt_img);
			
			$("#img_ampliada .int img").load(function() {
				$("#img_ampliada .int").fadeIn(300);
			});
			
			return false;
		});
	}
	
	if ($("#b_fale-conosco").length == 1) {
		$("#frm_contato").ready(function() {
			addSubmitContatoEvent();
		});
	}
	
});
///*** FIM LINKS EXTERNOS ***///