/*	Alessio Cartocci
	IWA Italy  (c)2001-2008
--------------------------------*/
			

window.onload = function() {
	init();
};

function getKey(e) {
    return e.keyCode;
}



/*  browser handler
-----------------------*/

uA = navigator.userAgent.toLowerCase();
aV = navigator.appVersion;
isMac = uA.indexOf("mac");
isLinux = uA.indexOf("linux");

function detect(browser) {
	browser = browser.toLowerCase();
	ver = 0;
	switch (browser) {
		case "ie":
			ver = parseFloat(aV.split("MSIE")[1]);
			break;
		case "opera":
			if (window.opera) ver = aV.substr(0,3);
			break;
		case "ns":
			ver = parseFloat(uA.split("Netscape/"))
			break;
		case "khtml":
			if (uA.indexOf("khtml")!=-1) ver = 1;
			else ver = 0;
			break;
	}
	return ver;
}



/*  windows handler
-----------------------*/

var moz = document.getElementById && !document.all;
var sel, isDragged = false;
var x, y, _obj, obj, tmpX, tmpY;
			
function init() {
	icn = document.getElementById('icons').getElementsByTagName('a');
	for (var a=0; a<icn.length; a++) {
		if (icn[a].className=="folder") icn[a].className = "folder draggable";
		else {
			icn[a].className = "draggable";
			icn[a].style.backgroundImage = "url(images/"+ icn[a].innerHTML.toLowerCase().replace(/\ /g,'') +".gif)";
		}
		//if (icn[a].offsetHeight>50) { // CSS enabled
			icn[a].onclick = function() {return false;};
			icn[a].onkeypress = function(e) {if (getKey(window.event||e)==13) create(this);};
			icn[a].ondblclick = function() {create(this);};
		//}
	}
	objEl = document.getElementsByTagName('div');
	for (var i=0; i<objEl.length; i++) {
		if (objEl[i].className.indexOf('window')>-1) {
			objEl[i].style.position = "absolute";
			objEl[i].style.left = "250px";
			objEl[i].style.top = "-10px";
			if (objEl[i].className.indexOf('window')>-1) {
				objEl[i].style.zIndex = i;
				objH = objEl[i].getElementsByTagName('h3')[0];
				objFoot = objEl[i].getElementsByTagName('span')[0];
				objH.ondblclick = function() {enlarge(this.getElementsByTagName('a')[1],false);};
				if (window.opera) objH.style.width = objFoot.style.width = "auto";
			}
		}
	}
}

function move(e) {
	if (isDragged) {
		if (obj.className.indexOf('window')>-1 || obj.className.indexOf('draggable')>-1) {
			try {
		 		obj.style.left = moz ? tmpX + e.clientX - x + "px" : tmpX + event.clientX - x + "px";
				obj.style.top  = moz ? tmpY + e.clientY - y + "px" : tmpY + event.clientY - y + "px";
			}
			catch (e) {}
		}
		else {
			obj.parentNode.style.width = moz ? tmpX + e.clientX - x + "px" : tmpX + event.clientX - x + "px";
			obj.parentNode.style.height  = moz ? tmpY + e.clientY - y + "px" : tmpY + event.clientY - y + "px";
		}
		sel = true;
	}
	return false;
}

function select(e) {
	_obj = moz ? e.target : event.srcElement;
	if(_obj.className.indexOf('draggable')>-1) obj = _obj;
	else obj = _obj.parentNode;
	try {
		if(_obj.className=="wfoot") {
			if (obj.className.indexOf('window')>-1) {
				if (detect("ie")>0) obj.getElementsByTagName('h3')[0].className = "";
				else obj.getElementsByTagName('h3')[0].removeAttribute('class');
				zInd = parseInt(obj.style.zIndex);
				resetZindex(true);
				obj.style.zIndex = 999;
			}
			return false;
		}
		if (obj.className.indexOf('window')>-1 || obj.className.indexOf('draggable')>-1 || _obj.className=="wh") {					
			isDragged = true;
			if (sel==false) {
				tmpX = 0;
				tmpY = 0;
			}
			else {
				if (obj.className.indexOf('window')>-1 || obj.className.indexOf('draggable')>-1) {
					tmpX = parseInt(obj.style.left);
					tmpY = parseInt(obj.style.top);
				}
				else {
					tmpX = parseInt(obj.parentNode.style.width);
					tmpY = parseInt(obj.parentNode.style.height);
				}
			}
			x = moz ? e.clientX : event.clientX;
			y = moz ? e.clientY : event.clientY;
			if (obj.className.indexOf('window')>-1) {
				zInd = parseInt(obj.style.zIndex);
				resetZindex(true);
				obj.getElementsByTagName('h3')[0].className = "void";
				obj.style.zIndex = 999;
			}
			document.onmousemove = move;
		}
		return false;		
	}
	catch (e) {}
}

