
isExpanded = false;
var selectedItem = null;
var selectedCutItem = -1;
var doUserAction;
var singleBranch = false;
var treeControlId = "";
var selectedRoot = null;
var rootChildResize = false;
var treeResize = false;
var selectFullPath = false;
var treeReady = false;

function SetState()
{
		var tempColl = document.getElementsByTagName("div");
		var state="";
		for (i=0; i<tempColl.length; i++) {
			if (tempColl[i].className == "child" && tempColl[i].style.display != "none")
			{
				state += tempColl[i].id
			}
		}
		if (document.forms[0].action.indexOf('?')>-1)
		{
			document.forms[0].action += '&';
		}	
		else
		{
			document.forms[0].action += '?';
		}
		document.forms[0].action += 'state='+state;
}

function Reload(newId)
{
	SetState();
	if (newId)
	{
		/* open current selection after reload */
		//document.forms[0].action += 'm'+newId+'C';
		/* select newnode after reload */
		document.getElementById('selectedNode').value = newId;
	}
	document.forms[0].submit();
}

function mouseOver(e)
{
	var relTarg;
	if (!e) var e = window.event;
	if (e.target) relTarg = e.target;
	else if (e.toElement) relTarg = e.toElement;

	var oObject = relTarg;
	while (!changeClass(oObject,'treelabel','hoover') && oObject && oObject.tagName!='DIV')
	{
		oObject = (oObject.parentNode) ? oObject.parentNode : oObject.parentElement;
	}
}

