$(document).ready(function () {
    $(".newsletter .txt").Watermark("E-mail:");
    $(".listas .busca .txt").Watermark("Nome:");

    if ($(".cep")[0]) {
        $(".cep").mask("99999-999");
    }

    if ($(".fone")[0]) {
        $(".fone").mask("(99) 9999-9999");
    }

    if ($(".data")[0]) {
        $(".data").mask("99/99/9999");
    }

    if ($(".validade-cartao")[0]) {
        $(".data").mask("99/99");
    }


    $('.box-pagamento .enviar').click(function () {
        var contador = 0;
        $('.formas-pagamento li input:radio').each(function (index) {
            if (!$(this).attr('checked')) {
                contador++;
            }
        });

        if (contador == 3) {
            alert("Selecione o cart\u00E3o de cr\u00E9dido.");
            return false;
        }
    })

    $('.paginas .centro a').click(function () {
        $(this).parent().find('a').removeClass('bold');
        $(this).addClass('bold');
    })



    $('#slider').nivoSlider({
        effect: 'fade', //Specify sets like: 'fold,fade,sliceDown'      
        animSpeed: 500, //Slide transition speed
        pauseTime: 3000,
        directionNavHide: false, //Only show on hover        
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav: true, //Use left & right arrows
        pauseOnHover: true, //Stop animation while hovering
        manualAdvance: false, //Force manual transitions
        captionOpacity: 0.8 //Universal caption opacity
    });

    $(".menu-lista .abre-lista").mouseenter(function () {
        $('.menu-lista div').fadeIn();
        $(this).addClass('atv');
    })

    $(".menu-lista div").mouseleave(function () {
        $('.menu-lista div').fadeOut(function () {
            $(".menu-lista .abre-lista").removeClass('atv');
        });
    });

    $(".img-decoracao").mouseenter(function () {
        $(".img-bazar").fadeTo(300, 0.6);
    }).mouseleave(function () {
        $(".img-bazar").fadeTo(300, 1);
    });



    $(".img-bazar").mouseenter(function () {
        $(".img-decoracao").fadeTo(100, 0.6);
    }).mouseleave(function () {
        $(".img-decoracao").fadeTo(100, 1);
    });

    $('.seleciona-produtos .txt').keyup(function () {

        var valorProduto = parseFloat($(this).parent().parent().find('.preco-unitario span').html().replace('R$ ', '').replace(".", "").replace(",", "."));
        var quantidade = parseInt($(this).parent().parent().find('.txt').attr('value'));
        var quantidadeEstoque = parseInt($(this).parent().parent().find('#hddQuantidadeEstoque').attr('value'));

        var total = valorProduto * quantidade;
        //alert(calcula(total));
        //$('#total').text(phpjs.number_format(total, 2, ',', '.'));
        //alert(total);
        //alert(phpjs.number_format(total, 2, ',', '.'))

        if (quantidadeEstoque < quantidade) {
            $(this).parent().parent().find('.alerta').css('display', 'block');
        } else {
            $(this).parent().parent().find('.alerta').css('display', 'none');
        }


        $(this).parent().parent().find('.preco-total span').html('R$ ' + CurrencyFormatted.number_format(total, 2, ',', '.'));
    });

    $('.carrinho ul.lista li.qtd .txt').keyup(function () {

        var valorProduto = parseFloat($(this).parent().parent().find('.preco-unitario').html().replace('R$ ', '').replace(".", "").replace(",", "."));
        var quantidade = parseInt($(this).parent().parent().find('.txt').attr('value'));
        var quantidadeEstoque = parseInt($(this).parent().parent().find('.hddQuantidade').attr('value'));

        var total = valorProduto * quantidade;
        //alert(calcula(total));
        //$('#total').text(phpjs.number_format(total, 2, ',', '.'));
        //alert(total);
        //alert(phpjs.number_format(total, 2, ',', '.'))

        $(this).parent().parent().find('.preco-total').html('R$ ' + CurrencyFormatted.number_format(total, 2, ',', '.'));


        var valorTotal = 0;

        var valorTotal = 0;


        $('ul li.preco-total').each(function () {
            if ($(this) != undefined) {
                valorTotal = parseFloat(valorTotal) + parseFloat($(this).html().replace('R$ ', '').replace(".", "").replace(",", "."));
            }
        });

        $('.total strong').html('R$ ' + CurrencyFormatted.number_format(valorTotal, 2, ',', '.'));

    });




    $(".lista .qtd .txt").numeric();
});





var CurrencyFormatted = {
    number_format: function (number, decimals, dec_point, thousands_sep) {
        number = (number + '').replace(',', '').replace(' ', '');
        var n = !isFinite(+number) ? 0 : +number,
            prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
            sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
            s = '',
            toFixedFix = function (n, prec) {
                var k = Math.pow(10, prec);
                return '' + Math.round(n * k) / k;
            };
        // Fix for IE parseFloat(0.55).toFixed(0) = 0;
        s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
        if (s[0].length > 3) {
            s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
        }
        if ((s[1] || '').length < prec) {
            s[1] = s[1] || '';
            s[1] += new Array(prec - s[1].length + 1).join('0');
        }
        return s.join(dec);
    }
};


$('.newsletter .btn').click(function () {
    $('.retornoNewsletter').html('');
});




$('.lista-presente li a').click(function () {
    $('.lista-presente li a').removeClass('atv');
    $(this).addClass('atv');

    var div = $(this);

    $('#hddTipoBusca').attr('value', $(this).parent().attr('id'));
    //alert($('#hddTipoBusca').attr('value'));


    $('.form-busca').fadeOut('fast', function () {
        var offset = div.offset();

        $('.form-busca').css('top', offset.top - 47);
        $('.form-busca').css('left', offset.left - 50);
        $('.form-busca').fadeIn();

    });
    
    $(".form-busca .txt").Watermark("Nome:");


});




