/*
*	freeDOM-ajax (0.1)
*	Autor: Andre Metzen (andre[at]metzen.com.br)
*	Autor: Leandro Vieira (leandro[at]w3invent.com.br)
*	08/12/2006 - 10:37
*
*	Funcoes disponiveis:
*
*
*/

var startedLoading;

function ajax()
{ 
	this.fila = new Array();
	this.isUrlEscaped = true;
	this.nextExecFn = false;
	this.currentRequest = false;

	///////////////////////////////////////////////////
	// Cria o objeto XMLHttpRequest
	///////////////////////////////////////////////////
	if (window.XMLHttpRequest) { this.XHR=new XMLHttpRequest(); }
  	else
	{
    	try { this.XHR=new ActiveXObject("Msxml2.XMLHTTP"); }
   		catch(e)
		{
     		try { this.XHR=new ActiveXObject("Microsoft.XMLHTTP"); }
      		catch(e) { this.XHR=false; }
		}
	}

	///////////////////////////////////////////////////
	// Funcao que adicona uma nova requisaico ajax a fila
	///////////////////////////////////////////////////
	
	this.addRequest = function(strUrl, fnRetorno, dados)
	{
		if(arguments.length < 1) return false;
		this.fila.push({url: strUrl, data:(typeof(dados) != "undefined") ? this.getDados(dados): null , fn: fnRetorno});
		if(this.fila.length == 1 && !this.currentRequest) this.doRequest();
	}

	///////////////////////////////////////////////////
	// Funcao que executa a requisicao
	///////////////////////////////////////////////////
	this.doRequest = function()
	{
		if(this.currentRequest = this.fila.pop())
		{
			var method = (this.currentRequest.data==null) ? "GET" : "POST";
			this.XHR.open(method,this.currentRequest.url+(this.currentRequest.url.indexOf("?")==-1?"?":"&")+Math.random(),true);
			this.XHR.onreadystatechange=function() { var a=this; return function () { a.doReturn.apply(a, [a.XHR]); }}.apply(this);
			if(method=="POST"){ this.XHR.setRequestHeader("Content-type","application/x-www-form-urlencoded"); this.XHR.setRequestHeader("encoding", "UTF-8"); }
			this.XHR.send(this.currentRequest.data);
		}
	}

	///////////////////////////////////////////////////
	// Funcao que trabalha o retorno da requisicao
	///////////////////////////////////////////////////
	
	this.doReturn = function()
	{
		if(this.XHR.readyState==4)
		{
			this.currentRequest.fn(this.XHR.responseText);
			this.currentRequest = false;
			if(this.fila.length>0)this.doRequest();
		}
	}


	///////////////////////////////////////////////////
	// Efeito de loading
	///////////////////////////////////////////////////

  // crie uma id loading e uma classe hidden de estilo

  this.startLoading = function()
  {
    removeClass($("loading"), "hidden");
    startedLoading=true;
    
    /* Implementação */
    document.getElementById('body').style.overflow='hidden';
    document.getElementById('html').style.overflow='hidden'; //IE
  }
  
  this.finishLoading = function ()
  {
    if(startedLoading){
      addClass($("loading"), "hidden");
      
      /* Implementação */
      document.getElementById('body').style.overflow='auto';
      document.getElementById('html').style.overflow='auto'; //IE
    }
  }









	///////////////////////////////////////////////////
	// Funcao que trabalha os dados para serem enviados na requisicao
	///////////////////////////////////////////////////

	this.getDados = function(obj)
	{
		if(obj == null) return null;
		
		if(typeof(obj) == "string")
		{
			return obj;
		}
		if(obj.nodeName.toLowerCase() == "form")
		{
			var is, ss, ts, i;
			var s = "";
			var v = new Array();
			
			is = $tags('input',obj);
			ss = $tags('select',obj);
			ts = $tags('textarea',obj);
			
			for(i=0; i<ts.length; i++)
			{
				if(!ts[i].disabled) v.push([ts[i].name,ts[i].value]);
			}
			
			for(i=0; i<is.length; i++)
			{
				if(is[i].type == "text" && !is[i].disabled) v.push([is[i].name,is[i].value]);
				if(is[i].type == "password" && !is[i].disabled) v.push([is[i].name,is[i].value]);
				if(is[i].type == "hidden" && !is[i].disabled) v.push([is[i].name,is[i].value]);
				if((is[i].type == "radio" || is[i].type == "checkbox") && is[i].checked  && !is[i].disabled) v.push([is[i].name,is[i].value]); 
			}
			
			for(i=0; i<ss.length; i++)
			{
				if(ss[i].selectedIndex == -1) v.push(ss[i].name,'');
				else v.push(new Array(ss[i].name,ss[i].options[ss[i].selectedIndex].value));
			}
			
			for(i=0; i<v.length; i++)
			{
				if(this.isUrlEscaped) s += encodeURIComponent(v[i][0])+"="+encodeURIComponent(v[i][1])+"&";
				else s += v[i][0]+"="+v[i][1]+"&";
			}
			
			return s;
		}
	}




}