function changeClass(oObject,oldClassName,newClassName)
{
	if(oObject)
	{
		if (oObject.className.indexOf('root') > 0)
		{
			oldClassName += 'root';
			newClassName += 'root';
		}

		if (oObject.className==oldClassName)
		{
			oObject.className=newClassName;
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function mouseLeave(e)
{
	var relTarg;
	if (!e) var e = window.event;
	if (e.target) relTarg = e.target;
	else if (e.fromElement) relTarg = e.fromElement;

	var oObject = relTarg;

	var className = "treelabel";
	if (selectedItem)
	{
		if (GetItemID(oObject) == selectedItem)
		{
			className="highlight"
		}
	}
	while (!changeClass(oObject,'hoover',className) && oObject && oObject.tagName!='DIV')
	{
		oObject = (oObject.parentNode) ? oObject.parentNode : oObject.parentElement;
	}
	//window.event.fromElement.className=className;
}

function sNode(id)
{
	if (document.getElementById('m'+selectedItem+'A'))
	{
		/* reset selected item */
		if (document.getElementById('m'+selectedItem+'A').className == "highlight")
		{
			document.getElementById('m'+selectedItem+'A').className = "treelabel";
		}
		else if (document.getElementById('m'+selectedItem+'A').className == "highlightroot")
		{
			document.getElementById('m'+selectedItem+'A').className = "treelabelroot";
		}		
	}
	if (!id)
	{
		selectedItem = this.id.substr(1,this.id.length-2);
		if (this.className.indexOf('root') > 0)
		{
			this.className="highlightroot";
		}
		else
		{
			this.className="highlight"
		}		
	}
	else
	{
		if (document.getElementById('m'+id+'A'))
		{
			if (document.getElementById('m'+id+'A').className.indexOf('root') > 0)
			{
				document.getElementById('m'+id+'A').className="highlightroot";
			}
			else
			{
				document.getElementById('m'+id+'A').className="highlight"
			}			
			selectedItem = id;
		}
	}
	resizeRootChild(null,true);
	
}

function doTreeOnClick(e)
{
	var oObject;
	if (!e) var e = window.event;
	if (e.target) oObject = e.target;
	else if (e.srcElement) oObject = e.srcElement;

	if (!oObject.id)
	{
		oObject = oObject.parentElement;
	}
	else if (GetItemID(oObject)==-1 || !GetItemID(oObject))
	{
		oObject = oObject.parentElement;
	}

	SelectFolder(GetItemID(oObject),true);
	//sNode(GetItemID(oObject));
	doUserAction(selectedItem);
}

function resizeRootChild(e, clicked)
{
	var doResize = false;
	if (clicked)
	{
		var oObject = document.getElementById("m" + selectedItem + "P");
		while (oObject && oObject.className && oObject.className.substr(oObject.className.length-4,4) != "root")
		{
			oObject = (oObject.parentNode) ? oObject.parentNode : oObject.parentElement;
		}
		if (oObject && oObject.className)
		{
			if (selectedRoot != oObject)
			{
				doResize = true;
			}
			selectedRoot = oObject;
		}
	}
	else if (!clicked && treeResize)
	{
		Resize(treeControlId);
		doResize = true;
	}
	if (doResize && rootChildResize && selectedRoot)
	{
		var parentItem = (selectedRoot.parentNode) ? selectedRoot.parentNode : selectedRoot.parentElement;
		var tempColl = parentItem.childNodes;
		if (tempColl)
		{
			var selectedId = GetItemID(selectedRoot);
			var height = 0;
			for (i=0; i<tempColl.length; i++) 
			{
				if (tempColl[i].id && tempColl[i].id.substr(tempColl[i].id.length-1, 1) == "P" && tempColl[i].id.substr(0,1) == "m")
				{
					height += tempColl[i].offsetHeight;
				}
			}
			var parHeight = document.getElementById(treeControlId).style.height.substr(0,parentItem.style.height.length-2);
			var oContainer = document.getElementById("m" + selectedId+"C");
			if (oContainer)
			{
				if (parHeight-height > 0)
				{
					oContainer.style.height = parHeight-height;
				}
				else
				{
					oContainer.style.height = 0;
				}
			}	
		}
	}	
}

function doTreeOnDblClick(e)
{
	var oObject;
	if (!e) var e = window.event;
	if (e.target) oObject = e.target;
	else if (e.srcElement) oObject = e.srcElement;
	//SelectFolder(GetItemID(oObject),true);
	FolderExpand('m'+GetItemID(oObject));
	doUserAction2(selectedItem);
}

function getIndex($1) {
	ind = null;
	for (i=0; i<document.layers.length; i++) {
		whichEl = document.layers[i];
		if (whichEl.id == $1) {
			ind = i;
			break;
		}
	}
	return ind;
}

function arrange() {
	nextY = document.layers[firstInd].pageY + document.layers[firstInd].document.height;
	for (i=firstInd+1; i<document.layers.length; i++) {
		whichEl = document.layers[i];
		if (whichEl.visibility != "hide") {
			whichEl.pageY = nextY;
			nextY += whichEl.document.height;
		}
	}
}

function SelectFolder(id, isevent)
{
	if (isevent || id != selectedItem)
	{
		sNode(id);
		ExpandBranche(id);
		if (id != selectedItem && id != getIdFromUrl())
		{
			//node not found, try server-side reload
			reloadTree(id);
		}
	}
}


function getIdFromUrl()
{
	found = document.location.href.toLowerCase().indexOf('selectedid=');
	if (found>0)
	{
		found += 11;
		end = document.location.href.indexOf('&',found);
		if (end<0)
		{
			end = document.location.href.length;
		}
		return document.location.href.substring(found,end);
	}
}

function reloadTree(id)
{
	found = getIdFromUrl();
	if (found)
	{
		re = 'selectedid='+found;
		newUrl = document.location.href.toLowerCase().replace(re,'selectedid='+id);
	}
	else
	{
		if (document.location.href.indexOf('?')>0)
		{
			newUrl = document.location.href+'&selectedid='+id;
		}	
		else
		{
			newUrl = document.location.href+'?selectedid='+id;
		}
	}
	document.location.href = newUrl;
}

function ExpandBranche(id)
{
	OpenFolder('m'+id);
	var parentId = GetParentId(id);
	if (parentId)
	{
		if (selectFullPath)
		{
			sNode(parentId);
		}
		ExpandBranche(parentId);
	}
}

function FolderInit(id,userAction,userAction2,initId,doAction, isItemTree)
{
	//alert('id: '  + id + ' userAction: ' + userAction + ' userAction2: ' + userAction2 + ' initId: ' + initId + ' doAction: ' + doAction + ' isItemTree: ' + isItemTree)
	treeControlId = id;

	if (userAction)
	{
		doUserAction = new Function("id",userAction);
	}
	else
	{
		doUserAction = new Function("id","return;");
	}
	
	if (userAction2)
	{
		doUserAction2 = new Function("id",userAction2);
	}
	else
	{
		doUserAction2 = new Function("id","return;");
	}
	tempColl = document.getElementById(id).getElementsByTagName("div");
	/* tempColl = document.all.tags("DIV"); */
	for (i=0; i<tempColl.length; i++) {
		if ((isItemTree == null || (isItemTree != null && isItemTree == false)) && (tempColl[i].className == "child" || tempColl[i].className == "childroot")) 
		{
			tempColl[i].style.display = "none";
		}
		else
		{
			tempColl[i].noWrap=true;
		}
	}
	tempColl = document.getElementById(id).getElementsByTagName("SPAN");
	/* tempColl = document.all.tags("DIV"); */
	for (i=0; i<tempColl.length; i++) {
		if (tempColl[i].id)
		{
			tempColl[i].onclick = doTreeOnClick;
			tempColl[i].onmouseover = mouseOver;
			tempColl[i].onmouseout = mouseLeave;	
			tempColl[i].ondblclick = doTreeOnDblClick;
		}
	}
	//restore prior selection
	if (initId)
	{
		var link = document.getElementById('m'+initId+'A');
		if (doAction && link && link.childNodes.length > 0 && link.childNodes[0].tagName == 'A')
		{
			link.childNodes[0].click();
		}
		SelectFolder(initId, true);
	}
	if (doAction && initId)
	{
		doUserAction(initId);
	}
	document.getElementById(id).style.display='inline';
	if (treeResize || rootChildResize)
	{
		window.onresize = resizeRootChild;
		resizeRootChild();
	}
	treeReady = true;
}



function FolderExpand($1) {
	ExpandChild = document.getElementById($1 + "C");
	if (!ExpandChild) return;
	ExpandTree = document.getElementById($1 + "T");
	ExpandFolder = document.getElementById($1 + "F");
	if (ExpandChild.style.display == "none") {
		ExpandChild.style.display = "block";
		CloseSibling($1);
		if (ExpandTree)
		{
			ExpandTree.src = ExpandTree.src.replace(/plus/i,'minus'); 
		}
		if (ExpandFolder)
		{
			ExpandFolder.src = ExpandFolder.src.replace(/_C/i,'_O'); 
		}
	}
	else 
	{
		ExpandChild.style.display = "none";
		if (ExpandTree)
		{
			ExpandTree.src = ExpandTree.src.replace(/minus/i,'plus'); 
		}
		if (ExpandFolder)
		{
			ExpandFolder.src = ExpandFolder.src.replace(/_O/i,'_C'); 
		}
	}
}

function CloseSibling($1)
{
	if (singleBranch)
	{
		var parentItem
		parentItem = (ExpandChild.parentNode) ? ExpandChild.parentNode : ExpandChild.parentElement;

			
		// close other childs in parent folder
		var tempColl = parentItem.childNodes;
		for (i=0; i<tempColl.length; i++) 
		{
			if (tempColl[i].id)
			{
				var tempid = tempColl[i].id.substr(0, tempColl[i].id.length-1);
				if (tempid != $1)
				{
					CloseFolder(tempid);
				}
			}
		}

	}	
}

function CloseFolder($1) {
	var ExpandChild = document.getElementById($1 + "C");
	if (!ExpandChild) return;
	var ExpandTree = document.getElementById($1 + "T");
	var ExpandFolder = document.getElementById($1 + "F");
	if (ExpandChild.style.display == 'block') {
		ExpandChild.style.display = 'none';
		if (ExpandTree)
		{
			ExpandTree.src = ExpandTree.src.replace(/minus/i,'plus'); 
		}
		if (ExpandFolder)
		{
			ExpandFolder.src = ExpandFolder.src.replace(/_O/i,'_C'); 
		}
	}
}

function OpenFolder($1) {
	ExpandChild = document.getElementById($1 + "C");
	if (!ExpandChild) return;
	
	CloseSibling($1);
	
	ExpandTree = document.getElementById($1 + "T");
	ExpandFolder = document.getElementById($1 + "F");
	if (ExpandChild.style.display == "none") {
		ExpandChild.style.display = "block";
		if (ExpandTree)
		{
			ExpandTree.src = ExpandTree.src.replace(/plus/i,'minus'); 
		}
		if (ExpandFolder)
		{
			ExpandFolder.src = ExpandFolder.src.replace(/_C/i,'_O'); 
		}
	}
}

function showAll() {
	for (i=firstInd; i<document.layers.length; i++) {
		whichEl = document.layers[i];
		whichEl.visibility = "show";
	}
}


function GetItemID(obj)
{
	var itemId = -1;
	if (obj)
	{
		if (obj.id)
		{
			itemId = parseInt(obj.id.substr(1,obj.id.length-2));
		}
		else
		{
			if (obj.parentNode)
			{
				return GetItemID(obj.parentNode);
			}
		}
	}
	return itemId;
}

function GetParentId(id)
{
	var currentNode = document.getElementById('m'+id+'P')
	if (currentNode)
	{
		if (currentNode.parentNode)
		{
			return GetItemID(currentNode.parentNode);
		}
	}
}

function GetCurrentItemName()
{
	var currentNode = document.getElementById('m'+selectedItem+'A')
	if (currentNode)
	{
		return currentNode.innerText;
	}
}

function GetPreviousItemID(id)
{
	var currentNode = document.getElementById('m'+id+'P')
	if (currentNode.previousSibling)
	{
		return GetItemID(currentNode.previousSibling);
	}
}

function GetNextItemID(id)
{
	var currentNode = document.getElementById('m'+id+'P')
	if (currentNode.nextSibling)
	{
		if (GetItemID(currentNode.nextSibling) == id)
		{
			if(currentNode.nextSibling.nextSibling)
			{
				return GetItemID(currentNode.nextSibling.nextSibling);
			}
		}
		else
		{
			return GetItemID(currentNode.nextSibling);
		}
	}
}



function ReplaceImage(inputstring,image)
{
	return inputstring.substr(0,inputstring.lastIndexOf('/')+1)+image;
}