/*Array.prototype.toArray = Array.prototype.clone;  */

/**
* May Control Ajax Class
* @author Dominik Danninger
* @version 1.0
*/
var Ajax = Class.create();
Ajax.prototype = {
    /**
    * Intialize the Connection
    *
    * @param  string URL. Required
    * @param  string Function to execute. Required
    * @param  string Method. Required
    * @param  string Params to send. Required
    * @return void
    * @access public
    */
    initialize: function(url, func, method, params) {
        this.transport = this.getTransport();
        this.func = func;
        this.method = method;
        this.params = params;
        this.request(url);
    },

    /**
    * Get the Transport .. For Firefox / MS IE
    *
    * @return void
    * @access private
    */
    getTransport: function() {
        return Try.these (
            function() {return new XMLHttpRequest()},
            function() {return new ActiveXObject('Msxml2.XMLHTTP')},
            function() {return new ActiveXObject('Microsoft.XMLHTTP')}
        ) || false;
    },

    /**
    * Send a Request to the url
    *
    * @param  string URL. Required.
    * @return void
    * @access private
    */
    request: function(url) {
        this.url = url;
        this.contentType = 'application/x-www-form-urlencoded';
        this.transport.open(this.method, this.url, true);
        this.transport.onreadystatechange = this.responseToFunc.bind(this);
        this.transport.setRequestHeader("Content-type", this.contentType);
        this.transport.setRequestHeader("Content-length", this.params.length);
        this.transport.setRequestHeader("Connection", "close");
        this.transport.send(this.params);
    },

    /**
    * If Get Response send it to the Function
    *
    * @return void
    * @access private
    */
    responseToFunc: function() {
        var readyState = this.transport.readyState;
        if (readyState == 4 && this.transport.status == 200) {
            this.func(this.transport);
        }
    },
}
