/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.app.App = function(cfg){
    Ext.apply(this, cfg);
    this.addEvents({
        'ready' : true,
        'beforeunload' : true
    });

    Ext.onReady(this.initApp, this, {delay: 2000});
};

Ext.extend(Ext.app.App, Ext.util.Observable, {
    isReady: false,
    startMenu: null,
    modules: null,

    getStartConfig: function() {

    },

    initApp: function() {
        //Idioma
        var s = document.createElement("script");
        s.type = 'text/javascript';
        s.src = "scripts/ext-lang-" + desktopLang + ".js";
        document.getElementsByTagName("head")[0].appendChild(s);

        this.startConfig = this.startConfig || this.getStartConfig();
        this.desktop = new Ext.Desktop(this);
        this.launcher = this.desktop.taskbar.startMenu;
        this.modules = this.getModules();
        if (this.modules)
            this.initModules(this.modules);
        this.init();

        Ext.EventManager.on(window, 'beforeunload', this.onUnload, this);
        this.fireEvent('ready', this);
        this.isReady = true;

        window.setTimeout("fadeSplash();", 2000);
    },

    getModules: Ext.emptyFn,
    init: Ext.emptyFn,

    initModules: function(ms) {
        for (var i = 0, len = ms.length; i < len; i++) {
            var m = ms[i];
            this.launcher.add(m.launcher);
            m.app = this;
        }
    },

    getModule: function(name) {
        var ms = this.modules;
        for (var i = 0, len = ms.length; i < len; i++) {
            if (ms[i].id == name || ms[i].appType == name) {
                return ms[i];
            }
        }
        return '';
    },

    onReady: function(fn, scope) {
        if (!this.isReady) {
            this.on('ready', fn, scope);
        } else {
            fn.call(scope, this);
        }
    },

    getDesktop: function() {
        return this.desktop;
    },

    onUnload: function(e) {
        if (this.fireEvent('beforeunload', this) === false) {
            e.stopEvent();
        }
    }
});

Ext.app.Module = function(config){
    Ext.apply(this, config);
    Ext.app.Module.superclass.constructor.call(this);
    this.init();
}

Ext.extend(Ext.app.Module, Ext.util.Observable, {
    init : Ext.emptyFn
});

function fadeSplash() {
    soundManager.play('start');
    var timer = 0;
    for (i = 100; i >= 0; i--)
        setTimeout("changeOpac(" + i + ")", (timer++ * 5));
    setTimeout("document.getElementById('splash').style.display='none';welcome();", (timer * 5));
}
function changeOpac(opacity) {
    var object = document.getElementById('splash').style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
function playNewWindow() {
    try {
        soundManager.play('newwindow');
    }
    catch (e) { }
}