﻿$(function() {

    $('#txtNomeNews, #txtEmailNews').placeholder();
    
    if ($('#txtBusca').length > 0) {
        $('#txtBusca').bind('keypress', function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                doSearch();
            }
        });
    }
});
function doSearch() {
    location.href = 'busca.aspx?q=' + $('#txtBusca').val();
}
function doFilter(type) {
    var url = location.href;
    url = url.replace('#', '');
    url = url.replace('&ordem=populares', '');
    url = url.replace('&ordem=alfabetica', '');
    url = url.replace('&ordem=preco', '');
    
    if (url.indexOf("?") != -1) {
        if (type == 'A') {
            location.href = url + '&ordem=alfabetica';
        }
        if (type == 'P') {
            location.href = url + '&ordem=populares';
        }
        if (type == 'M') {
            location.href = url + '&ordem=preco';
        }
    }
    else {
        if (type == 'A') {
            location.href = url + '?ordem=alfabetica';
        }
        if (type == 'P') {
            location.href = url + '?ordem=populares';
        }
        if (type == 'M') {
            location.href = url + '?ordem=preco';
        }
    }
    return false;
}
function assinarNews() {
    var nome = $('#txtNomeNews').val();
    var email = $('#txtEmailNews').val();
    if (nome != "" && email != "") {
        $.get("assinar-news.aspx", { txtNomeNews: nome, txtEmailNews: email },
                function(data) {
                    if (data != "") {
                        dialog(true, 'Assinar newsletter', data);
                        return false
                    }
                    cancelloading();
                    location.href = "cart.aspx";
                });
    }
    else {
        dialog(true, 'Assinar newsletter', 'Informe seu nome e e-mail.');
    }
}
function dialog(action, titulo, content) {
    if (action) {
        $('.overlay').fadeIn('fast', function() {
            $('.dialog, .dialog-content').fadeIn('fast', function() {
                $('.dialog-content h5').html(titulo);
                $('.dialog-content p').html(content);
            });
        });
    } else {
        $('.dialog, .dialog-content').fadeOut('fast', function() {
            $('.overlay').fadeOut('fast');
        });
    }
}
