﻿FeedControl = function() { };

FeedControl.prototype.PanelId = null;
FeedControl.prototype.ItemClass = null;
FeedControl.prototype.Url = null;

FeedControl.prototype.panel = null;
FeedControl.prototype.items = null;
FeedControl.prototype.minItems = null;
FeedControl.prototype.maxItems = null;
FeedControl.prototype.displayedItems = null;
FeedControl.prototype.totItems = null;

FeedControl.prototype.Init = function() {
    this.PanelId = arguments[0];
    this.ItemClass = arguments[1];
    this.Url = arguments[2];

    this.panel = $(this.PanelId);
    this.items = $.makeArray(this.panel.find(this.ItemClass));
    
    this.minItems = 3;
    this.maxItems = 5;
    this.displayedItems = this.minItems;
    this.totItems = this.items.length;
    
    var s = this;

    this.addMargin($(this.items[this.displayedItems - 1]));

    this.panel.children('.plus-btn')
            .css('display', 'block')
            .click(function() {
                if (s.displayedItems == s.totItems || s.displayedItems == s.maxItems) {
                    window.location = s.Url;
                } else {
                    s.removeMargin($(s.items[s.displayedItems - 1]));

                    $(s.items[s.displayedItems]).show();
                    s.displayedItems++;

                    s.addMargin($(s.items[s.displayedItems - 1]));
                }
            });

    this.panel.children('.minus-btn')
            .css('display', 'block')
            .click(function() {
                if (s.displayedItems > s.minItems) {
                    s.removeMargin($(s.items[s.displayedItems - 1]));

                    $(s.items[s.displayedItems - 1]).hide();
                    s.displayedItems--;

                    s.addMargin($(s.items[s.displayedItems - 1]));
                }
            });
};

FeedControl.prototype.addMargin = function(obj) {
    obj.children('p')
        .css('margin-bottom', '0px');
};

FeedControl.prototype.removeMargin = function(obj) {
    obj.children('p')
        .css('margin-bottom', '');
};
