/*
 XAJAX - XANIDO AJAX
 (c) Timothy Anido 2006/2007
 
Provides an easy to use AJAX controller

Usage:
  myajax = new ajax( 'url' );
  myajax.onLoading = function;
  myAjax.onSuccess = function;
  myAjax.sendQeury( 'query=string' );
  
 Requires:
  - xoo.js

*/

var ajax = new Class( {
	
	connector : null,
	onLoading : function() { },
	onSuccess : function() { },
	receiver: null,
	response: null,
	cleanRequest: false,
	
	initialize : function( receiver )
	{
		if (window.ActiveXObject) connector = new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) connector = new XMLHttpRequest();
		this.connector = connector;
		if( receiver ) this.receiver = receiver;
	},
	
	sendRequest : function( query )
	{
		if( this.loading( ) == 1 ) return;
		
		t = new Date().getTime( );
		
		querystring = ( this.cleanRequest ) ? query + '/' + t : "?"+t+"&"+query;
		
		
		
	    this.connector.open('get', this.receiver + querystring);
	    this.connector.onreadystatechange = this.stateChange.bind(this);
	    this.connector.send(null);
	},
	
	stateChange : function( )
	{
		if(this.connector.readyState == 1) { this.onLoading(); }
		if(this.connector.readyState == 4) { this.response = this.connector.responseText; this.onSuccess(); }
	},
	
	loading : function( )
	{
		return this.connector.readyState;
	}

} );
