var App = function(cfg){
    Ext.apply(this, cfg);
    this.addEvents({
        'ready' : true,
        'beforeunload' : true
    });

    Ext.onReady(this.initApp, this);
};

Ext.extend(App, Ext.util.Observable, {

    isReady: false,
    modules: {},

    initApp : function(){

        //Modules must add themself to application
        this.init();
		
		this.initModules();

        Ext.EventManager.on(window, 'beforeunload', this.onUnload, this);
		this.fireEvent('ready', this);
        this.isReady = true;
		
    },

    getModules : function(){
		return this.modules;
	},
	
    init : Ext.emptyFn,

    initModules : Ext.emptyFn,
	
    start : Ext.emptyFn,
	
	addModule : function(module) {
		this.modules[module.id] = module;
	},
	
    getModule : function(id){
    	var ms = this.modules;
		
    	if(ms[id]) return ms[id];
		
        return false;
    },
	
    onReady : function(fn, scope){
        if(!this.isReady){
            this.on('ready', fn, scope);
        }else{
            fn.call(scope, this);
        }
    },
    
	onUnload : function(e){
        if(this.fireEvent('beforeunload', this) === false){
            e.stopEvent();
			this._desctruct();
        }
    },	
	
	_desctruct : function(){
		
    }
});