document.onmousedown = select;

document.onmouseup = document.onrelease = function() {
	isDragged = false;
	document.onmousemove = null;
}
			
function resetZindex(cl) {
	objTag = document.getElementsByTagName('div');
	for (i=0; i<objTag.length; i++) {
		if (objTag[i].className.indexOf('window')>-1) {
			zOri = parseInt(objTag[i].style.zIndex);
			if (cl) {
				if (cl==true) objTag[i].getElementsByTagName('h3')[0].className = "unsel";
				if (zOri<999) objTag[i].className = zOri + " window";
				else {
					objTag[i].style.zIndex = parseInt(objTag[i].className.split(' ')[0]);
					objTag[i].className = "window";
				}
			}
			else {
				objTag[i].style.zIndex = i;
				objTag[i].className = "window";
			}
		}
	}
}

function enlarge(el,ic) {
	windw = el.parentNode.parentNode;
	wCoords = parseInt(windw.style.width) + "_" + parseInt(windw.style.height) + "_" + parseInt(windw.style.top) + "_" + parseInt(windw.style.left);
	if (el.className=="wr" || el.className=="wm") {
		wClass = el.className;
		el.className = wCoords + " " + wClass;

	if (wClass=="wr") {
		if (detect('ie')>0)	{
			document.getElementsByTagName('html')[0].style.overflow = "hidden";
			windw.style.height = document.documentElement.clientHeight -45 + "px";
			windw.getElementsByTagName('iframe')[0].setAttribute('height', windw.style.height);
		}
		else {
			document.body.style.overflow = "hidden";
			windw.style.height = window.innerHeight - 45 + "px";
		}
		windw.style.width = document.body.offsetWidth - 8 + "px";
		windw.style.left = "0px";
		windw.style.top = 0 - parseInt(document.getElementById('head').offsetHeight) + "px";
	}
	else {
		windw.style.width = "150px";
		windw.style.height = "12px";
		windw.style.left = "0px";
		windw.style.top = "400px";
		windw.style.bottom = "0px";
	}

		windw.style.marginTop = "-1em";
		windw.style.marginLeft = "-4.3%";
		whs = windw.getElementsByTagName('img');
		for (i=0; i<whs.length; i++) {
			if (whs[i].className=="wh") {
				whs[i].style.visibility = "hidden";
			}
		}
		document.onmousedown = null;
	}
	else {
		cls = el.className;
		pos = cls.split('_');
		el.className = wClass;
		windw.style.width = pos[0] + "px";
		windw.style.height = pos[1] + "px";
		windw.style.top = pos[2] + "px";
		windw.style.left = pos[3].substr(0, pos[3].length-3) + "px";
		windw.style.marginTop = windw.style.marginLeft = "0px";
		if (detect('ie')>0)	{
			windw.getElementsByTagName('iframe')[0].setAttribute('height', "100%");
			document.getElementsByTagName('html')[0].style.overflow = "";
		}
		else document.body.style.overflow = "auto";
		whs = windw.getElementsByTagName('img');
		for (i=0; i<whs.length; i++) {
			if (whs[i].className=="wh") {
				whs[i].style.visibility = "visible";
			}
		}
		document.onmousedown = select;
	}
}

function create(ic) {
	id = ic.innerHTML.toLowerCase().replace(/\ /g,'');
	windw = document.getElementById(id);
	windw.style.display = "block";
	resetZindex();
	windw.getElementsByTagName('h3')[0].className = "sel";
}

function destroy(ic) {
	id = ic.parentNode.parentNode.id;
	windw = document.getElementById(id);
	resetZindex();
	windw.style.display = "none";
}


