var $=jQuery;
var jlHideAndShow = function() {
    var
        _alphaOmega = function(elements, noOfItemsPerRow) {
            var count = 0;
            elements.each(function() {
                count++;
                if(count % noOfItemsPerRow == 1) {
                    $(this).addClass('alpha');
                }
                if(count % noOfItemsPerRow == 0) {
                    $(this).addClass('omega');
                }
            });

        },
        _showItems = function(elements) {
            var showCount = 0,
                noOfItemsPerRow = $('#post-list-items-per-row').html();

            elements.each(function() {
                showCount++;
                $(this).removeClass('alpha');
                $(this).removeClass('omega');
                $(this).fadeIn('slow', function() {
                    if(showCount == elements.length) {
                        _alphaOmega(elements, noOfItemsPerRow);
                    }
                });
            });
        };

    return {
        show: function(allcategory, category) {
            var itemsToShow = $('.'+category),
                noOfItemsToHide = $('.post-list .item').not(itemsToShow).length,
                hideCount = 0;

            $('.post-list .item').not(itemsToShow).each(function() { // fade out items one by one
                $(this).fadeOut('slow', function() {
                    hideCount++;
                    $(this).removeClass('alpha');
                    $(this).removeClass('omega');
                    // fix alpha omega on last fadeOut
                    if(hideCount == noOfItemsToHide) {
                        _showItems(itemsToShow);
                    }
                });
            });
        },

        showAll: function() {
            _showItems($('.post-list .item'));
        }
    };
}();

$(document).ready(function($) {

    $('#'+'jl-hide-and-show-controls a.show-all').click(function(event) {
        event.preventDefault();
        jlHideAndShow.showAll();
    });
    $('#'+'jl-hide-and-show-controls a.show-cat').click(function(event) {
        event.preventDefault();
        var cat=$(this).attr('href').replace('#', '');
        jlHideAndShow.show('projekt', cat);
    });
});

