(function() {
    var Ext = window.Ext4 || window.Ext;

    /**
     * @private
     * Utilities for working with the main window.
     */
    Ext.define('Rally.util.Window', {
        singleton: true,

        getMainWindow: function() {
            var win = this.getWindow();
            if (win.opener) {
                try {
                    if (win.opener.location.host === win.host) {
                        win = win.opener;
                    }
                } catch(e) {}
            }
            if (win.top && win.top !== win) {
                win = win.top;
            }

            return win;
        },

        getWindow: function() {
            return window;
        },

        getScrollX: function () {
            var x,
                w = this.getWindow();

            if (w.pageXOffset) {
                x = w.pageXOffset;
            } else if (document.documentElement && document.documentElement.scrollLeft) {
                x = document.documentElement.scrollLeft;
            } else if (document.body) {
                x = document.body.scrollLeft;
            }

            return x;
        },

        getScrollY: function () {
            var y,
                w = this.getWindow();

            if (w.pageYOffset) {
                y = w.pageYOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {
                y = document.documentElement.scrollTop;
            } else if (document.body) {
                y = document.body.scrollTop;
            }

            return y;
        },

        getWidth: function () {
            var x,
                w = this.getWindow();

            if (w.innerHeight) {
                x = self.innerWidth;
            }
            else if (document.documentElement && document.documentElement.clientWidth) {
                x = document.documentElement.clientWidth;
            }
            else if (document.body) {
                x = document.body.clientWidth;
            }

            return x;
        },

        getHeight: function() {
            var y;

            if (self.innerHeight) {
                y = self.innerHeight;
            }
            else if (document.documentElement && document.documentElement.clientHeight) {
                y = document.documentElement.clientHeight;
            }
            else if (document.body) {
                y = document.body.clientHeight;
            }

            return y;
        },

        isInFrame: function() {
            return top !== self;
        }

    });

    /**
     * @private
     * @method getWindow
     * @member Rally
     * @alias Rally.util.Window#getWindow
     */
    Rally.getWindow = function() {
        return Rally.util.Window.getWindow();
    };

})();