self.efetuar_pagamento = function(forma_de_pagamento) {
    if(forma_de_pagamento == 'pagseguro') {
        $('#form-pagseguro').submit();
    }
    if(forma_de_pagamento == 'transferencia' || forma_de_pagamento == 'boleto') {
        $('#form-a-vista').submit();
    }
}

self.alterar_aba = function(aba) {
    $('div[class=aba]').each(function(){
        $(this).hide();
    });
    $(aba).show();
}

self.open_certificado = function() {
  window.open('https://smarticon.geotrust.com/smarticonprofile?Referer=https://www.justshop.com.br', 'certificado', 'height=600,width=400');
}

$(document).ready(function()
{

    /* Limpando Busca e Newsletter */
    if($('#busca_palavra_chave').val() != '') {
        $('#busca_palavra_chave').clearField();
    }

    $('#busca_familia_pai_id').change(function() {
        $('#busca_palavra_chave').val(' ');
    });

    if($('#newsletter_nome').val() != '') {
        $('#newsletter_nome').clearField();
    }

    if($('#newsletter_email').val() != '') {
        $('#newsletter_email').clearField();
    }

    /* Menu de Categorias */

    $('.menu li .sub-menu').hide();
    $('.menu li').each(function()
    {
        if($(this).children().next().is('.sub-menu'))
        {
            $(this).addClass('fechada');
            $(this).hover(function(){
                $(this).removeClass('fechada');
                $(this).addClass('aberta');
                $(this).children('.sub-menu').show();
            }, function()
            {
                $(this).addClass('fechada');
                $(this).removeClass('aberta');
                $(this).children('.sub-menu').hide();
            });
        }
    });

    /* Formulário de Cadastro */

    $('#cadastro_class_name_cliente_fisica').click(function() {
        $('#s_nome_extendido').html("* Nome Completo:");
        $('#s_nome_resumido').html("* Quero ser chamado por:");
        $('#s_cpf_cnpj').html("* CPF:");
        $('#li_sexo').show();
        $('#li_data_nascimento').show();
        $('#li_inscricao_estadual').hide();
        $('#li_contato').hide();
    });

    $('#cadastro_class_name_cliente_juridica').click(function() {
        $('#s_nome_extendido').html("* Razão social:");
        $('#s_nome_resumido').html("* Nome Fantasia:");
        $('#s_cpf_cnpj').html("* CNPJ:");
        $('#li_contato').show();
        $('#li_sexo').hide();
        $('#li_data_nascimento').hide();
        $('#li_inscricao_estadual').show();
    });

    /* Cálculo de Frete */
        
    $("input:radio[name=endereco_entrega_id]").click(function() {
        endereco_entrega_id = $("input:radio[name=endereco_entrega_id]:checked").val();
        window.location.href = "/home/endereco_entrega?endereco_entrega_id=" + endereco_entrega_id;
    });

    /* Fechar Pedido */

    self.gerar_pagamento = function(forma_pagamento) {
        if(forma_pagamento == "pagseguro") {
            $('#form-pagseguro').submit();
        } else {
            $('#form-a-vista').submit();
        }

    }

    if($("#pedido-forma-pagamento").text() != undefined && $("#pedido-forma-pagamento").text() != '') {
        gerar_pagamento($("#pedido-forma-pagamento").text());
        $("#pedido-forma-pagamento").html(" ");
    }

    $("select:[id$=_quantidade]").change(function(){
        $("#form-alterar-item").submit();
    });

    $('#bt-frete').click(function(){
        if($("#cep_destino").val() == '' || $("#cep_destino").val() == null) {
            alert("Digite um cep de destino.");
            return false;
        }
    });

    /* Imprimir Pedido */

    $("#bt-imprimir").click(function(){
        $('#basic-modal-content').modal();
        $('#form-impressao').submit();
    });

    /* Efetuar Pagamento */

    $('#bt-efetuar-pagamento').click(function(){
        var forma_de_pagamento = $('#div-forma-pagamento').text();
        efetuar_pagamento(forma_de_pagamento);
    });

    /* Exibir detalhes de pedido */

    $('#[id$=-bt-exibir-pedido]').each(function() {
        $(this).click(function(){
            if($("#"+this.id).attr("class") == "bt-exibir-pedido") {
                $("#"+this.id).attr("class", "bt-ocultar-pedido");
            } else {
                $("#"+this.id).attr("class", "bt-exibir-pedido");
            }
            $("#detalhes-pedido-"+parseInt(this.id).toString()).toggle();
        });
    });
});
