﻿var amg_popupStatus = 0;
var amg_popupStatus2 = 0;
var amg_popupStatus3 = 0;
function amg_loadPopup() {
    if (amg_popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#enviar_amigo").fadeIn("slow");
        amg_popupStatus = 1;
    }
}
function amg_loadPopup2() {
    if (amg_popupStatus2 == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#recuperarsenha").fadeIn("slow");
        amg_popupStatus2 = 1;
    }
}
function amg_loadPopup3() {
    if (amg_popupStatus3 == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#regulamentos").fadeIn("slow");
        amg_popupStatus3 = 1;
    }
}


function amg_disablePopup() {
    if (amg_popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#enviar_amigo").fadeOut("slow");
        amg_popupStatus = 0;
    }
}

function amg_disablePopup3() {
    if (amg_popupStatus3 == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#regulamentos").fadeOut("slow");
        amg_popupStatus3 = 0;
    }
}

function amg_disablePopup2() {
    if (amg_popupStatus2 == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#recuperarsenha").fadeOut("slow");
        amg_popupStatus2 = 0;
    }
}
function amg_centerPopup() {
    var amg_windowWidth = document.documentElement.clientWidth;
    var amg_windowHeight = document.documentElement.clientHeight;
    var amg_popupHeight = $("#enviar_amigo").height();
    var amg_popupWidth = $("#enviar_amigo").width();
    $("#enviar_amigo").css({
        "position": "absolute",
        "top": amg_windowHeight / 2 - amg_popupHeight / 2,
        "left": amg_windowWidth / 2 - amg_popupWidth / 2
    });
    $("#backgroundPopup").css({
        "height": amg_windowHeight
    });
}
function amg_centerPopup2() {
    var amg_windowWidth2 = document.documentElement.clientWidth;
    var amg_windowHeight2 = document.documentElement.clientHeight;
    var amg_popupHeight2 = $("#recuperarsenha").height();
    var amg_popupWidth2 = $("#recuperarsenha").width();
    $("#recuperarsenha").css({
        "position": "absolute",
        "top": amg_windowHeight2 / 2 - amg_popupHeight2 / 2,
        "left": amg_windowWidth2 / 2 - amg_popupWidth2 / 2
    });
    $("#backgroundPopup").css({
        "height": amg_windowHeight2
    });
}

function amg_centerPopup3() {
    var amg_windowWidth3 = document.documentElement.clientWidth;
    var amg_windowHeight3 = document.documentElement.clientHeight;
    var amg_popupHeight3 = $("#regulamentos").height();
    var amg_popupWidth3 = $("#regulamentos").width();
    $("#regulamentos").css({
        "position": "absolute",
        "top": amg_windowHeight3 / 2 - amg_popupHeight3 / 2,
        "left": amg_windowWidth3 / 2 - amg_popupWidth3 / 2
    });
    $("#backgroundPopup").css({
        "height": amg_windowHeight3
    });
}
function GetCarrinho() {
    $('#carrinho_menu').slideUp("fast");
    $.post('/Carrinho/GetCarrinho', '', function(data) { $('#carrinho').html(data); }, "html");
    $('#carrinho_menu').slideDown("fast");
    document.carrinhoAbertoAdd = true;
    $("#btnCarrinho").show();
    $("#btnVerCarrinho").show();
    window.clearInterval(document.timerID);
    document.timerID = window.setInterval("trocaCarrinho()", 5000);
    $("#btnCarrinhoWait").hide();
}


function showCarrinho() {
    if ((document.carrinhoAberto || document.carrinhoAbertoOver || document.carrinhoAbertoAdd) && $('#carrinho_menu:hidden').length > 0 && $('#carrinho').html() != '0 itens : R$ 0,00') {
        $('#carrinho_menu:hidden').slideDown('fast');
    }
    if (!document.carrinhoAberto && !document.carrinhoAbertoOver && !document.carrinhoAbertoAdd && $('#carrinho_menu:hidden').length == 0) {
        $('#carrinho_menu').slideUp('fast');
    }
}
var qtd = $("#TamanhoID").attr('qtd');
function trocaCarrinho() {
    document.carrinhoAbertoAdd = false;
    $.get("/Carrinho/CarrinhoAjax/", function(data) {
        $('#carrinho_menu').html(data);
    });
    window.clearInterval(document.timerID);
}
function validaForm() {
    qtd = $("#TamanhoID").attr('qtd');
    if (qtd == undefined) qtd = $("#TamanhoID option:selected").attr("qtd");
    if (qtd == undefined) {

        alert('Escolha um tamanho');
        return false;
    }
    if (qtd == "0") {
        alert('Infelizmente não temos esse tamanho em estoque');
        return false;
    }

    if (qtd < Number($("#Quantidade").val())) {

        alert('Infelizmente só temos em nosso estoque ' + qtd + ' unidade(s) desse tamanho');
        $("#Quantidade").val(qtd);
        return false;

    }
    $("#btnCarrinho").hide();
    $("#btnCarrinhoWait").show();
    return true;
}

