/*
 * $Id: useragent.js 45 2009-09-27 07:19:26Z wru $
 */

function CVersion (themajor, theminor, therelease, thebuild)
{
    if (! (this instanceof CVersion))
        return (new CVersion (themajor, theminor, therelease, thebuild));
    var major   = (typeof themajor   != "undefined" ? themajor   : 0);
    var minor   = (typeof theminor   != "undefined" ? theminor   : 0);
    var release = (typeof therelease != "undefined" ? therelease : 0);
    var build   = (typeof thebuild   != "undefined" ? thebuild   : 0);

    this.GetBuild = function ()
    {
        return (build);
    }

    this.GetMajor = function ()
    {
        return (major);
    }

    this.GetMinor = function ()
    {
    
        return (minor);
    }

    this.GetRelease = function ()
    {
        return (release);
    }

}

CVersion.Compare = function (thelhs, therhs)
{
    if (thelhs.GetMajor   () != therhs.GetMajor   ()) return (thelhs.GetMajor   () < therhs.GetMajor   () ? -1 : 1);
    if (thelhs.GetMinor   () != therhs.GetMinor   ()) return (thelhs.GetMinor   () < therhs.GetMinor   () ? -1 : 1);
    if (thelhs.GetRelease () != therhs.GetRelease ()) return (thelhs.GetRelease () < therhs.GetRelease () ? -1 : 1);
    if (thelhs.GetBuild   () != therhs.GetBuild   ()) return (thelhs.GetBuild   () < therhs.GetBuild   () ? -1 : 1);
    return (0);
}

CVersion.FromString = function (theverstr)
{
    var versions = theverstr.split ('.', 4);
    while (versions.length < 4)
        versions.push ("0");
    return (new CVersion (parseInt (versions [0], 10), parseInt (versions [1], 10), parseInt (versions [2], 10), parseInt (versions [3], 10)));
}

CVersion.prototype.ToNumber = function ()
{
    return (parseFloat (this.GetMajor () + "." + this.GetMinor ()));
}

CVersion.prototype.toString = function ()
{
    return (this.GetMajor ().formatInt () + "." + this.GetMinor ().formatInt (2) + "." + this.GetRelease ().formatInt (2) + "." + this.GetBuild ().formatInt (4));
}

function CUserAgent ()
{
    var agentinfo = new Object;
    agentinfo [CUserAgent.chrome   ] = /Chrome\/\s*(\d+(\.\d+){0,3})/;
    agentinfo [CUserAgent.epiphany ] = /Epiphany\/\s*(\d+(\.\d+){0,3})/;
    agentinfo [CUserAgent.firefox  ] = /(?:Firefox|Iceweasel)\/\s*(\d+(\.\d+){0,3})/;
    agentinfo [CUserAgent.iexplore ] = /MSIE\s*(\d+(\.\d+)?)/;
    agentinfo [CUserAgent.konqueror] = /Konqueror\/\s*(\d+(\.\d+){0,3})/;
    agentinfo [CUserAgent.netscape ] = /Netscape\/\s*(\d+(\.\d+)?)/;
    agentinfo [CUserAgent.opera    ] = /Opera\/\s*(\d+(\.\d+)?)/;
    agentinfo [CUserAgent.safari   ] = /Version\/\s*(\d+(\.\d+){0,3})\s+Safari\/\s*(\d+(\.\d+){0,2})/;
    var agentname    = '';
    var agentversion = new CVersion ();
    if (typeof window.navigator.userAgent != "undefined") {
        var agent = window.navigator.userAgent;
        for (var name in agentinfo) {
            var matches = agent.match (agentinfo [name]);
            if (matches !== null && matches.length >= 2) {
                agentname = name;
                agentversion = CVersion.FromString (matches [1]);
                break;
            }
        }
    }

    this.GetName = function ()
    {
        return (agentname);
    }

    this.GetVersion = function ()
    {
        return (agentversion);
    }

}

CUserAgent.chrome    = "Chrome";
CUserAgent.epiphany  = "Epiphany";
CUserAgent.firefox   = "Firefox";
CUserAgent.iexplore  = "Internet Explorer";
CUserAgent.konqueror = "Konqueror";
CUserAgent.netscape  = "Netscape";
CUserAgent.opera     = "Opera";
CUserAgent.safari    = "Safari";
CUserAgent.instance  = null;

CUserAgent.Get = function ()
{
    if (CUserAgent.instance === null)
        CUserAgent.instance = new CUserAgent ();
    return (CUserAgent.instance);
}
