﻿/*	+--------------------------------------------
| CLASS clientIdentification 
|
| issues
| -------
|         ie mac -> identifies always as mac classic
|   netscape 4.7 -> identifies winxp  as win2k
|         safari -> doesn't give the correct browser version (actually gives the build version)
|
|
| [browser/os]
| -- some patterns from http://jsbrwsniff.sourceforge.net/
|
| [dimension]
| -- some dectections from http://quirksmode.org
|
|
|------------------
| ci.browser
|------------------
| ci.browser.getId()
|    >> browser
|
| ci.browser.getVersion()
|    >> version
|
| ci.browser.getMajorVersion()
|    >> major version
|
| ci.browser.getMinorVersion()
|    >> minor version
|
| ci.browser.isIe()
|    >> is internet explorer
|
| ci.browser.isNetscape()
|    >> is netscape
|
| ci.browser.isFirefox()
|    >> is firefox
|
| ci.browser.isOpera()
|    >> is opera
|
| ci.browser.isSafari()
|    >> is safari
|
| ci.browser.isCamino()
|    >> is camino
|
| ci.browser.isKonqueror()
|    >> is konqueror
|
| ci.browser.isMozilla()
|    >> is mozilla
|
| ci.browser.isUnknown()
|    >> is browser unknown
|
| ci.browser.isVersionMin(version)
|    -- minimum version
|    >> is a least this version
|    
|    
|------------------
| ci.dimension
|------------------
| ci.dimension.getDocumentWidth()
|    >> page width
|    
| ci.dimension.getDocumentHeight()
|    >> page height
|    
| ci.dimension.getScreenWidth()
|    >> screen width
|    
| ci.dimension.getScreenHeight()
|    >> screen height
|    
| ci.dimension.getWindowWidth()
|    >> window width
|    
| ci.dimension.getWindowHeight()
|    >> window height
|    
|    
|------------------
| ci.os
|------------------
| ci.os.getId()
|    >> os
|
| ci.os.getVersion()
|    >> version
|
| ci.os.isLinux()
|    >> is linux
|
| ci.os.isUnix()
|    >> is unix
|
| ci.os.isMac()
|    >> is mac
|
| ci.os.isWindows()
|    >> is windows
|
| ci.os.isUnknown()
|    >> is os unknown
|
| ci.os.isVersionMin(version)
|    -- minimum version
|    >> is a least this version
|    
|    
|------------------
| ci.position
|------------------
| ci.position.getScrollX()
|    >> scroll X position
|    
| ci.dimension.getScrollY()
|    >> scroll Y position
|    
|    
|------------------
| ci.speed
|------------------
| ci.speed.test(code)
|       tests the connection speed
|    -- code to execute when test is over write   / * speed * /  [with no space] where speed value is needed
|    -- minimum speed in kbps (stops download if takes too much time)
|
+-------------------------------------------- */


/*try {
new clientIdentification();
} catch(e) {*/

/* +--------------------------------------------
| 
| _clientIdentification_browser 
| 
+-------------------------------------------- */

