// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


// Repository of old "Atlas" code that we're waiting to have integrated into the new Microsoft Ajax Library


///////////////////////////
/// Sys.UI.DomElement

// DELTA - not present in codebase but called from PopupBehavior
Sys.UI.DomElement.setVisible = function(e, value) {

    if (!e) return;

    if (value != Sys.UI.DomElement.getVisible(e)) {
        
        if (value) {
            if (e.style.removeAttribute) {
                e.style.removeAttribute("display");
            } else {
               e.style.removeProperty("display");
            }
        }
        else {
            e.style.display = 'none';
        }
        
        e.style.visibility = value ? 'visible' : 'hidden';
    }
}

Sys.UI.DomElement.getVisible = function(e) {

    if (!e) return false;

    return (("none" != AjaxControlToolkit.CommonToolkitScripts.getCurrentStyle(e, "display")) &&
        ("hidden" != AjaxControlToolkit.CommonToolkitScripts.getCurrentStyle(e, "visibility")));
}


/////////////////////////////////////////////////////////////////
//  Sys.Net.ServiceMethodRequest
//


Sys.Net.ServiceMethodRequest = function() {
    Sys.Net.ServiceMethodRequest.initializeBase(this);
}
Sys.Net.ServiceMethodRequest.prototype = {
    _url: null,
    _methodName: null,
    _parameters: null,
    _userContext: null,
    _result: null,
    _request: null,
    _timeoutInterval: 0,
    
    // DELTA
    
    get_userContext : function() {
        return this._userContext;
    },
    
    get_url: function() {
        /// <value type="String">The url</value>
        return this._url;
    },
    set_url: function(value) {
        this._url = value;
    },
    // called by WebMethod
    _get_path: function() {
        /// <value type="String">The url</value>
        return this.get_url();
    },
 
    get_methodName: function() {
        /// <value type="String">WebMethod method name.</value>
        return this._methodName;
    },
    set_methodName: function(value) {
        this._methodName = value;
    },
 
    get_parameters: function() {
        /// <value type="Array">Input parameters to the WebMethod.</value>
        if (this._parameters === null) {
            this._parameters = { };
        }
        return this._parameters;
    },
    
    get_result: function() {
        /// <value type="Object">Value returned from the invoked WebMethod.</value>
        return this._result;
    },
 
    get_timeoutInterval: function() {
        /// <value type="Number">The timeout interval in milliseconds.</value>
        return this._timeoutInterval;
    },
    
    set_timeoutInterval: function(value) {
        this._timeoutInterval = value;
    },
    
    // compat with WebMethod
    get_timeout: function() {
        /// <value type="Number">The timeout interval in milliseconds.</value>
        return this.get_timeoutInterval();
    },
    
    // Events
    add_completed: function(handler) {
        /// <summary>Adds a event handler for the completed event.</summary>
        /// <param name="handler" type="Function">The handler to add to the event.</param>
        this.get_events().addHandler("completed", handler);
    },
    
    remove_completed: function(handler) {
        /// <summary>Removes a event handler for the completed event.</summary>
        /// <param name="handler" type="Function">The handler to remove from the event.</param>
        this.get_events().removeHandler("completed", handler);
    },
 
    add_timeout: function(handler) {
        /// <summary>Adds a event handler for the timeout event.</summary>
        /// <param name="handler" type="Function">The handler to add to the event.</param>
        this.get_events().addHandler("timeout", handler);
    },
    
    remove_timeout: function(handler) {
        /// <summary>Removes a event handler for the timeout event.</summary>
        /// <param name="handler" type="Function">The handler to remove from the event.</param>
        this.get_events().removeHandler("timeout", handler);
    },
    
    add_error: function(handler) {
        /// <summary>Adds a event handler for the error event.</summary>
        /// <param name="handler" type="Function">The handler to add to the event.</param>
        this.get_events().addHandler("error", handler);
    },
    
    remove_error: function(handler) {
        /// <summary>Removes a event handler for the error event.</summary>
        /// <param name="handler" type="Function">The handler to remove from the event.</param>
        this.get_events().removeHandler("error", handler);
    },
 
    invoke: function(userContext) {
        /// <param name="userContext" mayBeNull="true" optional="true">WebMethod success callback</param>
        /// <returns type="Boolean">True if method was invoked, false if not.</returns>
        if (this._request !== null) {
            return false;
        }
 
        this._request = Sys.Net._WebMethod._invoke(this, this._methodName, this._methodName,false,
                                        this.get_parameters(), onMethodComplete, onMethodError, this);
 
        function onMethodComplete(result, target, methodName) {
            target._request = null;
            target._userContext = userContext;
            target._result = result;
            var handler = target.get_events().getHandler("completed");
            if (handler) {
                // DELTA - bug, was this
                handler(target, Sys.EventArgs.Empty);
            }
        }
 
        function onMethodError(result, target, methodName) {
            target._request = null;
            target._userContext = userContext;
            // CONSIDER: should the error object (result) have its own member _error?
            target._result = result;
            var isTimeout=false;
            if(result.get_errorStatus) isTimeout = (result.get_errorStatus() === 2);
            else if(result.get_timedOut) isTimeout = result.get_timedOut();
            var handler;
            if (isTimeout) {
                handler = target.get_events().getHandler("timeout");
            }
            else {
                handler = target.get_events().getHandler("error");
            }
            if (handler) {
                handler(target, Sys.EventArgs.Empty);
            }
        }
        return true;
    }
}
// Removed in ValueAdd: response, priority, appUrl, abort (event)
Sys.Net.ServiceMethodRequest.descriptor = {
    properties: [   {name: 'url', type: String},
                    {name: 'methodName', type: String},
                    {name: 'parameters', type: Object, readOnly: true},
                    {name: 'result', type: Object, readOnly: true},
                    {name: 'timeoutInterval', type: Number} ],
    methods: [ {name: 'invoke', params: [ {name: 'userContext', type: Object} ] } ],
    events: [   {name: 'completed'},
                {name: 'timeout'},
                {name: 'error'} ]
}
 
Sys.Net.ServiceMethodRequest.registerClass('Sys.Net.ServiceMethodRequest', Sys.Component);


//////////////////////////////////////
// Sys.UI.Control.overlaps
//

Sys.UI.Control.overlaps = function overlaps(r1, r2) {
    var xLeft = (r1.x >= r2.x && r1.x <= (r2.x + r2.width));
    var xRight = ((r1.x + r1.width) >= r2.x && (r1.x + r1.width) <= r2.x + r2.width);
    var xComplete = ((r1.x < r2.x) && ((r1.x + r1.height) > (r2.x + r2.height)));
    
    var yLeft = (r1.y >= r2.y && r1.y <= (r2.y + r2.height));
    var yRight = ((r1.y + r1.height) >= r2.y && (r1.y + r1.height) <= r2.y + r2.height);
    var yComplete = ((r1.y < r2.y) && ((r1.y + r1.height) > (r2.y + r2.height)));
    if ((xLeft || xRight || xComplete) && (yLeft || yRight || yComplete)) {
        return true;
    }
    
    return false;
}
