/**
 * Get some window properties and extend default JavaScript window object
 *
 * @version $Id: windowx.js,v 1.1 2006/02/06 13:53:53 Leendert Exp $
 */
var WindowX = {

    innerSize         : null,
    innerWidth        : null,
    innerHeight       : null,
    pageSize          : null,
    pageWidth         : null,
    pageHeight        : null,
    pageOffset        : null,
    pageXOffset       : null,
    pageYOffset       : null,

    _previousOnResize : null,
    _previousOnLoad   : null,
    
    _bodyMargin       : null,

    /**
     * @constructor
     */
    create : function()
    {
        WindowX.getWindowSize();
        WindowX._previousOnResize = window.onresize;
        window.onresize = WindowX.onresize;
        WindowX._previousOnLoad = window.onload;
        window.onload = WindowX.onload;
    },

    /**
     * Handle onresize events
     *
     * @access public
     * @return true
     */
    onresize : function(e)
    {
        if (e) event = e;
        result = WindowX.getWindowSize();
        /* again after a second */
        setTimeout('WindowX.getWindowSize();', 1000);
        if (WindowX._previousOnResize) {
            /* call previous handler */
            return WindowX._previousOnResize(event);
        } else {
            return result;
        }
    },

    /**
     * Handle onload events
     */

    onload : function(e)
    {
        if (e) event = e;
        result = WindowX.getWindowSize();
        if (WindowX._previousOnLoad) {
            /* call previous handler */
            return WindowX._previousOnLoad(event);
        } else {
            return result;
        }
    },

    /**
     * Get window properties
     *
     * @access public
     * @return true
     */
    getWindowSize : function()
    {
        WindowX.getInnerSize();
        WindowX.getPageSize();
        WindowX.getPageOffset();
        return true;
    },

    /**
     * Get inner sizes of the window
     *
     * @access public
     * @return the sizes or false on error
     */
    getInnerSize : function()
    {
        try {
            WindowX.innerWidth = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0);
        } catch(error) {
        }
        try {
            WindowX.innerHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0);
        } catch(error) {
        }
        WindowX.innerSize = { x : WindowX.innerWidth, y : WindowX.innerHeight};
        window.innerSize = WindowX.innerSize;
        return WindowX.innerSize;
    },

    /**
     * Get body margin
     *
     * @access private
     * @return body margin
     */
    _getBodyMargin : function()
    {
        /* first node is block level node */
        if (document.body.hasChildNodes() && (document.body.firstChild.nodeName.match(/address|blockquote|dd|div|dl|dt|fieldset|form|frame|frameset|h\d|noframes|ol|p|ul|center|dir|hr|menu|pre/))) {
            return { x : document.body.firstChild.offsetLeft, y : document.body.firstChild.offsetTop };
        }
        /* block level node is preceded by text node containing spaces */
        if ((document.body.childNodes.length > 1) && (document.body.firstChild.nodeType == 3) && (document.body.firstChild.nodeValue.match(/^\s*$/g)) &&  (document.body.firstChild.nextSibling.nodeName.match(/address|blockquote|dd|div|dl|dt|fieldset|form|frame|frameset|h\d|noframes|ol|p|ul|center|dir|hr|menu|pre/))) {
            return { x : document.body.firstChild.nextSibling.offsetLeft, y : document.body.nextSibling.firstChild.offsetTop };
        }
        // default
        return { x : 8, y : 8 };
    },

    /**
     * Get page size
     *
     * @access public
     * @return the page size
     */
    getPageSize : function()
    {
        WindowX.getInnerSize();
        WindowX._bodyMargin = WindowX._getBodyMargin();
        if ((document.documentElement.scrollHeight == document.body.scrollHeight) && (document.body.scrollWidth == WindowX.innerSize.x) && (document.body.scrollHeight == WindowX.innerSize.y)) {
            /* Mozilla if no doctype given */
            WindowX.pageWidth = document.body.offsetWidth + 2 * WindowX._bodyMargin.x;
            WindowX.pageHeight = document.body.offsetHeight + 2 * WindowX._bodyMargin.y;
        } else {
            WindowX.pageWidth = document.body.scrollWidth + 2 * ((document.body.offsetLeft) ? document.body.offsetLeft : WindowX._bodyMargin.x);
            WindowX.pageHeight = document.body.scrollHeight + 2 * ((document.body.offsetTop) ? document.body.offsetTop : WindowX._bodyMargin.y);
        }
        WindowX.pageSize = { x : WindowX.pageWidth, y : WindowX.pageHeight};
        window.pageSize = WindowX.pageSize;
        return WindowX.pageSize;
    },

    /**
     * Get scroll offset of the window
     *
     * @access private
     * @return the offset
     */
    getPageOffset: function()
    {
        try {
            WindowX.pageXOffset = (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0);
        } catch(error) {
        }
        try {
            WindowX.pageYOffset = (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
        } catch(error) {
        }
        WindowX.pageOffset = { x : WindowX.pageXOffset, y : WindowX.pageYOffset};
        window.pageOffset = WindowX.pageOffset;
        return WindowX.pageOffset;
    },

    /**
     * Set inner sizes of window
     *
     * @access public
     * @param x the new width
     * @param y the new height
     * @return true
     */
    setInnerSize : function(x, y)
    {
        WindowX.getInnerSize();
        window.resizeBy(x - WindowX.innerSize.x, y - WindowX.innerSize.y);
        return true;
    },

    /**
     * Set size of window to content size
     *
     * @access public
     */
    sizeToContent : function()
    {
        WindowX.getPageSize();
        return WindowX.setInnerSize(WindowX.pageSize.x, WindowX.pageSize.y);
    }

};

/* call constructor */
WindowX.create();
