/*

webFinder javascript - gets the content of a directory
and displays it in a "Mac OS X Finder column view like" style

copyright 2005 lixlpixel - http://www.lixlpixel.org/

*/

var xmlhttp;
var currentdiv;

// connect to the server
function loadXMLDoc(url,what)
{
	// code for Mozilla, etc.
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	// code for IE
	}else if(window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp)
		{
			if(what == "list") xmlhttp.onreadystatechange=xmlhttpChange;
			else if(what == "show") xmlhttp.onreadystatechange=xmlhttp2Change;
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
		}
	}
}

// handle the server connection
function xmlhttpChange()
{
	// if xmlhttp shows "loaded"
	if(xmlhttp.readyState==4)
	{
		// if "OK"
		if(xmlhttp.status==200)
		{
			currentdiv.innerHTML = xmlhttp.responseText;
			var nodes = currentdiv.getElementsByTagName("li")
			var max = nodes.length;
			var s = document.getElementById("status");
			s.innerHTML = s.innerHTML + (max > 0 ? " &#63743; " + max + " item" + (max > 1 ? "s" : "") : "");
		}else{
			currentdiv.innerHTML = "Problem retrieving directory data";
		}
	}
}

// handle the server connection
function xmlhttp2Change()
{
	var viewfilediv = document.getElementById("viewfile");
	// if xmlhttp shows "loaded"
	if(xmlhttp.readyState==4)
	{
		// if "OK"
		if(xmlhttp.status==200)
		{
			viewfilediv.innerHTML = xmlhttp.responseText;
		}else{
			viewfilediv.innerHTML = "Problem retrieving file data";
		}
	}
}

// create new element
function newelement(newid)
{ 
	if(document.createElement)
	{ 
		var el = document.createElement('div'); 
		el.id = "level" + newid;
		el.innerHTML = '&nbsp;'; 
		document.getElementById("finder").appendChild(el);
	} 
}

// set the current column
function setdiv(what)
{
	var d = document.getElementById("finder");
	if(!document.getElementById("level" + what))
	{
		newelement(what);
		d.style.width = (what * 14) + "em";
	}
	currentdiv = document.getElementById("level" + what);
	var kids = d.childNodes;
	var numkids = kids.length;
	for(var i = what+1; i < numkids-1; i++)
		d.removeChild(document.getElementById("level" + i));
	if(what * 14 > 40)
	{
		d.style.width = (what * 14) + "em";
		d.style.position.left = "-" + ((what * 14) - 10) +"em";
	}else{
		d.style.width = "40em";
		d.style.position.left = "0em";
	}
}

// initiate the connection
function show(what)
{
	var d = new Date()
	loadXMLDoc("server.php?mode=" + what + "&r=" + d, "list");
}

// initiate the connection
function viewfile(what)
{
	currentdiv = document.getElementById("viewfile");
	currentdiv.style.display = "block";
	var d = new Date()
	loadXMLDoc("server.php?view=" + what + "&r=" + d, "show");
}

// close a window
function closefile(what)
{
	currentdiv = document.getElementById(what);
	currentdiv.style.display = "none";
}

// select the current item
function select(what)
{
	var nodes = document.getElementsByTagName("li")
	var max = nodes.length;
	for(var i = 0;i < max; i++)
	{
		var nodeObj = nodes.item(i);
		if(nodeObj.className == "selected" || nodeObj.className == "path")
			nodeObj.className = "none";
	}
	var theoldpath = "";
	var thepath = what.split("/");
	var maxp = thepath.length;
	for(var i = 0;i < maxp; i++)
	{
		if(i == maxp-1)
			var thenewpath = theoldpath + thepath[i];
		else
			var thenewpath = theoldpath + thepath[i] + "/";
		var theoldpath = thenewpath;
		if(thenewpath == what)
			document.getElementById(thenewpath).className = "selected";
		else
			document.getElementById(thenewpath).className = "path";
	}
	document.getElementById("status").innerHTML = what;
}

