////////////////////////////////////////
// FONCTIONS XMLHTTPREQUEST
////////////////////////////////////////
var req;
var xmlUrl = "spip.php";
var startReq, endReq;
//var loading = new Image();
//	loading.src = "img/loading.gif";

function GnooCom(n)
{
	this.name = n;
	this.xmlUrl = "spip.php";
	this.req = false;
	this.loaded = new Array();
	this.multiAct = new Array();
	this.a = new Array(); // liste des liens
	this.param = "";	// paramêtres de la requête
	this.target = null; // div de destination
	this.current=0;
	///////////////////////////////
	// constructeur
	///////////////////////////////
	this.init = function()
	{
		this.a = document.getElementsByTagName("A");
//		if(this.req)
//			this.req.abort();
		if(window.XMLHttpRequest)
		{
			try 
			{
				this.req = new XMLHttpRequest();
			} 
			catch(e)
			{
				this.req = false;
			}
			// branch for IE/Windows ActiveX version
		}
		else if(window.ActiveXObject)
		{
			try
			{
				this.req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try 
				{
					this.req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) 
				{
					this.req = false;
				}
			}
		}
		return this.req;
	}
	///////////////////////////////
	// chargement de XML
	///////////////////////////////
	this.loadMultiXML = function(tg)
	{
		//alert(tg)
		this.multi = true;
		this.multiAct = tg;
		this.current=0;
		this.loadXML(this.multiAct[this.current][0], this.multiAct[this.current][1]);
	}
	///////////////////////////////
	// chargement de XML
	///////////////////////////////
	this.loadXML = function(id, tg)
	{
		//alert(tg)
		var tmp = null;
		this.target = tg+id;
		//	alert(this.current);
		// SI deja affiche -> efface
		tmp = document.getElementById(tg+id);
		for(var i=0; i<this.loaded.length; i++)
		{
			if(this.loaded[i]["type"]==tg && this.loaded[i]["target"]==id )
			{
				this.hideData(this.loaded[i]["type"]+this.loaded[i]["target"]);
				this.loaded.splice(i, this.loaded.length-i);
				return;
			}
		}
		if(tmp.style.display == 'block')
		{
//			tmp.style.display == 'none';
			this.hideData(tg+id);
			return;
		}
		this.init();
		this.loaded[this.loaded.length] = {type:tg, target:id};
		if(this.req)
		{
			self.document.body.style.cursor = 'wait';
			for(var j=0; j<this.a.length; j++)
			{
				this.a[j].style.cursor = 'wait';
			}
			var tmp = document.getElementById(this.target);
			if(tmp)
			{
				tmp.innerHTML = "Chargement...<img src=\"images/loading.gif\" />";
				tmp.style.display = "block";
				tmp.style.visibility = "visible";
				tmp.style.cursor = 'wait';
			}
			this.param = "page=xml&id_rubrique="+id+"&type="+tg+"&toto="+Math.random (10000);
			this.req.abort();
			this.req.onreadystatechange = new Function("", this.name+".getXML();");
			this.req.open("GET", this.xmlUrl+"?"+this.param, true);
//			this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.req.send(null);//this.param
		}
		return this.req;
	}
	///////////////////////////////
	// reception de XML
	///////////////////////////////
	this.getXML = function()
	{
		// only if req shows "loaded"
		if (this.req.readyState == 4) 
		{
			// only if "OK"
			if (this.req.status == 200) 
			{
				this.showData();
			}
			else
			{
//				alert("There was a problem retrieving the XML data:\n"+this.req.statusText);
			}
		}
	}
	///////////////////////////////
	// affichage du résultat
	///////////////////////////////
	this.showData = function(tg)
	{
		var tmp = document.getElementById(this.target);
		if(tmp)
		{
			tmp.innerHTML = this.req.responseText;
			tmp.style.display = "block";
			tmp.style.visibility = "visible";
			self.document.body.style.cursor = 'default';
			for(var j=0; j<this.a.length; j++)
			{
				this.a[j].style.cursor = 'pointer';
			}
			tmp.style.cursor = 'default';
			if(this.multi)
			{
				if(this.current<this.multiAct.length-1)
				{
					this.init();
					this.current++;
//					alert(this.target+" "+this.current+" "+this.multiAct[this.current][0]+" , "+this.multiAct[this.current][1]);
					
					this.loadXML(this.multiAct[this.current][0], this.multiAct[this.current][1]);
				}
				else
				{
					this.multi=false;
				}
			}
		}
		else
			alert("destination inconnue : "+this.target);
	}
	///////////////////////////////
	// masquage du résultat
	///////////////////////////////
	this.hideData = function(tg)
	{
		var tmp = document.getElementById(tg);
		if(tmp)
		{
			tmp.innerHTML = "";
			tmp.style.display = "none";
			tmp.style.visibility = "hidden";
		}
		else
			alert("destination inconnue");
	}
	///////////////////////////////
	// annulation de la requête
	///////////////////////////////
	this.cancel = function()
	{
		this.req.abort();
	}
	///////////////////////////////
	return this;
///////////////////////////////
} // end class
///////////////////////////////
// instanciation
var GC = new GnooCom("GC");

function setDisplay(id,type)
{
	if(GC)
		GC.loadXML(id,type);
	return false;
}

function setMultiDisplay(data)
{
	if(GC)
		GC.loadMultiXML(data);
	return false;
}

///////////////////////////////
