/**
 * Get screen properties
 * 
 * @version $Id: screenx.js,v 1.6 2005/07/28 09:17:06 mkortleven Exp $  
 */
ScreenX_Class = function ()
{

    this.cookieName = 'js_screenx_props';
    this.resDivId = 'resolution_get_div_id';
    this.resPtWidth = 300;
    this.resEmWidth = 30;
    
    this._screenProperties = [ 'width', 'height', 'availWidth', 'availHeight',  'colorDepth' ];
    
    this._resDiv = null;
    this._properties = null;
    
    /**
     * Get screen properties of default JavaScript screen object
     *
     * @access private
     * @return bool true
     */
    this._getScreenProperties = function()
    {
        /* fetch object properties */
        try {
            for (this._i in screen) {
                this[this._i] = screen[this._i];
            }
        } catch(error) {
        }
        /* try to fetch additional properties because previous statement does not fetch all */
        for (this._i in this._screenProperties) {
            try {
                this[this._screenProperties[this._i]] = screen[this._screenProperties[this._i]];
            } catch(error) {
            }
        }
        return true;
    };

    /**
     * Get screen resolution in DPI
     *
     * @access public
     * @return int resolution
     */
    this.getResolution = function()
    {
        /* create DIV in pt */
        this._resDiv = document.createElement('DIV');
        this._resDiv.id = this.resDivId;
        this._resDiv.style.position = 'absolute';
        this._resDiv.style.display = 'block';
        this._resDiv.style.width = this.resPtWidth + 'pt';
        this._resDiv.style.height = '12pt';
        this._resDiv.style.top = '0px';
        document.body.appendChild(this._resDiv);
        this.pxPtRatio = this._resDiv.offsetWidth / this.resPtWidth;
        this.resolution = this.pxPtRatio * 72;
        /* attach to window object too */
        window.pxPtRatio = this.pxPtRatio;
        window.resolution = this.resolution;
        /* remove DIV */
        document.body.removeChild(this._resDiv);
        /* create DIV  in em*/
        this._resDiv = document.createElement('DIV');
        this._resDiv.id = this.resDivId;
        this._resDiv.style.position = 'absolute';
        this._resDiv.style.display = 'block';
        this._resDiv.style.width = this.resEmWidth + 'em';
        this._resDiv.style.height = '1em';
        this._resDiv.style.top = '0px';
        document.body.appendChild(this._resDiv);
        this.pxEmRatio = this._resDiv.offsetWidth / this.resEmWidth;
        /* attach to window object too */
        window.pxEmRatio = this.pxEmRatio;
        /* remove DIV */
        document.body.removeChild(this._resDiv);
        return this.resolution;
    };
    
    /**
     * Get properties
     *      
     * @link http://json.org/ JavaScript Object Notation 
     * @access private
     * @return string the JSON encoded properties array
     */         
    this._getPropertiesAsJson = function()
    {
        this._jsonString = '{';
        for (this._i in this) {
            if (!this._i.match(/^_/) && (typeof this[this._i] == 'number')) {
                this._jsonString += '"' + this._i + '": ' + this[this._i] + ',';
            }
        }
        this._jsonString = this._jsonString.replace(/,$/, '');
        this._jsonString += '}';
        return this._jsonString;
    };

    /**
     * Store screen properties to cookie
     * 
     * access public
     * @return bool false on error          
     */              
    this.storeProperties = function()
    {
        document.cookie = this.cookieName + '=' + escape(this._getPropertiesAsJson());
    };
    
    /**
     * Load screen properties from cookie
     * 
     * access public
     * @return bool false on error          
     */              
    this.loadProperties = function()
    {
        if (!document.cookie || (document.cookie == '')) {
            return false;
        }
        this._cookies = document.cookie.split(';');
        this._cookieFound = false;
        for (this._i in this._cookies) {
            this._cookie = this._cookies[this._i].split('=');
            if ((this._cookie.length == 2) && (this._cookie[0] == this.cookieName)) {
                this._properties = eval('(' + unescape(this._cookie[1]) + ')');
                for (this._j in this._properties) {
                    this[this._j] = this._properties[this._j];
                }
                this._cookieFound = true;
                break;
            }
        }
        return this._cookieFound;
    };
    
    /**
     * @constructor
     */
    this._getScreenProperties();
    this.getResolution();
    
};

ScreenX = new ScreenX_Class();
