/*
	Title: Xtree - CSS/JavaScript Directory Tree
	Author: Gusts 'gusC' Kaksis
	e-mail: gusc@grafton.lv
	www: http://gusc.orb.lv/
	Version: 0.2 alpha
	Date: 2007-03-21
	License: BSD
*/

/*
	holderClick function
*/
clickXTreeHolder = function(oLi){
	for (a = 0; a < oLi.childNodes.length; a ++){
		if (oLi.childNodes[a].nodeName.toLowerCase() == 'ul'){
			var oUl = oLi.childNodes[a];
		}
		if (oLi.childNodes[a].nodeName.toLowerCase() == 'a'){
			var aClass = oLi.childNodes[a].className;
			if(aClass == 'open' || aClass == 'closed'){
				var oA = oLi.childNodes[a];
			}
		}
	}
	if(typeof oUl != 'undefined'){
		if (oUl.className == 'open'){
			oUl.className = 'closed';
		} else {
			oUl.className = 'open';
		}
	}
	if(typeof oA != 'undefined'){
		if (oA.className == 'open'){
			oA.className = 'closed';
		} else {
			oA.className = 'open';
		}
	}
}
/*
	Set class="last" on last LI
*/
setXTreeLast = function(oUl){
	for (b = 0; b < oUl.childNodes.length; b ++){
		if (oUl.childNodes[b].nodeName.toLowerCase() == 'li'){
	    var lastLi = oUl.childNodes[b];
		}
	}
	if (typeof lastLi != 'undefined'){
   	lastLi.className = 'last';
	}
}
/*
	Initialize Tree Row (LI)
*/
initXTreeRow = function(oLi,params){
	var oHolder = null;
	var oItem = null;
	for (b = 0; b < oLi.childNodes.length; b ++){
		if (oLi.childNodes[b].nodeName.toLowerCase() == 'ul'){
	    oHolder = oLi.childNodes[b];
		}
		if (oLi.childNodes[b].nodeName.toLowerCase() == 'a'){
	    oItem = oLi.childNodes[b];
		}
	}
	if (typeof oHolder != 'undefined' && oHolder != null){
		var oA = document.createElement('a');
		oA.className = params['default-condition'];
		oA.onclick = function (){
			clickXTreeHolder(this.parentNode);
		};
		oLi.insertBefore(oA,oLi.firstChild);
	  oHolder.className = params['default-condition'];
	  if (params['set-class'] == true){
	  	oItem.className = params['holder-class'];
		}
	} else if (typeof oItem != 'undefined' && oItem != null){
		if (params['set-class'] == true){
			oItem.className = params['item-class'];
		}
	}
}
/*
	Initialization function
*/
initXTree = function(id,params){
	if (typeof id == 'undefined'){
		alert('Xtree ID not specified!');
		return false;
	}
	if (typeof params == 'undefined'){
   	alert('Xtree parameters not specified!');
   	return false;
	}
	var oUl = document.getElementById(id);
	if (typeof oUl == 'undefined'){
   	//alert('Xtree object with specified ID=\'' + id + '\' not found!');
   	return false;
	}
  if (oUl == null){
   	//alert('Xtree object with specified ID=\'' + id + '\' is null!');
   	return false;
	}

	/* Set default values if not deffined */
	if (typeof params['set-class'] == 'undefined'){
   	params['set-class'] = true;
	}
  if (typeof params['set-last'] == 'undefined'){
   	params['set-last'] = true;
	}

	var oLi = oUl.getElementsByTagName('li');
	for (a = 0; a < oLi.length; a ++){
  	initXTreeRow(oLi[a],params);
	}
  if (params['set-last'] == true){
    setXTreeLast(oUl);
    var oUl2 = oUl.getElementsByTagName('ul');
    for (a = 0; a < oUl2.length; a ++){
    	setXTreeLast(oUl2[a]);
		}
	}
}