$(document).ready(function() {

    var sombraImg = '<img src="../media/images/site/portfolio-sombra.png" alt="Sombra" />';
    $(".img-mini-item-portfolio").mouseover(function() {
        $(this).html("");
    });

    $(".img-mini-item-portfolio").mouseout(function() {
        $(this).html(sombraImg);
    });

    $("#cobreTudo").css("oacity" , "0.4");
    $("#focus").focus();

    $(".pointer").mouseover(function() {
        $(this).css("cursor", "pointer");
    }); 

    $("._voltar").click(function() {
        history.go(-1);
    });

    $("._voltar").mouseover(function() {
        $(this).css("cursor", "pointer");
    });


    
    $("#interna-vertical").css("height" , $("#principal-interno").height() - 1);
    
    
    $(".img-mini-item").css("cursor" , "pointer");
    
//    $(".img-mini-item").click(function() {
//        $("#interna-img-principal").html( "<img onclick='amplFotoImg(this)' class='ampl-img pointer' src='../img-"+$(this).attr("title")+"-350-250'  alt='"+$(this).attr("title")+"' /> <div class='img-legenda'>"+$(this).attr("id")+"</div>" );
//    });

    $(".ampl-img").click(function() {
        amplFoto($(this).attr("alt") , "");
    });



    $("#baixo-newsletter-email").click(function() {
        if($(this).val()=="e-mail") {
            $(this) .val("");
        }
    });

    $("#newsletter-form").submit(function() {
        if(isEmpty($("#baixo-newsletter-email").val()) || $("#baixo-newsletter-email").val()=="e-mail" || !validarEmail($("#baixo-newsletter-email").val()) ) {
            $("#baixo-newsletter-email").focus();
            $("#newsletter-form").after("<span class='aviso'>Digite um email válido</span>");
            return false;
        } else {
            return true;
        }
    });

    $("#buscar-input-text").click(function() {
        if($(this).val()=="buscar") {
            $("#buscar-input-text").val("");
        }
    });

    $("#menu-busca-form").submit(function() {
        if($("#buscar-input-text").val()=="" || $("#buscar-input-text").val()=="buscar") {
            $("#buscar-input-text").focus();
            return false;
        } else {
            return true;
        }

    });


    $(".img-mini-item-portfolio").css("cursor" , "pointer");

    //    $(".img-mini-item-portfolio").click(function() {
    //
    //        $("#interna-img-principal").html( "<img class='ampl-img' src='../img-"+$(this).attr("title")+"-750-550'  alt='"+$(this).attr("title")+"' /> <div class='img-legenda'>"+$(this).attr("id")+"</div>" );
    //    });


    
    
    $('#amplFotoDiv').scrollFollow( {
        speed : 500 ,
        offset : 0
    });


    $('#acessibilidade-menos').click(function() {
        fonteMenor();
    });

    $('#acessibilidade-normal').click(function() {
        fonteNormal();
    });

    $('#acessibilidade-mais').click(function() {
        fonteMaior();
    });

    $('#acessibilidade-contraste').toggle(function(){
        $("#texto").addClass("contraste");
    }, function() {
        $("#texto").removeClass("contraste");
    });

    $(".acessibilidade-item").mouseover(function() {
        $(this).addClass("btn-over");
    });

    $(".acessibilidade-item").mouseout(function() {
        $(this).removeClass("btn-over");
    });

    $("#fechar-btn").click(function() {
        fecharJanela();
    });

    $("#enviar-para-um-amigo-btn").click(function(e){
        enviarParaUmAmigo();
        $("#janela").css("top" ,e.pageY );
        $("#janela").css("left" , e.pageX - 150);
    });

});

function aplFotoPortfolio(img , redimensionar , legenda) {
    if(redimensionar=='2') {
        $("#interna-img-principal").html( "<img class='ampl-img' src='../files/foto_pagina/"+img+"'  alt='"+$(this).attr("title")+"' /> <div class='img-legenda'>"+legenda+"</div>" );
    } else {
        $("#interna-img-principal").html( "<img class='ampl-img' src='../img-"+WEB_DIR+"/files/foto_pagina/"+img+"-750-550'  alt='"+$(this).attr("title")+"' /> <div class='img-legenda'>"+legenda+"</div>" );
    }
}

function ampl(img , redimensionar , legenda) {
    if(redimensionar=='2') {
        $("#interna-img-principal").html( "<img onclick='amplFotoImg(this)' class='ampl-img pointer' src='../files/foto_pagina/"+img+"'  alt='"+WEB_DIR+"/files/foto_pagina/"+img+"' /> <div class='img-legenda'>"+legenda+"</div>" );
    } else {
        $("#interna-img-principal").html( "<img onclick='amplFotoImg(this)' class='ampl-img pointer' src='../img-"+WEB_DIR+"/files/foto_pagina/"+img+"-350-250'  alt='"+WEB_DIR+"/files/foto_pagina/"+img+"' /> <div class='img-legenda'>"+legenda+"</div>" );
    }
}


function enviarParaUmAmigo() {
    exibirJanela();

    sf.ajaxLoad("ajax/enviar_para_um_amigo", "janela-conteudo");
}

function exibirJanela() {
    $("#janela").slideDown("fast");
}

function fecharJanela() {
    $("#janela").slideUp("fast");
}


atualFonteSize = 12;
maxFontSize = 22;

