$(document).ready(function() {

    var cordenadas = $("#topo").position();

    $(".pointer").mouseover(function() {
        $(this).css("cursor", "pointer");
    }); 
    

    $("._btnVoltar").click(function() {
        history.go(-1);
    });

    $("._btnVoltar").mouseover(function() {
        $(this).css("cursor", "pointer");
    });

    $("#menu-lista a").mouseover(function() {
        voltaMenus();
    });


    $(".news-form").click(function() {
        $(this).attr("value", "");
        $(this).css("color", "#000000");
    });




    $('#amplFotoDiv').scrollFollow( {
        speed : 500 ,
        offset : 0
    });
    
    $("#topo-busca-select").change(function() {
        $("#topo-form").submit();
    });

    var buscaIni = "buscar";

    $("#topo-form-busca-input").val(buscaIni);

    $("#topo-form-busca-input").click(function() {
        if($("#topo-form-busca-input").val()==buscaIni) {
            $("#topo-form-busca-input").val("");
        } 
    });


});

function voltaMenus() {
    $("#artigos-drop-menu").css('display', 'none');
    $("#atuacao-drop-menu").css('display', 'none');
    $("#multimidia-drop-menu").css('display', 'none');
    $("#noticias-drop-menu").css('display', 'none');
    voltaSubmenu();
}

function voltaSubmenu() {
    $("#atuacao-parlamentar-drop-menu").css('display', 'none');
}


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 amplFoto(foto , legenda) {
    $("#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
    }, 600);

    $("#amplFotoDiv").slideDown(1000);
    
    $("#amplFoto").html("<img src='../img-"+foto+"-500-400' alt='"+legenda+"' /> <br> " + legenda);

// $("#superConteudo").html(conteudoPagina);
}

function fecharFoto() {
    $("#cobreTudo").animate( {
        opacity :0,
        borderWidth :1
    }, 1000, function() {
        $("#cobreTudo").hide();
    });

    $("#amplFotoDiv").slideUp(1000);

}


function validarNewsletter() {
    if($("#news-form-nome").val() == "" || $("#news-form-nome").val() == "nome") {
        alert("Preencha seu nome para realizar o cadastro");
        $("#news-form-nome").focus();
        $("#news-form-nome").css("border" , "1px solid red");
        return false;
    }

    if($("#news-form-email").val() == "" || $("#news-form-email").val() == "e-mail") {
        alert("Preencha seu e-mail para realizar o cadastro");
        $("#news-form-email").focus();
        $("#news-form-email").css("border" , "1px solid red");
        return false;
    }

    if(!validarEmail($("#news-form-email").val())) {
        alert("Preencha preencha um email valido");
        $("#news-form-email").focus();
        $("#news-form-email").css("border" , "1px solid red");
                return false;
    }

    return true;
}

function getURL(link) {
	document.location.href = link;
}


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 validarCadastro() {
    var retorno = false;
    retorno =  validarFormulario( new Array(
        'nome' ,
        'email' ,
        'senha' ,
        'senha2' ,
        'nascimento' ,
        'logradouro' ,
        'numero' ,
        'complemento' ,
        'cep' ,
        'bairro' ,
        'cidade' ,
        'estado'
        ));

    if(retorno) {
        if(!validarEmail($("#email").val())) {
            alert("Preencha um email válido");
            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;
    }
}


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>");
        }
    

    });
}


function carregarRepresentantes(sigla) {
    $("#repLista").attr("src", "representantes_lista-"+sigla);
}


function carregarRepresentantesTella(sigla) {
    $("#repLista").attr("src", "representantes_lista_tella-"+sigla);
}

    function isEmpty(val) {
        var emptyCases = new Array("" , null , undefined);
        for(var i=0 ; i<emptyCases.length ; ++i) {
            if(val==emptyCases[i]) {
                return true;
            }
        }
        return false;
    }