/* Class: Controllers This is a namespace for all the classes that control interactive behavior on the page. It also manages event messaging so that controllers can communicate with each other. */ var Controllers = new Class({ Implements: Events, initialize: function(cssPrefix) { this.cssPrefix = cssPrefix; this.controllers = []; this.countdown = 2; window.addEvent('domready', this.onReady.bind(this)); this.addEvent('onReady', this.onReady.bind(this)); }, onReady: function() { // In order to initialize controllers safely, wait to make sure that both // the DOM is ready and that the 'onReady' custom event has been fired. // Firing 'onReady' indicates that all controllers have been added. this.countdown--; if (this.countdown == 0) { this.initializeControllers(); } }, initializeControllers: function() { // After the page loads, search for elements with particular CSS classes // that correspond to JavaScript behavior controllers. for (property in this) { if ($type(this[property]) != 'class') { continue; } $$('.' + this.cssPrefix + property).each(function(el) { try { var controller = this[property]; this.controllers.push(new controller(el, this)); } catch(e) { dbug.log(e.message); } }.bind(this)); } // Notify any listeners that all controllers are done initializing. Also, // set a top-level CSS class to allow for progressive enhancement. this.fireEvent('onInitialize'); $(document.body).addClass(this.cssPrefix + 'enabled'); } }); dbug.enable(); var MoMA = new Controllers('JS_'); MoMA.Calendar = new Class({ /* ... */ }); MoMA.Promotions = new Class({ /* ... */ }); // Plus a bunch more MoMA.fireEvent('onReady');