function fonteMaior() {
    atualFonteSize = atualFonteSize + 1;

    if(atualFonteSize >= maxFontSize) {
        atualFonteSize = maxFontSize;
    }

    $("#texto").css("font-size", (atualFonteSize)+ "px");
}

function fonteNormal() {
    atualFonteSize = 12;

    if(atualFonteSize >= maxFontSize) {
        atualFonteSize = maxFontSize;
    }

    $("#texto").css("font-size", (atualFonteSize)+ "px");
}

function fonteMenor() {
    atualFonteSize = atualFonteSize - 1;

    if(atualFonteSize >= maxFontSize) {
        atualFonteSize = maxFontSize;
    }

    $("#texto").css("font-size", (atualFonteSize)+ "px");
}

function fonteContraste() {
    $("#texto").addClass("contraste");
}



function amplFotoImg(obj) {
    amplFoto($(obj).attr("alt") , "");
}


function getURL(href) {
    document.location.href= href;
}

function getUrl(href) {
    getURL(href);
}

function hide(element) {
    $(element.id).hide("slow");
}


function down(element) {
    $(element).slideDown("slow");
}

function up(element) {
    $(element).slideUp("slow");
}

function cobrirTudo() {
    $("#cobreTudo").css("width", $(document).width());
    $("#cobreTudo").css("height", $(document).height());
    $("#cobreTudo").css("opacity", "0");
    $("#cobreTudo").css("display", "block");

    $("#cobreTudo").animate( {
        opacity :0.5,
        borderWidth :1
    }, 300);

}


function amplFoto(foto , legenda) {

    cobrirTudo();

    $("#amplFotoDiv").slideDown(500);
    
    $("#amplFoto").html("<img src='../img-"+foto+"-640-480' alt='"+legenda+"' /> <br> " + legenda);


//alert("<img src='"+foto+"' alt='"+legenda+"' /> <br> ");

// $("#superConteudo").html(conteudoPagina);
}

function fecharFoto() {
    $("#cobreTudo").animate( {
        opacity :0,
        borderWidth :1
    }, 500, function() {
        $("#cobreTudo").hide();
    });

    $("#amplFotoDiv").slideUp(1000);

}



function limparSelect(selectId) {
    $("#" + selectId).html("");
}

function addListValue(inputComValor, targetSelect) {
    var valor = $("#" + inputComValor).attr("value");
    $("#" + targetSelect).append(
        "<option value='" + valor + "' selected='selected'>" + valor
        + "</option>");
    $("#" + inputComValor).attr("value", "");
}


function validarContato() {
    var retorno = false;
    retorno =  validarFormulario( new Array('nome' , 'email' , 'mensagem'));
    return retorno;
}

function validarEnquete() {
    var retorno = false;
    retorno =  validarFormulario( new Array('nome' , 'email'));

    if(retorno) {
        if(!validarEmail($("#email").val())) {
            alert("Preencha um email válido");
            retorno = false;
        }
    }


    return retorno;
}


function  validarFormulario (campo) { //campo tem as IDs dos campos obrigatorios
    tamanho =  campo.length;
    var camp = campo;

    for(i=0;i<camp.length ; i++) {
        if(document.getElementById(camp[i]).value=='' || document.getElementById(camp[i]).value==null) {
            window.alert("Preencha o campo "+ camp[i].toUpperCase() );
            document.getElementById(camp[i]).style.border = "1px solid #C75F4E";
            document.getElementById(camp[i]).focus();

            return false;
        }
    }

    return true;
}

function validarBusca() {
    
    if($("#topo-form-busca").val() == "" || $("#topo-form-busca").val() == null) {
        $("#topo-form-busca").after().html("<div class='form-busca-erro'>Preencha um termo para busca</div>");
        return false;
    } else {
        return true;
    }

}

function enviarBusca() {
    if($("#topo-form-busca").val() != "buscar") {
        $("#form-busca").submit();
    } else {
        $("#topo-form-busca").focus();
        alert("Selecione algum termo para busca");
    }
    
}


function validarInscricao() {
    var retorno = false;
    retorno =  validarFormulario( new Array('nome' , 'email' , 'telefone' , 'instituicao')  );

    if(retorno) {
        if(!validarEmail($("#email").val())) {
            alert("Preencha um email valido");
            retorno = false;
        }
    }

    if($("#senha"))


        return retorno;
}


function validarEmail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){
            return true;
        }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
            return true;
        }
    }else{
        return false;
    }
    return false;
}


function carregarCidades(estado) {

    $("#cidade").attr("disabled", "disabled");
    $("#cidade").html(
        "<option style='color: silver'>Carregando as cidades....</option>");

    $.post("carregar_cidades.ajax.php", {
        estado :estado
    }, function(data) {
        var valores = data.split(",");
        $("#cidade").html("");
        $("#cidade").attr("disabled", "");

        $("#cidade").append(
            "<option value=''>..:: Selecione uma Cidade ::..</option>");

        for (i = 0; i < valores.length; ++i) {

            nome = valores[i];
            i++;
            id = valores[i];

            $("#cidade").append(
                "<option value='" + id + "'>" + nome + "</option>");
        }
    

    });
}

/**
* Retorna TRUE caso value seja vazio
* 
* @param value String
* @return boolean
*/
function isEmpty(value) {
    
    var emptyCases = new Array("" , undefined , null);
    for(var i=0 ; i<emptyCases.length ; ++i) {
        if(value==emptyCases[i]) {
            return true;
        }
    }

    return false;

}

