// global App Pointer
// this is used in templates for triggering events
// e.g. ..onclick="g_APP.plugin_editor_x.do_something_intelligen()"

var g_APP ; 

$(document).ready(function() {

    // This can (must not be) used in conjunction with a script block in a 
    // django template which defines the "main_module" var and the "other_modules"
    // for selecting a dependent setup behaviour
    // was used in devbox to either launch the "tracker" or the "projects" 
    // The current setup you can find in the "pages/base.html" template
    
    // If the "modules" passed to the constructor of the global app is empty or "*"
    // all modules will be loaded
    
    var main_module = ["page"] ; 
    var other_modules = [] ; 
    
    var modules = main_module.concat(other_modules) ; 
    
    // this IS the sandbox
    // stuff can be done here right away, but was not done so in devbox, used 
    // only to startup the engine

	g_APP = new DDWebsite(modules, function(T) {  
	    // e.g.:  
	    // alert(T.demo.say_hello()) ; 
	}) ; 
    
    // now g_APP is a valid sandbox do other stuff you want to do in "document ready"
}) ;


// The main sandbox object and implementation
// Read "Java Script Patterns" (the o'reilly book) - Sandbox - for more details

// IMPORTANT: 
// Each module lives in a separate file and each of this separate files must
// be added to compress.py

function DDWebsite() { 

    // turning arguments into an array
    var args = Array.prototype.slice.call(arguments),
        // the last argument is the callback 
        callback = args.pop(),
        // modules can be passed as an array or as individual parameters
        modules = (args[0] && typeof args[0] === "string") ? args : args[0], 
        i ; 
    
    // make sure the function is called as a constructor
    if (!(this instanceof DDWebsite)) {
        return new DDWebsite(modules, callback);
    }
    
    // add properties to `this` as needed:
    // this.a = 1;
    // this.b = 2;
    
    // now add modules to the core `this` object
    // no modules or "*" both mean "use all modules"
    if (!modules || modules === '*') {
    modules = [];
        for (i in DDWebsite.modules) {
            if (DDWebsite.modules.hasOwnProperty(i)) {
                modules.push(i);
            }
        }
    }
    
    // add default modules that are always used, adapt and uncomment the following
     if (!modules.contains("utilities")) modules.push("utilities") ;
    // if (!modules.contains("data_source")) modules.push("data_source") ;  
    // etc. 
    
    // initialize the required modules
    var modules_length = modules.length ; 
    for (i = 0; i < modules_length; i += 1) {
        DDWebsite.modules[modules[i]](this) ;
    }

    // call init on every module
    for (i = 0; i < modules_length; i += 1) {
        this[modules[i]].init() ; 
    }
    // call the callback
    callback(this) ;    
    
} ; 

    // important !
DDWebsite.modules = {} ; 

    // any prototype properties as needed
DDWebsite.prototype = {
    name: "devdev Website",
    version: "0.1",
    getName: function () {
        return this.name;
    }
} ;



// this is optional, used in devbox, can be removed if not used
// would probably be better in another file, who cares

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}





