UCLU.homepage = {
    modules:        null,
    modulesChanged: false,

    setup: function() {
        YAHOO.util.Event.onAvailable('module-list', function() {
            var divs = YAHOO.util.Dom.getElementsByClassName('module', 'div', 'module-list');
        
            var ms = [];
        
            for (var i=0; i<divs.length; i++)
                ms.push(new UCLUHomepageModule(divs[i]));
        
            var modules = new KitDragDropList('module-list', { items: ms });

            modules.onInsertItem =
                modules.onRemoveItem =
                modules.onMoveItem = function() {
                    UCLU.homepage.modulesChanged = true;
                    return true;
                };
        
            this.modules = modules;
        
            UCLU.util.switchControls('module-edit-controls');
        }, this, true);
    
        YAHOO.util.Event.addListener('get-more-stuff-link', 'click', function() {
            window.location.pathname = '/homepage-module-browser.php';
        });
    
        YAHOO.util.Event.addListener('module-edit-link', 'click', this.editModules, this, true);
        YAHOO.util.Event.addListener('module-done-link', 'click', this.doneEditing, this, true);
    },

    editModules: function() {
        for (var i=0; i<this.modules.items.length; i++)
            this.modules.items[i].collapse();
    
        this.modules.activate();
        UCLU.util.switchControls('module-while-editing');
    },

    doneEditing: function() {
        this.modules.deactivate();

        if (!this.modulesChanged) {
            done();
        } else {
            var names = [];

            for (var i=0; i<this.modules.items.length; i++)
                names.push(this.modules.items[i].moduleName);

            UCLU.util.switchControls('saving-modules');
            
            UCLU.util.hide('module-list');
    
            UCLU.myStuff.save(names, {
                success: done,
            
                failure: function(o, info) {
                    community.reportError(o, info);
                    UCLU.homepage.editModules();
                }
            });
        }

        function done() {
            UCLU.util.switchControls('module-edit-controls');
            UCLU.homepage.modulesChanged = false;
        
            for (var i=0; i<UCLU.homepage.modules.items.length; i++)
                UCLU.homepage.modules.items[i].expand();
            
            UCLU.util.show('module-list');
            
            UCLU.util.redraw('middle');
        }
    },

    removeModule: function(name) {
        for (var i=0; i<this.modules.items.length; i++)
            if (this.modules.items[i].moduleName == name)
                this.modules.removeItem(i);
    }
};

function UCLUHomepageModule(div) {
    this.init(div.id, {visible:true});

    this.moduleName = div.getAttribute('module-path');

    this.contentDiv   = YAHOO.util.Dom.getElementsByClassName('module-html',          'div',    this.body)[0]
    this.deleteButton = YAHOO.util.Dom.getElementsByClassName('module-delete-button', 'button', this.body)[0]

    YAHOO.util.Event.addListener(this.deleteButton, 'click', function(e) {
        YAHOO.util.Event.stopPropagation(e);
        
        if (!confirm('Are you sure you want to remove this module?')) return;
    
        UCLU.homepage.removeModule(this.moduleName);
    }, this, true);
}

UCLUHomepageModule.prototype = new YAHOO.widget.Module();

UCLUHomepageModule.prototype.collapse = function() {
    UCLU.util.hide(this.contentDiv);
    UCLU.util.show(this.deleteButton);
    YAHOO.util.Dom.addClass(this.body, 'collapsed');
}

UCLUHomepageModule.prototype.expand = function() {
    UCLU.util.hide(this.deleteButton);
    UCLU.util.show(this.contentDiv);
    YAHOO.util.Dom.removeClass(this.body, 'collapsed');
}

/* Let's go */

if (community.getUsername())
    UCLU.homepage.setup();