﻿$namespace("sys");

// provide compatibility to WScript, etc.
if(typeof(window) == "undefined") window = new Object();

function $namespace(_namespace, shortcut)
{
    var i, parts;
    
    var root = window;
    
    // check namespace registration
    var oriNs = root;
    parts = _namespace.split('.');
    for (i = 0; i < parts.length; i++) 
    {
        if (!oriNs[parts[i]]) oriNs[parts[i]] = {};
        oriNs = oriNs[parts[i]];
    }
    
    // register namespace shortcut if necessary
    var scNs = root;
    if(typeof(shortcut) == 'string' && null == Object.eval(shortcut))
    {
        var pre = '', end = '';
        var di = shortcut.lastIndexOf('.');
        if(-1 == di) { pre = sys.env.getScriptRootName(); end = shortcut; }
        else if(0 == di || di == shortcut.length - 1) {
            throw new /*Argument*/Exception("shortcut");
        } else { 
            pre = shortcut.substring(0, di); 
            end = shortcut.substring(di + 1);
        }
        scNs = $namespace(pre);
        if(!scNs[end]) scNs[end] = oriNs;
    }
    
    return oriNs;
}

sys.env = new function()
{
    // gets the environment script root object
    this.getScriptRoot = function() { return !sys.scriptRoot ? window : sys.scriptRoot; }

    // gets the name of script root object
    this.getScriptRootName = function() { return "window"; }
    
    // defines the user agent in the environment
    // see http://en.wikipedia.org/wiki/User_agent
    this.userAgent = new function()
    {
        var m_ua = navigator.userAgent.toLowerCase();
        
        // browser
        this.isOldIE = function(){return m_ua.indexOf("microsoft internet explorer") != -1;}
        this.isNewIE = function() { return m_ua.indexOf("msie") != -1; }
        this.isIE = function() {return this.isOldIE() || this.isNewIE();}
        this.isMozillaCompatible = function() {return m_ua.indexOf("gecko") != -1;}
        this.isMozilla = function() {return this.isMozillaCompatible();}
        this.isFirefox = function() {return m_ua.indexOf("firefox") != -1;}
        this.isOpera = function() {return m_ua.indexOf("opera") != -1;}
        this.isSafari = function() {return m_ua.indexOf("safari") != -1;}
        this.isNS = function() {return !this.isIE() && !this.isMozillaCompatible();}
        this.isKonqueror = function() { return m_ua.indexOf("konqueror") != -1;}
        
        var _$ = function(_s) { return m_ua.indexOf(_s) != -1; }
        
        // platform
        this.isWindows = function() {return _$("windows");}
        this.isWin95 = function() {return _$("windows 95");}
        this.isWin98 = function() {return _$("windows 98");}
        this.isWinMe = function() {return _$("windows 98; win9x 4.90");}
        this.isWinNT = function() {return _$("windows nt");}
        this.isWin2000 = function() {return _$("windows nt 5.0");}
        this.isWinXp = function() {return _$("windows nt 5.1");}
        this.isWinServer2003 = function() {return _$("windows nt 5.2");}
        this.isWinCE = function() {return _$("windows ce");}
        this.isVista = function() {return _$("windows nt 6.0");}
        this.isMac = function() {return _$("mac");}
        this.isBSD = function() {return _$("bsd");}
        
        // language
        this.getLanguage = function() 
        {
            if(typeof(navigator.userLanguage) != 'undefined') return navigator.userLanguage;
            if(typeof(navigator.language) != 'undefined') return navigator.language;
            if(typeof(navigator.browserLanguage) != 'undefined') return navigator.browserLanguage;
            return "xx-xx";
        }
        
        this.isCookieEnabled = function() { return navigator.cookieEnabled; }
        this.getAppName = function() { return navigator.appName; }
        this.getAppCodeName = function() { return navigator.appCodeName; }
        this.getVersion = function() 
        { 
            var i;
            if(this.isIE())
            {
                if(this.isOldIE()) { 
                    i = m_ua.indexOf("microsoft internet explorer") + ("microsoft internet explorer").length;
                    return parseFloat(m_ua.substring(
                        i + 1,
                        m_ua.indexOf("(", i + 1) - 1
                        ));
                } else {
                    i = m_ua.indexOf("msie") + ("msie").length;
                    return parseFloat(m_ua.substring(
                        i + 1, 
                        m_ua.indexOf(";", i + 1) - 1
                        ));
                }   
            }
            return parseFloat(navigator.appVersion); 
        }
    };
};

sys.Utils = new function()
{
    this.destroyObject = function(obj,bRecursive,bDeleteFunction)
	{
		for(var p in obj) 
		{
			if(!bDeleteFunction && typeof(p) == 'function') continue;
			if(bRecursive && typeof(obj[p]) == 'object') this.destroyObject(obj[p]); 
			delete obj[p];
		}
	}
}

sys.Event = function(name)
{
    this.name = name;
    this._handlers = [];
}

sys.Event.prototype = 
{
    attach : function(handler) 
    { 
        if(handler && !this._handlers.contains(handler)) 
        {
            this._handlers.push(handler); 
        }
    }, 
    
    detach : function(handler) 
    { 
        this._handlers.remove(handler); 
    }, 
    
    clear : function() 
    { 
        this._handlers.clear(); 
    }, 
    
    fire : function(ev, async) 
    { 
        function _fire(handler)
        {
            try { handler(ev); }
            catch(e) {}
        }
        
        this._handlers.forEach
        (
            function(handler)
            {
                if(async) 
                {
                    setTimeout(
                        function() { _fire(handler); }, 
                        1
                        );
                }
                else
                {
                    _fire(handler);
                }
            }
        );
    }, 
    
    dispose : function()
    {
        this.clear();
    }
};