function getXMLHttpRequestCrossBrowser()
{
	var XHR = null;
	
	userBrowser = navigator.userAgent.toUpperCase();
	
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
	{
		XHR = new XMLHttpRequest();
	}
	else if(window.ActiveXObject && (userBrowser.indexOf("MSIE4") < 0))
	{
		if(userBrowser.indexOf("MSIE5") < 0)
		{
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		}
		else
		{
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return XHR
}

function load(link, fileName, div)
{
	var XHR = getXMLHttpRequestCrossBrowser();
	var xml = null;
	
	if(XHR)
	{
		XHR.open("GET", fileName, true);
		XHR.setRequestHeader("connection", "close");
		XHR.onreadystatechange = function()
		{
			if(XHR.readyState == 4)
			{
				if(XHR.status == 200)
				{
					createWork(div, XHR.responseXML, link);
				}
			}
		}
		XHR.send(null);
	}
	else
	{
		alert("Il browser in uso non supporta ajax! Devi abilitare javascript nel tuo browser o utilizzarne uno pił recente al fine di visualizzare correttamente il sito");
	}
}


function createWork(div, xml, id)
{
	var works = xml.getElementsByTagName("work");
	var title = "", description = ""; imgSrc = "";
	
	//alert(works.length);
	//alert("nodeValue: " + works[4].attributes.item(0).nodeValue + "\n" + "id: " + id);
	for(i = 0; i < works.length; i++)
	{
		//alert("nodeValue: " + works[i].attributes.item(0).nodeValue + "\n" + "id: " + id + "\n" + i + " su " + works.length);
		if(works[i].attributes.item(0).nodeValue == id.href)
		{
			//alert(id.href);
			work = works[i];
			break;
		}
	}
	//alert("ciao");
	
	elements = work.childNodes;
	for(i = 0; i < elements.length; i++)
	{
		if(elements[i].tagName == "screen")
		{
			if(elements[i].textContent)
				imgSrc = elements[i].textContent;
			else
				imgSrc = elements[i].text;
		}
		else if(elements[i].tagName == "title")
		{
			if(elements[i].textContent)
				title = elements[i].textContent;
			else
				title = elements[i].text;
		}
		else if(elements[i].tagName == "description")
		{
			if(elements[i].textContent)
				description = elements[i].textContent;
			else
				description = elements[i].text;
		}
	}
	
	div.removeChild(div.firstChild);
	
	/* Creazione del link di chiusura */
	closeLink = document.createElement("a");
	closeLink.setAttribute("id", "closeLink");
	closeLink.setAttribute("href", "javascript: closeWork();");
	closeText = document.createTextNode("Chiudi (x)");
	closeLink.appendChild(closeText);
	
	/* Creazione del titolo */
	titleSpan = document.createElement("span");
	titleSpan.setAttribute("id", "showDivTitle");
	titleText = document.createTextNode(title);
	titleSpan.appendChild(titleText);
	
	if(imgSrc != "")
	{
		/* Creazione dell'immagine */
		imgLink = document.createElement("a");
		imgLink.setAttribute("href", id.href);
		img = document.createElement("img");
		img.setAttribute("src", imgSrc);
		imgLink.appendChild(img);
	}
	
	/* Creazione della descrizione */
	descriptionSpan = document.createElement("div");
	descriptionSpan.setAttribute("id", "showDivDescription");
	descriptionText = document.createTextNode(description);
	descriptionSpan.appendChild(descriptionText);
	
	div.appendChild(closeLink);
	div.appendChild(titleSpan);
	if(imgSrc != "")div.appendChild(imgLink);
	div.appendChild(descriptionSpan);
}

function closeWork()
{
	work = document.getElementById("showDiv");
	document.body.removeChild(work);
	prepareLinks();
}
