/*
 * mini-JAX 
 *
 * A very lightweight AJAX wrapper.
 *
 * By Lee Shepherd 
 * Copyright (c) 2007 Lee Shepherd
 *
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
function mJAXRequest(requestor, url, method, content, onloaded, attachment) {
	this.requestor = requestor
	this.url = url
	this.method = method
	this.content = content
	this.onloaded = onloaded
	this.attachment = attachment
}

function mJAXRequestor() {
	this.queue			= new Array()
	this.pending		= null
	this.http			= null
	this.processing		= false

	this.client	= function()
	{ 
		if ( window.XMLHttpRequest ) {
			return new XMLHttpRequest()
		}
		else if ( window.ActiveXObject ) {
			return new ActiveXObject( 'Microsoft.XMLHTTP' )
		}
		return null
	}
	
	this.get = function(url, onloaded, attachment) {
		this.enqueue(url, 'GET', null, onloaded, attachment)
		if (!this.processing) {
			this.next()
		}
	}

	this.post = function(url, content, onloaded, attachment) {
		this.enqueue(url, 'POST', content, onloaded, attachment)
		if (!this.processing) {
			this.next()
		}
	}

	this.process = function(request) {
		this.http = this.client()
		this.http.onreadystatechange = function(e) {
			if (ajax.pending != null && ajax.pending.requestor.http != null &&
				ajax.pending.requestor.http.readyState == 4 ) {
				eval( ajax.pending.onloaded + '(ajax.pending.requestor.http.responseText, ajax.pending)' )
				ajax.pending.requestor.next()
			}
		}
	
		this.pending = request
		if ( request.method == 'GET' ) {
			this.http.open( 'GET', request.url, true )
			this.http.send( null )
		}
		else {
			this.http.open( "POST", request.url, true )
			this.http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
			this.http.send( request.content )
		}
	}
	
	this.enqueue = function(url, method, content, onloaded, attachment) {
		this.queue.push(new mJAXRequest(this, url, method, content, onloaded, attachment))
	}

	this.next = function() {
		this.processing = (this.queue.length > 0)
		if (this.processing) {
			this.process(this.queue.shift())
		}
	}
}
var ajax = new mJAXRequestor()

