(function($) {

    // my namespace
    $.listManager = {
        
        // plugin defaults
        defaults:  {
            size: 3
        },
        
        listGetMaxSize: function(obj) {
            return $("div.item", $(obj)).size(); 
        },

        listSaveSize: function(obj) {
            var moduleId = $(obj).attr('id').replace(/_[0-9]+$/, "").replace(/^m_/, "");
            var optionKey = $(obj).attr('id').replace(/^m_[0-9]+_/, "");
            Microgroove.Dock.Manager.Instance().getModule(moduleId).setUserPref(optionKey, 'Size', $.listManager.listGetCurrentSize(obj));
            Microgroove.Dock.Manager.Instance().saveToSession();
        },
        
        listShow: function(options) {
            var opts = $.extend({}, $.listManager.defaults, options);
            return this.each(
            function() {
                $("div.item", $(this)).each(function(i) {
                    if(i < opts.size) {
                        $(this).show();
                        return;
                    }
                    $(this).hide();
                });
            });
        },

        listGetCurrentSize: function(obj) {
            return $("div.items div.item:not(:hidden)", $(obj)).size();
        },

        listIncrementSize: function(obj) {
            var currentSize = $.listManager.listGetCurrentSize(obj);
            var maxSize =  $.listManager.listGetMaxSize(obj);
            if(currentSize+1>maxSize){
                $(obj).find('a.add').css('opacity', 0.4);
                return;
            }
            $("div.items", obj).listShow({size:currentSize+1});
            $.listManager.listSaveSize(obj);
            $(obj).find('a.add,a.remove').css('opacity', 1);
        },

        listDecrementSize: function(obj) {
            var currentSize = $.listManager.listGetCurrentSize(obj);
            if(currentSize-1<=0){
            $(obj).find('a.remove').css('opacity', 0.4);
                return;
            }
            $("div.items", obj).listShow({size:currentSize-1});
            $.listManager.listSaveSize(obj);
            $(obj).find('a.remove,a.add').css('opacity', 1);
        },

        listShowAll: function(obj) {
            return $('div.items div.item', obj).each(function(i) {
                    $(this).show();
                });
        },

        listHideAll: function() {
            return this.each(function() {
                $('div.item', $(this)).each(function(i) {
                    $(this).hide();
                });
            });
        }
    }

    // public methods exposed (add to the jQuery namespace)
    $.each($.listManager, function(i) {
        $.fn[i] = this;
    });

})(jQuery);
