var CommonClientScript = new Object();
var Cynapps = new Object();

// ==================================================
// Basic functionality to create namespaces.
//
CommonClientScript.RegisterNamespace = function(parentNamespaceName, newNamespaceName) {

    if (parentNamespaceName == null) {
        var isDefined = false;
        try {
            if (typeof (eval(newNamespaceName)) != "undefined") {
                isDefined = true;
            }
        }
        catch (e) {
            //
        }

        // if the namespace is already defined, we don't have
        // to add it again.
        if (!isDefined) {
            // create the new namespace.
            eval(newNamespaceName + " = new Object();");
        }
    }
    else {
        // if the namespace is already defined, we don't have
        // to add it again.
        if (typeof (eval(parentNamespaceName + "." + newNamespaceName)) == "undefined") {
            // create the new namespace.
            eval(parentNamespaceName + "." + newNamespaceName + " = new Object();");
        }
    }
}

// ==================================================
// Create namespace and class 'Cynapps.Frameworks.Web.Core'
// And add methods to the class.
//
CommonClientScript.RegisterNamespace(null, "Cynapps");
CommonClientScript.RegisterNamespace("Cynapps", "Frameworks");
CommonClientScript.RegisterNamespace("Cynapps.Frameworks", "Web");

// ==================================================
// You can put some construction logic here.
//
Cynapps.Frameworks.Web._core = function() {
    this._agent = null;
    this._version = null;
}

// ==================================================
// Create a prototype.
//
Cynapps.Frameworks.Web._core.prototype = {

    // ==================================================
    // Crosso browser event handling
    //
    AddEvent: function(element, eventType, handler) {
        if (!element.eventHandlers) {
            element.eventHandlers = [];
        }

        if (!element.eventHandlers[eventType]) {
            element.eventHandlers[eventType] = [];
            if (element['on' + eventType]) {
                element.eventHandlers[eventType].push(element['on' + eventType]);
            }

            element['on' + eventType] = this._handleEvent;
        }
        element.eventHandlers[eventType].push(handler);
    },

    RemoveEvent: function(element, eventType, handler) {
        var handlers = element.eventHandlers[eventType];
        for (var i in handlers) {
            if (handlers[i] == handler) {
                delete handlers[i];
            }
        }
    },

    _fixEvent: function(e) {
        // add W3C standard event methods
        e.preventDefault = function() {
            this.returnValue = false;
        };

        e.stopPropagation = function() {
            this.cancelBubble = true;
        };

        return event;
    },

    _handleEvent: function(e) {
        var returnValue = true;
        if (!e) {
            e = Cynapps.Frameworks.Web.Core._fixEvent(event);
        }

        var handlers = this.eventHandlers[e.type]
        for (var i in handlers) {
            this.$$handleEvent = handlers[i];
            returnValue = !((returnValue && this.$$handleEvent(e)) == false);
        }

        return returnValue;
    },

    // ==================================================
    // Browser detection.
    //
    GetBrowser: function() {
        if (this._agent == null) {
            switch (Sys.Browser.agent) {
                case Sys.Browser.InternetExplorer:
                    this._agent = "IE";
                    break;
                case Sys.Browser.Safari:
                    this._agent = "SA";
                    break;
                case Sys.Browser.Opera:
                    this._agent = "OP";
                    break;
                case Sys.Browser.Firefox:
                    this._agent = "FF";
                    break;
            }
        }

        if (this._version == null) {
            this._version = parseInt(navigator.appVersion);
        }

        return { agent: this._agent, version: this._version };
    },

    // ==================================================
    // Get the absolute position (x,y) of an element
    // on the page.
    //
    GetLocation: function(element) {
        return Sys.UI.DomElement.getLocation(element);
    },

    // ==================================================
    // Get the size (w,h) of the specified element.
    //
    GetSize: function(element) {
        return { w: element.offsetWidth, h: element.offsetHeight };
    },

    // ==================================================
    // Get the rectangle (x,y,w,h) of the specified element.
    //
    GetRect: function(element) {
        var bounds = Sys.UI.DomElement.getBounds(element);
        bounds.y += (Sys.Browser.agent == Sys.Browser.InternetExplorer) ? document.body.scrollTop : 0;
        return { x: bounds.x, y: bounds.y, w: bounds.width, h: bounds.height };
    },

    // ==================================================
    // Positions the element to fit in the specified rectangle.
    //
    SetRect: function(element, x, y, w, h) {
        if (x == null || y == null) {
            var rect = this.GetRect(element);
            if (x == null) {
                x = rect.x;
            }
            if (y == null) {
                y = rect.y;
            }
        }

        Sys.UI.DomElement.setLocation(element, x, y);
        if (w != null && w >= 0) {
            element.style.width = w;
        }

        if (h != null && h >= 0) {
            element.style.height = h;
        }
    },

    // ==================================================
    // IE + FireFox compatible use the browser's build in encoding
    // capabilities to perform html encoding.
    //
    EscapeHTML: function(s) {
        var div = document.createElement('div');
        div.appendChild(document.createTextNode(s));
        return div.innerHTML;
    },

    // ==================================================
    // Gets the width and height of the browser client window (excluding scrollbars)
    // returns type="Sys.UI.Bounds"
    // Browser's client width and height
    //
    GetClientSize: function() {
        var w = 0;
        var h = 0;

        //IE
        if (!window.innerWidth) {
            //strict mode
            if (!(document.documentElement.clientWidth == 0)) {
                w = document.documentElement.clientWidth;
                h = document.documentElement.clientHeight;
            }
            //quirks mode
            else {
                w = document.body.clientWidth;
                h = document.body.clientHeight;
            }
        }
        //w3c
        else {
            w = window.innerWidth;
            h = window.innerHeight;
        }
        return { w: w, h: h };
    }
}

// ==================================================
// Create an instance of the prototype.
//
Cynapps.Frameworks.Web.Core = new Cynapps.Frameworks.Web._core();


// ==================================================
// Create namespace and class 'Cynapps.Frameworks.Web.UI.Controls'
// And add methods to the class.
//
CommonClientScript.RegisterNamespace(null, "Cynapps");
CommonClientScript.RegisterNamespace("Cynapps", "Frameworks");
CommonClientScript.RegisterNamespace("Cynapps.Frameworks", "Web");
CommonClientScript.RegisterNamespace("Cynapps.Frameworks.Web", "UI");
CommonClientScript.RegisterNamespace("Cynapps.Frameworks.Web.UI", "Controls");
