(function(root, $, undefined) {
    
    var CollectionManager = function(collections, collectionIndex, screenIndex, callback) {
        
        this.collections = this._generateCollections(collections, collectionIndex, screenIndex, callback);
        this.activeCollection = this.collections[collectionIndex];
        this.activeCollectionIndex = collectionIndex;
        this._generateHTML();
        
    };
    
    CollectionManager.prototype = {
        
        _generateHTML: function(){
            
            var html = $("<div class='collectionManagerContainer'></div>");
            
            var i = -1;
            var len = this.collections.length;
            
            while(++i < len) {
                html.append(this.collections[i].html);
            }
            
            this.html = html;
            
        },
        
        _generateCollections: function(data, collectionIndex, screenIndex, callback) {
            
            var i = -1,
                j = -1,
                k = -1,
                len,
                len1,
                len2,
                elem,
                elem1,
                elem2,
                hotspot,
                hotspots = [],
                screen0,
                screens = [],
                collection,
                collections = [];
            
            len = data.length;    
            
            while(++i < len) {
                
                elem = data[i];
                
                len1 = elem.screens.length;
                
                while(++j < len1) {
                    
                    elem1 = elem.screens[j];
                    
                    screen0 = new B.Screen(elem1.image, elem1.originalSize, [], i === collectionIndex && j === screenIndex, i === collectionIndex && j === screenIndex ? callback : undefined);
                    
                    len2 = elem1.hotspots.length;
                    while(++k < len2) {
                        elem2 = elem1.hotspots[k];
                        hotspots.push(new B.Hotspot(screen0, elem2.image, elem2.content, elem2.type, elem2.position));
                    }
                    
                    k = -1;
                    
                    screen0.hotspots = hotspots;
                    screens.push(screen0);
                    hotspots = [];
                }
                j = -1;
                
                collection = new B.Collection(elem.name, elem.subtitle, elem.text, screens, i === collectionIndex, screenIndex);
                
                collections.push(collection);
                screens = [];
            }
            
            return collections;
            
        },
        
        changeActiveCollection: function(name) {
            
            var newIndex = (function(self) {
                var i = -1;
                var len = self.collections.length;
                while(++i < len) {
                    if(self.collections[i].name === name) {
                        return i;
                    }
                }
            })(this);
            
            this._setActiveCollection(newIndex);
            
        },
        
        _setActiveCollection: function(index) {
            
            if(index === this.activeCollectionIndex) return;
            
            this.activeCollectionIndex = index;
            this.activeCollection.hide();
            this.activeCollection = this.collections[index];
            this.activeCollection.show();

			$(this).trigger("changeCollection");

            $(window).trigger("resize");

        }
        
    };
    
    root.B.CollectionManager = CollectionManager;
    
})(this, jQuery);
