/* Standard client-side browser sniffer.
 * See https://developer.mozilla.org/en/Browser_Detection_and_Cross_Browser_Support
 * See also http://www.useragentstring.com
 */
WebBrowser = {
    /** Get user agent. */
    _getAgent: function() {
        return navigator.userAgent.toLowerCase();
    },

    /** Get major version number.  */
    _getMajor: function() {
        return parseInt(navigator.appVersion);
    },

    /** Get minor version number.  */
    _getMinor: function() {
        return parseFloat(navigator.appVersion);
    },

    // Platform tests.

    _isLinux: function() {
        var agent = this._getAgent();
        return (agent.indexOf("inux") != -1);
    },

    _isMac: function() {
        var agent = this._getAgent();
        return (agent.indexOf("macintosh") != -1);
    },

    _isSun: function() {
        var agent = this._getAgent();
        return (agent.indexOf("sunos") != -1);
    },

    _isWin: function() {
        var agent = this._getAgent();
        return (agent.indexOf("win") != -1 || agent.indexOf("16bit") != -1);
    },

    getPlatform: function() {
        if (this._isLinux()) return "linux";
        if (this._isMac()) return "mac";
        if (this._isSun()) return "sun";
        if (this._isWin()) return "win";
        return null;
    },

    // Firefox/Mozilla tests.

    _isFirefox: function() {
        var agent = this._getAgent();
        return (agent.indexOf("firefox") != -1);
    },

    _isFirefox1: function() {
        var agent = this._getAgent();
        return (this._isFirefox()
            && this._getMajor() == 5
            && agent.indexOf("firefox/1.") != -1);
    },

    _isFirefox2: function() {
        var agent = this._getAgent();
        return (this._isFirefox()
            && this._getMajor() == 5
            && agent.indexOf("firefox/2.") != -1);
    },

    _isFirefox3: function() {
        var agent = this._getAgent();
        return (this._isFirefox()
            && this._getMajor() == 5
            && agent.indexOf("firefox/3.") != -1);
    },

    _isGecko: function() {
        // Note: Safari is based on WebKit.
        var agent = this._getAgent();
        return (agent.indexOf("gecko") != -1 && agent.indexOf("safari") == -1);
    },

    _isMozilla: function() {
        var agent = this._getAgent();
        return (agent.indexOf("gecko") != -1 && agent.indexOf("firefox") == -1
            && agent.indexOf("safari") == -1);
    },

    // Internet Explorer tests.

    _isIe: function() {
        var agent = this._getAgent();
        return (agent.indexOf("msie") != -1 && agent.indexOf("opera") == -1);
    },

    _isIe3: function() {
        return (this._isIe()
            && this._getMajor() < 4);
    },

    _isIe4: function() {
        var agent = this._getAgent();
        return (this._isIe()
            && this._getMajor() == 4
            && agent.indexOf("msie 4") != -1);
    },

    _isIe4up: function() {
        return (this._isIe()
            && this._getMajor() >= 4);
    },
 
    _isIe5: function() {
        var agent = this._getAgent();
        return (this._isIe()
            && this._getMajor() == 4
            && agent.indexOf("msie 5.0") != -1);
    },

    _isIe5_5: function() {
        var agent = this._getAgent();
        return (this._isIe()
            && this._getMajor() == 4
            && agent.indexOf("msie 5.5") != -1);
    },

    _isIe5up: function() {
        return (this._isIe() 
            && !this._isIe3()
            && !this._isIe4());
    },

    _isIe5_5up: function() {
        return (this._isIe()
            && !this._isIe3()
            && !this._isIe4()
            && !this._isIe5());
    },

    _isIe6: function() {
        var agent = this._getAgent();
        return (this._isIe()
            && this._getMajor() == 4
            && agent.indexOf("msie 6.") != -1);
    },

    _isIe6up: function() {
        return (this._isIe()
            && !this._isIe3()
            && !this._isIe4()
            && !this._isIe5()
            && !this._isIe5_5());
    },

    _isIe7: function() {
        var agent = this._getAgent();
        return (this._isIe()
            && this._getMajor() == 4
            && agent.indexOf("msie 7.") != -1);
    },

    _isIe7up: function() {
        return (this._isIe()
            && !this._isIe3()
            && !this._isIe4()
            && !this._isIe5()
            && !this._isIe5_5()
            && !this._isIe6());
    },

    _isIe8: function() {
        var agent = this._getAgent();
        return (this._isIe()
            && this._getMajor() == 4
            && agent.indexOf("msie 8.") != -1);
    },

    _isIe8up: function() {
        return (this._isIe()
            && !this._isIe3()
            && !this._isIe4()
            && !this._isIe5()
            && !this._isIe5_5()
            && !this._isIe6()
            && !this._isIe7());
    },

    // Navigator tests.

    _isNav: function() {
        // Note: Opera and WebTV spoof Navigator.
        var agent = this._getAgent();
        return (agent.indexOf("mozilla") != -1
            && agent.indexOf("spoofer") == -1
            && agent.indexOf("compatible") == -1);
    },

    _isNavOnly: function() {
        var agent = this._getAgent();
        return (this._isNav()
            && agent.indexOf(";nav") != -1
            || agent.indexOf("; nav") != -1);
    },

    _isNav4: function() {
        return (this._isNav()
            && this._getMajor() == 4);
    },

    _isNav4up: function() {
        return (this._isNav()
            && this._getMajor() >= 4);
    },

    _isNav6: function() {
        return (this._isNav()
            && this._getMajor() == 5);
    },

    _isNav6up: function() {
        return (this._isNav()
            && this._getMajor() >= 5);
    },

    // Safari tests.

    _isSafari: function() {
        var agent = this._getAgent();
        return (agent.indexOf("safari") != -1);
    },

    _isSafari3: function() {
        var agent = this._getAgent();
        return (this._isSafari()
            && this._getMajor() == 5
            && agent.indexOf("version/3.") != -1);
    },

    _isSafari4: function() {
        var agent = this._getAgent();
        return (this._isSafari()
            && this._getMajor() == 5
            && agent.indexOf("version/4.") != -1);
    },

    getBrowser: function() {
        var ret = "";
        if (this._isFirefox()) ret += "firefox ";
        if (this._isFirefox1()) ret += "firefox1 ";
        if (this._isFirefox2()) ret += "firefox2 ";
        if (this._isFirefox3()) ret += "firefox3 ";
        if (this._isSafari()) ret += "safari ";
        if (this._isSafari3()) ret += "safari3 ";
        if (this._isSafari4()) ret += "safari4 ";
        if (this._isGecko()) ret += "gecko ";
        if (this._isMozilla()) ret += "mozilla ";
        if (this._isIe()) ret += "ie ";
        if (this._isIe3()) ret += "ie3 ";
        if (this._isIe4()) ret += "ie4 ";
        if (this._isIe5()) ret += "ie5 ";
        if (this._isIe5_5()) ret += "ie55 ";
        if (this._isIe6()) ret += "ie6 ";
        if (this._isIe7()) ret += "ie7 ";
        if (this._isIe8()) ret += "ie8 ";
        if (this._isNavOnly() && this._isNav4()) ret += "nav4 ";
        if (this._isNavOnly() && this._isNav6()) ret += "nav6 ";
        return ret;
    }

};
