﻿Type.registerNamespace("OmniWeb");

OmniWeb.Common = function()
{
    this._ScrollbarWidth = null;
    this._ScrollbarHeight = null;
}

OmniWeb.Common.prototype = {
    //
    // Set Display Style
    //
    SetDisplay: function(cntrl, value)
    {
        if (!value) {
            cntrl.style.display = "none";
        } else if (cntrl.style.removeAttribute) {
            cntrl.style.removeAttribute("display");
        } else {
            cntrl.style.removeProperty("display");
        }
    },
    //
    // Get Client Windows Size
    //
    GetClientSize: function()
    {
        var width;
        var height;
        switch (Sys.Browser.agent) {
            case Sys.Browser.Firefox:
                width = window.innerWidth;
                height = window.innerHeight;
                break;
            default:
                width = document.documentElement.clientWidth;
                height = document.documentElement.clientHeight;
                break;
        }
        return new Sys.UI.Point(width, height);
    },
    //
    // Get Client Scroll Position
    //
    GetScrollPosition: function()
    {
        var top;
        var left;
        switch (Sys.Browser.agent) {
            case Sys.Browser.Safari:
                top = document.body.scrollTop;
                left = document.body.scrollLeft;
                break;
            default:
                top = document.documentElement.scrollTop;
                left = document.documentElement.scrollLeft;
                break;
        }
        return new Sys.UI.Point(left, top);
    },
    //
    // Get Client Scroll Position
    //
    GetScrollBounds: function()
    {
        var scrollTop;
        var scrollLeft;
        var scrollWidth;
        var scrollHeight;
        switch (Sys.Browser.agent) {
            case Sys.Browser.Safari:
                scrollTop = document.body.scrollTop;
                scrollLeft = document.body.scrollLeft;
                scrollWidth = document.body.scrollWidth;
                scrollHeight = document.body.scrollHeight;
                break;
            default:
                scrollTop = document.documentElement.scrollTop;
                scrollLeft = document.documentElement.scrollLeft;
                scrollWidth = document.documentElement.scrollWidth;
                scrollHeight = document.documentElement.scrollHeight;
                break;
        }
        return new Sys.UI.Bounds(scrollLeft, scrollTop, scrollWidth, scrollHeight);
    },
    //
    // Center Element On Screen
    //
    CenterElement: function(cntrl)
    {
        var Size = this.GetClientSize();
        var Scroll = this.GetScrollPosition();
        var x = Math.floor((Size.x - cntrl.offsetWidth) / 2) + Scroll.x;
        if (x < 0) x = 0;
        var y = Math.floor((Size.y - cntrl.offsetHeight) / 2) + Scroll.y;
        if (y < 0) y = 0;
        Sys.UI.DomElement.setLocation(cntrl, x, y);
    },
    //
    // Calculate Size Of Scrollbars
    //
    GetScrollbarSize: function()
    {
        if (this._ScrollbarWidth === null || this._ScrollbarHeight === null) {
            var outer = document.createElement("div");
            outer.style.width = "50px";
            outer.style.height = "50px";
            outer.style.overflow = "scroll";
            outer.style.position = "absolute";
            outer.style.top = "-200px";
            outer.style.left = "-200px";
            var inner = document.createElement("div");
            outer.appendChild(inner);
            document.body.appendChild(outer);
            this._ScrollbarWidth = outer.offsetWidth - outer.clientWidth;
            this._ScrollbarHeight = outer.offsetHeight - outer.clientHeight;
            //document.body.removeChild(outer);     // This creates a storage leak!
        }
        return new Sys.UI.Point(this._ScrollbarWidth, this._ScrollbarWidth);
    },
    //
    // Calculate Element's Absolute Position
    //
    GetElementPosition: function(cntrl)
    {
        var top = 0;
        var left = 0;
        var temp = cntrl;
        while (temp) {
            top += temp.offsetTop;
            left += temp.offsetLeft;
            temp = temp.offsetParent;
        }
        return new Sys.UI.Point(left, top);
    }
}

var $OmniWeb = new OmniWeb.Common();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();