
/* global vars */

var requestObject = null;
var onStateChangeFunction = null;

/* ajax object */

var Ajax =
{
    initialized: false,
    requests: 0,

    getRequestObject: function()
    {
        http_request = null;

        if( window.XMLHttpRequest )
        { // FF usw.
            http_request = new XMLHttpRequest();

            if( http_request.overrideMimeType )
            {
                http_request.overrideMimeType('text/xml');
            }
        } else if( window.ActiveXObject ) {
            try
            { // IE
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch( e )
            {
                try
                {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch( e ){}
            }
        }

        return http_request || false;
    },

    setOptions: function( options )
    {
        this.method       = options.method       || this.method;
        this.asynchronous = options.asynchronous || this.asynchronous;
        this.contentType  = options.contentType  || this.contentType;
        this.encoding     = options.encoding     || this.encoding;

        if( typeof( options.parameters ) == 'object' )
        {
            this.parameters = '?';
            for( var i in options.parameters )
            {
                if( typeof(options.parameters[i]) == 'string' )
                {
                    this.parameters += i + "=" + options.parameters[i] + "&";
                }
            }
            this.parameters = this.parameters.substring(0, (this.parameters.length - 1)); /* cut & */
        }
        else if( typeof( options.parameters ) == 'string' )
        {
            this.parameters = '?' + options.parameters;
        }
        else
        {
            this.parameters = '';
        }
    },

    _initialize: function()
    {
        if( !this.initialized )
        {
            this.method       = 'get';
            this.asynchronous = true;
            this.contentType  = 'application/x-www-form-urlencoded';
            this.encoding     = 'UTF-8';
            this.parameters   = '';

            this.initialized = true; // remember status
        }
    },

    request: function( url, todo, options )
    {
        if( typeof( url )  != 'string' || url == '' )
        {
            return false
        }

        // get request object
        requestObject   = this.getRequestObject();

        if( !requestObject )
        {
            return false;
        }

        // new on state function for the request
        onStateChangeFunction = function()
        {
            if( requestObject.readyState == 4 )
            { // do it 
                todo( requestObject.responseText );
            }
        };

        // set options if necessary
        if( options ){ this.setOptions( options ) };

        // append parameters
        if( this.parameters != '' ){ url += this.parameters; };

        try
        {
            requestObject.open( this.method.toUpperCase(), url, this.asynchronous );
            requestObject.onreadystatechange = onStateChangeFunction;
            this.setRequestHeaders();
            requestObject.send( null );
        }
        catch( e )
        {
            return false;
        };

        // increment requests
        this.requests++;
    },


    /* from prototypejs */
    setRequestHeaders: function()
    {
        var headers =
        {
          'X-Requested-With': 'XMLHttpRequest',
          'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
        };

        if (this.method == 'post')
        {
            headers['Content-type'] = this.contentType + (this.encoding ? '; charset=' + this.encoding : '');

            /* Force "Connection: close" for older Mozilla browsers to work
             * around a bug where XMLHttpRequest sends an incorrect
             * Content-length header. See Mozilla Bugzilla #246651.
             */
            if( requestObject.overrideMimeType &&
                ( navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005 )
            {
                  headers['Connection'] = 'close';
            }
        }

        for (var name in headers)
        {
            requestObject.setRequestHeader(name, headers[name]);
        }
    }
};

Ajax._initialize();