function _clientIdentification_browser() {

    /* +--------------------------------------------
    | _getMajorVersion 
    +-------------------------------------------- */
    this._getMajorVersion = function(version) {
        if (version && /\./.test(version)) {
            return version.split('.')[0];
        } else {
            return version || 0;
        }
    }

    /* +--------------------------------------------
    | _getMinorVersion 
    +-------------------------------------------- */
    this._getMinorVersion = function(version) {
        if (version && /\./.test(version)) {
            return version.split('.')[1];
        } else {
            return 0;
        }
    }


    /* +--------------------------------------------
    | getId 
    +-------------------------------------------- */
    this.getId = function() {
        return this.id;
    }

    /* +--------------------------------------------
    | getVersion 
    +-------------------------------------------- */
    this.getVersion = function() {
        return this.version;
    }

    /* +--------------------------------------------
    | getMajorVersion 
    +-------------------------------------------- */
    this.getMajorVersion = function() {
        return this._getMajorVersion(this.version);
    }

    /* +--------------------------------------------
    | getMinorVersion 
    +-------------------------------------------- */
    this.getMinorVersion = function() {
        return this._getMinorVersion(this.version);
    }

    /* +--------------------------------------------
    | isIe 
    +-------------------------------------------- */
    this.isIe = function() {
        return (this.id == 'ie');
    }

    /* +--------------------------------------------
    | isNetscape 
    +-------------------------------------------- */
    this.isNetscape = function() {
        return (this.id == 'netscape');
    }

    /* +--------------------------------------------
    | isFirefox 
    +-------------------------------------------- */
    this.isFirefox = function() {
        return (this.id == 'firefox');
    }

    /* +--------------------------------------------
    | isOpera 
    +-------------------------------------------- */
    this.isOpera = function() {
        return (this.id == 'opera');
    }

    /* +--------------------------------------------
    | isSafari 
    +-------------------------------------------- */
    this.isSafari = function() {
        return (this.id == 'safari');
    }

    /* +--------------------------------------------
    | isCamino 
    +-------------------------------------------- */
    this.isCamino = function() {
        return (this.id == 'camino');
    }

    /* +--------------------------------------------
    | isKonqueror 
    +-------------------------------------------- */
    this.isKonqueror = function() {
        return (this.id == 'konqueror');
    }

    /* +--------------------------------------------
    | isMozilla 
    +-------------------------------------------- */
    this.isMozilla = function() {
        return (this.id == 'mozilla');
    }

    /* +--------------------------------------------
    | isUnknown 
    +-------------------------------------------- */
    this.isUnknown = function() {
        return (this.id == 'unknown');
    }

    /* +--------------------------------------------
    | isVersionMin 
    +-------------------------------------------- */
    this.isVersionMin = function(version) {

        // greater major
        if (this.getMajorVersion() > this.getMajorVersion(version)) {
            return true;

            // same major / greater or equal minor
        } else if (
				(this.getMajorVersion() == this._getMajorVersion(version)) &&
				(this.getMinorVersion() >= this._getMinorVersion(version))
			) {
            return true;

            // lesser major
        } else {
            return false;
        }
    }



    /* +--------------------------------------------
    | MAIN 
    +-------------------------------------------- */

    this.agent = navigator.userAgent.toLowerCase();


    // ie
    // ----------------------------
    if (/msie\s(\d+(\.?\d)*)/.test(this.agent)) {
        this.id = 'ie';
        this.version = this.agent.match(/msie\s(\d+(\.?\d)*)/)[1];

        // netscape [netscape 7.x]
        // ----------------------------
    } else if (/netscape\/(7\.\d*)/.test(this.agent)) {
        this.id = 'netscape';
        this.version = this.agent.match(/netscape\/(7\.\d*)/)[1];

        // netscape [netscape 6.x]
        // ----------------------------
    } else if (/netscape6[\/\s](\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'netscape';
        this.version = this.agent.match(/netscape6[\/\s](\d+([\.-]\d)*)/)[1];

        // netscape [netscape 4.x] - 1
        // ----------------------------
    } else if (/netscape4\/(\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'netscape';
        this.version = this.agent.match(/netscape4\/(\d+([\.-]\d)*)/)[1];

        // netscape [netscape 4.x] - 2
        // ----------------------------
    } else if (/mozilla\/(4.\d*)/.test(this.agent) && !(/msie\s(\d+(\.?\d)*)/.test(this.agent))) {
        this.id = 'netscape';
        this.version = this.agent.match(/mozilla\/(4.\d*)/)[1];

        // firefox
        // ----------------------------
    } else if (/firefox[\/\s](\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'firefox';
        this.version = this.agent.match(/firefox[\/\s](\d+([\.-]\d)*)/)[1];

        // firefox [firebird]
        // ----------------------------
    } else if (/firebird[\/\s](\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'firefox';
        this.version = this.agent.match(/firebird[\/\s](\d+([\.-]\d)*)/)[1];

        // firefox [phoenix]
        // ----------------------------
    } else if (/phoenix[\/\s](\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'firefox';
        this.version = this.agent.match(/phoenix[\/\s](\d+([\.-]\d)*)/)[1];

        // opera
        // ----------------------------
    } else if (/opera[\/\s](\d+(\.?\d)*)/.test(this.agent)) {
        this.id = 'opera';
        this.version = this.agent.match(/opera[\/\s](\d+(\.?\d)*)/)[1];

        // safari
        // ----------------------------
    } else if (/safari\/(\d)*/.test(this.agent)) {
        this.id = 'safari';
        this.version = ''; //this.agent.match(/safari\/(\d+(\.?\d*)*)/)[1];		  

        // camino
        // ----------------------------
    } else if (/camino[\/\s](\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'camino';
        this.version = this.agent.match(/camino[\/\s](\d+([\.-]\d)*)/)[1];

        // konqueror
        // ----------------------------
    } else if (/konqueror[\/\s](\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'konqueror';
        this.version = this.agent.match(/konqueror[\/\s](\d+([\.-]\d)*)/)[1];

        // mozilla    // always keep at the end
        // --------------------------------------
    } else if (/mozilla[\/\s](\d+([\.-]\d)*)/.test(this.agent)) {
        this.id = 'mozilla';
        this.version = this.agent.match(/rv\:(\d+([\.-]\d)*)/)[1];

        // unknown
        // ----------------------------
    } else {
        this.id = 'unknown';
        this.version = 0;
    }
}



/* +--------------------------------------------
| 
| _clientIdentification_dimension 
| 
+-------------------------------------------- */
function _clientIdentification_dimension() {

    // to not lose scope with setInterval
    var this2 = this;

    /* +--------------------------------------------
    | getDocumentWidth 
    +-------------------------------------------- */
    this.getDocumentWidth = function() {
        return this.documentWidth;
    }

    /* +--------------------------------------------
    | getDocumentHeight 
    +-------------------------------------------- */
    this.getDocumentHeight = function() {
        return this.documentHeight;
    }

    /* +--------------------------------------------
    | getScreenWidth 
    +-------------------------------------------- */
    this.getScreenWidth = function() {
        return this.screenWidth;
    }

    /* +--------------------------------------------
    | getScreenHeight 
    +-------------------------------------------- */
    this.getScreenHeight = function() {
        return this.screenHeight;
    }

    /* +--------------------------------------------
    | getWindowWidth 
    +-------------------------------------------- */
    this.getWindowWidth = function() {
        return this.windowWidth;
    }

    /* +--------------------------------------------
    | getWindowHeight 
    +-------------------------------------------- */
    this.getWindowHeight = function() {
        return this.windowHeight;
    }


    /* +--------------------------------------------
    | MAIN 
    +-------------------------------------------- */
    var xScroll;
    var yScroll;
    /*
        if (window.innerHeight && window.scrollMaxY) {
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
    */

    // firefox xhtml uses documentElement 
    var browser = new _clientIdentification_browser();
    xScroll = (browser.isFirefox() && (xScroll < document.documentElement.scrollWidth)) ? document.documentElement.scrollWidth : xScroll;

    if (self.innerHeight) {	// all except Explorer
        this.windowWidth = self.innerWidth;
        this.windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        this.windowWidth = document.documentElement.clientWidth;
        this.windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        this.windowWidth = document.body.clientWidth;
        this.windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < this.windowHeight) {
        this.documentHeight = this.windowHeight;
    } else {
        this.documentHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < this.windowWidth) {
        this.documentWidth = this.windowWidth;
    } else {
        this.documentWidth = xScroll;
    }

    this.screenWidth = screen.width;
    this.screenHeight = screen.height;
}



/* +--------------------------------------------
| 
| _clientIdentification_os 
| 
+-------------------------------------------- */
function _clientIdentification_os() {

    /* +--------------------------------------------
    | _checkMin 
    +-------------------------------------------- */
    this._checkMin = function(params) {

        var data = params['data'];
        var check = params['check'];
        var value = params['value'];

        // get check position
        var position = -1;
        for (var i = 0; i < data.length; ++i) {

            if (data[i] == check) {
                position = i;
                break;
            }
        }


        // never found it
        if (position == -1) {
            return false;
        } else {

            // validate with value position
            for (var i = 0; i < data.length; ++i) {

                // too far
                if (i > position) {
                    return false;
                }

                // got it before getting to far
                if (data[i] == value) {
                    return true;
                }
            }
        }
    }


    /* +--------------------------------------------
    | getId 
    +-------------------------------------------- */
    this.getId = function() {
        return this.id;
    }

    /* +--------------------------------------------
    | getVersion 
    +-------------------------------------------- */
    this.getVersion = function() {
        return this.version;
    }

    /* +--------------------------------------------
    | isLinux 
    +-------------------------------------------- */
    this.isLinux = function() {
        return (this.id == 'linux');
    }

    /* +--------------------------------------------
    | isUnix 
    +-------------------------------------------- */
    this.isUnix = function() {
        return (this.id == 'unix');
    }

    /* +--------------------------------------------
    | isMac 
    +-------------------------------------------- */
    this.isMac = function() {
        return (this.id == 'mac');
    }

    /* +--------------------------------------------
    | isWindows 
    +-------------------------------------------- */
    this.isWindows = function() {
        return (this.id == 'windows');
    }

    /* +--------------------------------------------
    | isUnknown 
    +-------------------------------------------- */
    this.isUnknown = function() {
        return (this.id == 'unknown');
    }

    /* +--------------------------------------------
    | isVersionMin 
    +-------------------------------------------- */
    this.isVersionMin = function(version) {

        var list;
        if (this.isWindows()) {
            list = ['2003', 'xp', '2000', 'me', '98', '95', 'nt4'];
        } else if (this.isMac()) {
            list = ['osx', 'classic'];
        } else {
            return false;
        }

        return this._checkMin({
            data: list,
            check: version,
            value: this.version
        });
    }




    /* +--------------------------------------------
    | MAIN 
    +-------------------------------------------- */

    this.agent = navigator.userAgent.toLowerCase();


    // linux
    // ----------------------------
    if (/linux/.test(this.agent)) {
        this.id = 'linux';

        // unix
        // ----------------------------
    } else if (/unix/.test(this.agent) || /x11/.test(this.agent)) {
        this.id = 'unix';

        // mac
        // ----------------------------
    } else if (/mac/.test(this.agent)) {

        this.id = 'mac';

        if (/mac\sos\sx/.test(this.agent)) {
            this.version = 'osx';

        } else if (/macintosh/.test(this.agent) || /mac\x5fpowerpc/.test(this.agent)) {
            this.version = 'classic';
        }


        // windows
        // ----------------------------
    } else if (/win/.test(this.agent)) {

        this.id = 'windows';

        if (/nt\s5\.2/.test(this.agent)) {
            this.version = '2003';

        } else if (/nt\s5\.1/.test(this.agent)) {
            this.version = 'xp';

        } else if (/nt\s5\.0/.test(this.agent)) {
            this.version = '2000';

        } else if (/windows\sme/.test(this.agent)) {
            this.version = 'me';

        } else if (/win98/.test(this.agent) || /windows\s98/.test(this.agent)) {
            this.version = '98';

        } else if (/windows\s95/.test(this.agent) || /win95/.test(this.agent)) {
            this.version = '95';

        } else if (/nt\s4\.0/.test(this.agent) || /nt4\.0/.test(this.agent)) {
            this.version = 'nt4';
        }

        // unknown
        // ----------------------------
    } else {
        this.id = 'unknown';
    }
}



/* +--------------------------------------------
| 
| _clientIdentification_position
| 
+-------------------------------------------- */
function _clientIdentification_position() {

    // to not lose scope with setInterval
    var this2 = this;

    /* +--------------------------------------------
    | getScrollX 
    +-------------------------------------------- */
    this.getScrollX = function() {
        return this.scrollX;
    }

    /* +--------------------------------------------
    | getScrollY 
    +-------------------------------------------- */
    this.getScrollY = function() {
        return this.scrollY;
    }


    /* +--------------------------------------------
    | MAIN 
    +-------------------------------------------- */
    this.scrollX = 0;
    if (document.documentElement && document.documentElement.scrollLeft) {
        this.scrollX = document.documentElement.scrollLeft;
    } else if (document.body && document.body.scrollLeft) {
        this.scrollX = document.body.scrollLeft;
    } else if (window.pageXOffset) {
        this.scrollX = window.pageXOffset;
    } else if (window.scrollX) {
        this.scrollX = window.scrollX;
    }

    this.scrollY = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        this.scrollY = document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        this.scrollY = document.body.scrollTop;
    } else if (window.pageYOffset) {
        this.scrollY = window.pageYOffset;
    } else if (window.scrollY) {
        this.scrollY = window.scrollY;
    }
}



/* +--------------------------------------------
| 
| _clientIdentification_speed 
| 
+-------------------------------------------- */
function _clientIdentification_speed() {

    // to not lose scope with setInterval
    var this2 = this;

    // 400k in kbits
    this.filesize = 400 * 8;


    /* +--------------------------------------------
    | _check 
    +-------------------------------------------- */
    this._check = function() {

        var end = new Date().getTime();
        var nbsec = ((end - this.start) / 1000);
        var toolong = (Math.floor(nbsec) > this.maxSeconds) ? 1 : 0;

        // if complete of it's been already too long
        if (this.binfile.complete || toolong) {
            clearInterval(this.interval);

            var speed = (toolong) ? 0 : Math.round(this.filesize / nbsec);

            // do the function
            eval(this.code.toString().replace('/*speed*/', 'speed'));
        }
    }
}



/* +--------------------------------------------
| 
| clientIdentification 
| 
+-------------------------------------------- */
function clientIdentification() {
    this.browser = new _clientIdentification_browser();
    this.dimension = new _clientIdentification_dimension();
    this.os = new _clientIdentification_os();
    this.position = new _clientIdentification_position();
    this.speed = new _clientIdentification_speed();
}
/*}*/
