
(function ($) {
    $.category = {
        showAllCategories: function (e) {
            $('.todascategorias')
                .slideDown('fast');
            $('.todas_categorias a strong').addClass('aberto'); 
            $(document).click($.category.hideAllCategories);
            $('.todas_categorias').unbind('click', $.category.showAllCategories);
            $('.todas_categorias').click($.category.hideAllCategories);
            e.stopPropagation();
            e.preventDefault();
        },
        hideAllCategories: function (e) {
            $('.todascategorias')
                .slideUp('fast');
            $('.todas_categorias a strong').removeClass('aberto'); 
            $(document).unbind('click', $.category.hideAllCategories);
            $('.todas_categorias').unbind('click', $.category.hideAllCategories);
            $('.todas_categorias').click($.category.showAllCategories);
            e.stopPropagation();
        },
        changeView: function (type, elemento){
            var linhadivisao = '<div class="linhadivisao"><img src="/layout/padrao/img/pixel.gif" alt="" /></div>';
            var seletor = "#conteudo .produtos";
            $(".visualizacoes a").removeClass('on').addClass('off');
            $(elemento).removeClass('off').addClass('on');
            if(!$(seletor+" #listagem").hasClass("listagem-"+type)){
                $(seletor+" #listagem")
                .slideUp('fast')
                .queue(function (){
                    $(this).removeClass();
                    $(seletor+" .produto:gt(0)").removeClass('primeiro');
                    $(this).addClass('listagem-'+type);
                    $(seletor+" .linhadivisao").each(function(){
                        $(this).remove();
                    });
                    switch (type) {
                        case "detalhes": {
                            $(seletor + " .produto .listagem-foto img").each(function () {
                                this.src = $(this).attr('imgthumbsmall');
                            });
                            $(seletor + " .produto").after(linhadivisao);
                        } break;
                        case "ladoalado": {
                            $(seletor + " .produto:nth-child(3n+1)").addClass('primeiro');
                            $(seletor + " .produto:nth-child(3n)").after(linhadivisao);
                            $(seletor + " .produto .listagem-foto img").each(function () {
                                this.src = $(this).attr('imgthumbsmall');
                            });
                            $(seletor + " .produto:last").after(linhadivisao);
                        } break;
                        case "icones": {
                            $(seletor + " .produto:nth-child(4n+1)").addClass('primeiro');
                            $(seletor + " .produto:nth-child(4n)").after(linhadivisao);
                            if (($(seletor + " .produto").length % 4) != 0) {
                                $(seletor + " .produto:last").after(linhadivisao);
                            }
                            $(seletor + " .produto .listagem-foto img").each(function () {
                                this.src = $(this).attr('imgicon');
                            });
                        } break;
                        default: {
                        } break;
                    }
                    $(this)
                        .dequeue()
                        .slideDown("fast");
                });
            }
        },
        ordernar: function(ctg) {
            $('#listaProdutos').load('/index', {
                list_mode: $('#listagem').attr('class').split('-')[1],
                page: 'misc/dynamic',
                content:'listProduct',
                with_price: 1,
                with_extra: 2,
                ctg: ctg,
                order: $('#ordem_val').val(),
                tipos: $('#classificacao_val').val(),
                checkFreeShipping: 1,
                checkAttribute: 1,
                otype: $('#ordem_val').val()+$('#classificacao_val').val(),
                limit: $('#itens_paginas_val').val(),
                templ: 'misc/dynamic_listProduct'
            });
        }
    };
})(jQuery);

$(function () {
    $('.menu .todas_categorias').click($.category.showAllCategories);
});

