/*Cadastro para Newsletter
=======================================================================*/
function is_email(email) {
    er = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/;

    if (er.exec(email)) {

        return true;
    } else {

        return false;
    }
}

function cadNews() {
    if (($("#cadNome").val() != "" || $("#cadNome").val() != "Nome" || $("#cadEmail").val() != "" || $("#cadEmail").val() != "Email") && is_email($("#cadEmail").val()) != false) {
        $("#imgLoad").show("fast", function () {
            $("#btNews").attr("disabled","disabled");
        });
        $.ajax({
            type: "POST",
            url: "/Contato/GravarEmail",
            data: "nome=" + $("#cadNome").val() + "&email=" + $("#cadEmail").val() + "",
            success: function (msg) {
                alert(msg);
                $("#cadNome").val("Nome");
                $("#cadEmail").val("Email");
                $("#imgLoad").hide("fast", function () {
                    $("#btNews").attr("disabled", "");
                });
            }
        });
    } else {
        alert("Preencha todos os campos corretamente.");
    }
};

/*Controle contato rodape
=======================================================================*/

function contatoRodape() {
    $("#contatoRodape").slideToggle("fast", function () { 
        if(!$("#contatoRodape").is("hidden")){
            $.scrollTo('#contatoRodape', 800);
        }
    });
}

/*Scroll Topo
=======================================================================*/

function proTopo() {
    $.scrollTo('#divTopo', 800);
}

/*Carrega portifolio
========================================================================*/

/**
 * Array de objectos de qual caracter deve substituir seu par com acentos
 */
var specialChars = [
	{val:"a",let:"áàãâä"},
	{val:"e",let:"éèêë"},
	{val:"i",let:"íìîï"},
	{val:"o",let:"óòõôö"},
	{val:"u",let:"úùûü"},
	{val:"c",let:"ç"},
	{val:"A",let:"ÁÀÃÂÄ"},
	{val:"E",let:"ÉÈÊË"},
	{val:"I",let:"ÍÌÎÏ"},
	{val:"O",let:"ÓÒÕÔÖ"},
	{val:"U",let:"ÚÙÛÜ"},
	{val:"C",let:"Ç"},
	{val:"",let:"?!()"}
];
 
/**
 * Função para substituir caractesres especiais.
 * @param {str} string
 * @return String
 */
function replaceAcentos(str) {
	var $spaceSymbol = '-';
	var regex;
	var returnString = str;
	for (var i = 0; i < specialChars.length; i++) {
		regex = new RegExp("["+specialChars[i].let+"]", "g");
		returnString = returnString.replace(regex, specialChars[i].val);
		regex = null;
	}
	return returnString.replace(/\s/g,$spaceSymbol);
};

function formataTitulo(titulo) {
    var str = titulo;
    str = replaceAcentos(str);
    str = str.replace(" ", "-");
    str = str.replace(",", "");
    str = str.replace(".", "");
    str = str.replace(":", "");
    str = str.replace("*", "");
    str = str.replace("{", "");
    str = str.replace("}", "");
    str = str.replace("(", "");
    str = str.replace(")", "");
    str = str.replace("=", "");
    str = str.replace(";", "");
    str = str.replace("+", "");
    str = str.replace("*", "");
    return str;
}

function carregaPortifolio(idCategoria) {
    var pagina = $("#pagina" + idCategoria).val();
    var classe = 0;
    $.getJSON("/Portfolio/carregaPortifolio", { idCategoria: idCategoria, pagina: pagina }, function(data) {
        $.each(data.Result, function(index, es) {
            var cliente = "'" + es.cliente + "'";
            if (classe / 2 == 0) {
                classe = "colunaPort2";
            } else {
                classe = "colunaPort1";
            }
            var url = "/Portfolio/Cliente/" + jQuery.trim(formataTitulo(cliente)) + "/" + es.id;
            url = url.replace("'", "");
            url = url.replace("'", "");
            $("#port" + idCategoria).append("<div class='" + classe + "' style='margin-bottom:20px;display:none;' id='thumb" + es.id + "'><img src='/Content/arquivos/" + es.imgDestaque + "' /><div class='descPortfolio'><a href=" + url + ">" + es.cliente + "</a></div></div>");
            pagina++;
            $("#pagina" + idCategoria).val(pagina);
            fadeThumb(es.id);
            classe++;
        });
    });
}

function fadeThumb(id) {
    $("#thumb"+id).fadeIn("fast");
}

