///<reference path="../../TESCO.js" />
///<reference path="../event.js" />
///<reference path="../eventManager.js" />
///<reference path="../exception.js" />
///<reference path="../serialization.js" />
///<reference path="XMLHTTP.js" />

/*
	Example:

	var _connection = new TESCO.system.connection.ajax(false);
    TESCO.system.event.attach(_connection, "complete",
		function(e) {
		    callback(e);
		}
	);
    TESCO.system.event.attach(_connection, "error",
		function(e) {
		    e.exception.dialogue.show(e.exception);
		}
	);
    _connection.request("url.aspx", {}, TESCO.system.connection.XMLHTTP.METHOD.post, TESCO.system.connection.XMLHTTP.MIMETYPE.xml);
*/
TESCO.$("system.connection").ajax = function(async, abortPreviousRequest, defaultMimeType, characterEncoding, credentials) {
	/// <summary>wrapper for XMLHTTP class, provides custom events and simple methods for AJAX</summary>
    
    TESCO.system.event.manager.call(this,
		"request",
		"beforerequest",
		"complete",
		"error",
		"abort",
		"authenticationError"
	);

    this.VERSION = "1.1.0";
    this.NAME = "TESCO.system.connection.ajax";
    var _self = this;
    var _XMLHTTP = this.XMLHTTP = new TESCO.system.connection.XMLHTTP(async, abortPreviousRequest, defaultMimeType, characterEncoding, credentials);

    TESCO.system.event.attach(_XMLHTTP, "abort",
		function(e) {
		    _self.dispatchEvent("abort", e);
		}
	);
    TESCO.system.event.attach(_XMLHTTP, "request",
		function(e) {
		    _self.dispatchEvent("request", e);
		}
	);
    TESCO.system.event.attach(_XMLHTTP, "beforerequest",
		function(e) {
		    _self.dispatchEvent("beforerequest", e);
		}
	);
    TESCO.system.event.attach(_XMLHTTP, "error",
		function(e) {
		    _self.dispatchEvent("error", e);
		}
	);

    TESCO.system.event.attach(_XMLHTTP, "complete", handleResponse);

    this.request = function(url, params, method, mimeType, customHeaders, customObject) {
		/// <summary>create the request and send it</summary>
        if (params.request) {
            params.request["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance";
            params.request.timestamp = new Date().getTime();
            params.request.referrerUrl = document.location.href;
            return _self.XMLHTTP.request(url, params, method, mimeType, customHeaders, customObject);
        } else {
            throw new TESCO.system.Exception("TESCO.system.connection.ajax: INVALID PARAM");
        }
    }

    function handleResponse(e) {
        try {
            if (e.response.response) {
                switch (e.response.response.status) {
                    case "Success":
                        _self.dispatchEvent("complete", e);
                        if (e.response.response.exceptions) {
                            TESCO.UI.Dialogue.Static.show(e.response.response.exceptions.nodeValue);
                        }
                        break;
                    default:
                        throw new exceptions.invalidResponseStatus(e.response.response.status, "handleResponse");
                }
            } else if (e.response.parsererror) {
                throw new exceptions.invalidResponseObject(" :" + e.response.parsererror, "handleResponse");
            } else {
                throw new exceptions.invalidResponseObject("", "handleResponse");
            }
        } catch (err) {
            _self.dispatchEvent("error", { exception: err });
        }
    }

    var exceptions = {
        "invalidResponseStatus": function(message, functionName) {
            this.exception = TESCO.system.Exception;
            this.exception("Invalid response.status: " + message, TESCO.system.connection.ajax, functionName);
        },
        "invalidResponseObject": function(message, functionName) {
            this.exception = TESCO.system.Exception;
            this.exception("Invalid response" + message, TESCO.system.connection.ajax, functionName);
        }
    }
}