dabei.nowOnlineWall = {

    interval: null,
    lastSlot: null,
    items: new dabei.stack,
    activeItems: new dabei.stack,

    init: function(itemArray) {
        var obj = this;

        // load items
        this.items.load(itemArray);

        // reset stack
        this.activeItems.reset();

        // pin images to current position and push into stack
        jQuery('.homeNowOnlineItem').each(function() {
            var el = jQuery(this);
            el.css({
                top: el.position().top,
                left: el.position().left
            });
            obj.activeItems.add(el.attr('href').toLowerCase());
            obj.items.add({
                'url' : el.attr('href'),
                'image' : jQuery('img', el).attr('src')
            });
        }).css('position', 'absolute');

        if (jQuery('.homeNowOnlineItem').length > 10) {
            this.interval = window.setInterval('dabei.nowOnlineWall.shuffle()', 1500);
        }
    },

    shuffle: function() {
        var slot = this.getRandomSlot();
        var item = this.getRandomItem();

        var newItem = jQuery('<a>')
            .attr('class', 'homeNowOnlineItem')
            .attr('href', item.url);

        newItem.hide();
        var newItemImage = jQuery('<img />').attr('src', item.image);
        newItem.append(newItemImage);

        jQuery('#homeNowOnlineList').append(newItem);

        newItem.css({
                top: slot.position().top,
                left: slot.position().left,
                position: 'absolute'
        });

        this.activeItems.del(slot.attr('href').toLowerCase());
        this.activeItems.add(item.url.toLowerCase());

        newItemImage.load(function() {
            newItem.fadeIn(500);
            slot.remove();
        });
    },

    getRandomItem: function() {
        var item = this.items.random();
        if (this.activeItems.has(item.url.toLowerCase())) {
            return this.getRandomItem();
        }
        return item;
    },

    getRandomSlot: function() {
        var slotIdx = Math.floor((jQuery('.homeNowOnlineItem').length) * (Math.random() % 1)) + 1;
        if (this.lastSlot == slotIdx) {
            return this.getRandomSlot();
        }
        this.lastSlot = slotIdx;
        return jQuery('.homeNowOnlineItem:nth-child('+slotIdx+')');
    }
};