$(document).ready(function() {

    $(".amigo").click(function() {
        amg_centerPopup();
        amg_loadPopup();
        return false;
    });
    self.setInterval('showCarrinho()', 50)
    $('#carrinho_menu').mouseover(function() {
        document.carrinhoAbertoOver = true;
    }).mouseout(function() {
        document.carrinhoAbertoOver = false;
    });

    $('#carrinho_box').mouseover(function() {
        if ($('#carrinho_menu:hidden').length > 0) {
            document.carrinhoAberto = true;
        }
    }).mouseout(function() {
        document.carrinhoAberto = false;
    });

    var options = {
        target: '#carrinho_menu',
        beforeSubmit: validaForm,
        success: GetCarrinho,
        url: '/Carrinho/AddAjax/'
    };

    $.post('/Carrinho/GetCarrinho', '', function(data) { $('#carrinho').html(data); }, "html");
    $.get("/Carrinho/CarrinhoAjax/", function(data) {
        $('#carrinho_menu').html(data);
    });
    $('#formCompra').ajaxForm(options);
    
    qtd = $("#TamanhoID").attr("qtd")
    $("#TamanhoID").change(function() {

        qtd = $("#TamanhoID option:selected").attr("qtd");
    })
            .trigger('change');
    $("#email").focus(function() {
        if ($(this).val() == "Seu e-mail") {
            $(this).val("");
        }
    });

    $("#email").blur(function() {
        if ($(this).val() == "") {
            $(this).val("Seu e-mail");
        }
    });

    $(".regulamento").click(function() {
        amg_centerPopup3();
        amg_loadPopup3();
        return false;
    });

    $(".esqueceusenha").click(function() {
        amg_centerPopup2();
        amg_loadPopup2();
        return false;
    });

    $(".cancelaAmigo").click(function() {
        amg_disablePopup();
        return false;
    });
    $(".cancelaSenha").click(function() {
        amg_disablePopup2();
        return false;
    });
    $("#backgroundPopup").click(function() {
        if (amg_popupStatus2 == 1)
            amg_disablePopup2();
        if (amg_popupStatus == 1)
            amg_disablePopup();
        if (amg_popupStatus3 == 1)
            amg_disablePopup3();
    });
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && amg_popupStatus == 1) {
            amg_disablePopup();
        }
        if (e.keyCode == 27 && amg_popupStatus2 == 1) {
            amg_disablePopup2();
        }
        if (e.keyCode == 27 && amg_popupStatus3 == 1) {
            amg_disablePopup3();
        }

    });
    var originalSizes = new Array();
    $('#divcaixa_camisas').jScrollHorizontalPane({ arrowSize: 20, showArrows: true, scrollbarHeight: 25, dragMinWidth: 136, dragMaxWidth: 136, resize: false });
    $('a[rel*=lightbox]').lightBox();
    $('#tamanho').lightBox();

    $("#submitvip").click(function() {

        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailToVal = $("#emailvip").val();
        if (emailToVal == '') {
            alert("Um e-mail deve ser informado");
            hasError = true;
        } else if (!emailReg.test(emailToVal)) {
            alert("Um e-mail válido deve ser informado");
            hasError = true;
        }
        if (!hasError) {
            var Mailing = { Email: emailToVal };

            $.post("/Mailing/Add", Mailing, function(data) {
                switch (data.status) {
                    case "1":
                        alert("E-mail cadastrado com sucesso!");
                        break;
                    case "0":
                        alert("Ocorreu um erro no cadastro do e-mail");
                        break;
                }

            }, "json");
        }
        return false;

    });


    $(".enviaSenha").click(function() {

        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailToVal = $("#emailRecupera").val();
        if (emailToVal == '') {
            alert("Um e-mail deve ser informado");
            hasError = true;
        } else if (!emailReg.test(emailToVal)) {
            alert("Um e-mail válido deve ser informado");
            hasError = true;
        }
        if (!hasError) {
            var Mailing = { Email: emailToVal };

            $.post("/MinhaConta/RecuperarSenha/", Mailing, function(data) {
                alert(data.Mensagem);
                if (amg_popupStatus2 == 1)
                    amg_disablePopup2();
            }, "json");
        }
        return false;

    });

    $("btnFecharPedido").click(function() {
        $(this).val("href", $(this).val("hfer") + $("CodCupom"));
        alert($(this).val("href"));
    });

});
