/** 
 * @package treeview
 * @version 1.2 
 * @author Frédéric LECOINTRE<frederic.lecointre@burnweb.net>
 * 
 */
 
Treeview = function(sId) {
	this.id = sId;
	this.fullyOpen = false;
	this.saveState = true;
	this.cookiePrefix = sId + "TreeviewState_";
};//end function

/**
 *
 * @return void
 */
Treeview.prototype.initialize = function() {

	var fullyOpen = false;
	var oUl = window.document.getElementById(this.id);
	
	if (oUl.className.indexOf('fully-open') != -1) {
		this.fullyOpen = true;
	}//end if
	
	oUl.className = 'treeview';
	
	var lis = oUl.getElementsByTagName('li');
	var bSaveState = this.saveState;
	
	for (var i = 0; i < lis.length; i++) {
	
		var uls = lis[i].getElementsByTagName('ul');
		
		if (uls.length > 0) {

				if (lis[i].className == 'open' || this.fullyOpen || (this.saveState && Cookies.exists(this.cookiePrefix + lis[i].id))) {
					this.uncollapse(lis[i]);
				}//end if
				else{
					this.collapse(lis[i]);
				}//end else
			
				var  sCookiePrefix = this.cookiePrefix;
				
			lis[i].onmousedown = function(e){
			
				if(this.className.substring(0, 4) == 'last'){

					if(this.className == 'lastopen'){
					
						if(bSaveState){
							Cookies.remove(sCookiePrefix + this.id);
						}//end if
						
						this.className = 'lastcollapsed';
						
					}//end if
					else{
						if(bSaveState){
							Cookies.write(sCookiePrefix + this.id, 'open');
						}//end if
					
						this.className = 'lastopen';
						
					}//end else

				}//end if
				else{
					if(this.className == 'open'){
						this.className = 'collapsed';
					}//end if
					else{
						this.className = 'open';
					}//end else
				}//end else
				
				cancelEvent(e);
			};//end function
			
		}//end if
		else {
			if( lis[i].id.substring(0, 4) == 'doc_') {
				lis[i].className = 'file';
			}//end if
			else {
				lis[i].className = 'folder';
			}//End else
		}//end else
		
		if (!this.nextElement(lis[i])) {
			lis[i].className = 'last' + lis[i].className;
		}//end if
		
	}//end for
	
	var as = oUl.getElementsByTagName('a');
	for (var i = 0; i < as.length; i++) {
		as[i].onmousedown = function(e) {
			cancelEvent(e);
		};//end function
	}//end for
	
};//end function


/**
 *
 * @return void
 */
Treeview.prototype.expand = function() {

	var oUl = window.document.getElementById(this.id);

	var lis = oUl.getElementsByTagName('li');
	
	for (var i = 0; i < lis.length; i++) {

		var uls = lis[i].getElementsByTagName('ul');
		
		if (uls.length > 0) {
		
			this.uncollapse(lis[i]);
			
			if(this.saveState){
				Cookies.write(this.cookiePrefix + lis[i].id, 'open');
			}//end if
			
		}//end if
		
	}//end for
	
};//end function

/**
 *
 * @return void
 */
Treeview.prototype.foldup = function() {

	var oUl = window.document.getElementById(this.id);

	var lis = oUl.getElementsByTagName('li');
	
	for (var i = 0; i < lis.length; i++) {
		
		var uls = lis[i].getElementsByTagName('ul');
		
		// Ne replie pas la racine
		if(i == 0){
			continue;
		}//end if
		
		if (uls.length > 0) {
		
			this.collapse(lis[i]);
			
			if(this.saveState){
				Cookies.remove(this.cookiePrefix + lis[i].id);
			}//end if
			
		}//end if
		
	}//end for
	
};//end function

/**
 *
 * @return void
 */
Treeview.prototype.searchHighLight = function() {

	var oUl = window.document.getElementById(this.id);
	var aAs = oUl.getElementsByTagName('a');
	var k = 0;
	
	for (var i = 0; i < aAs.length; i++) {
		
		if(aAs[i].className == 'treeviewHighlitght'){
			
			oParent = aAs[i].parentNode;
			
			do{
				// Infinite loop lock
				if(k > 15) {
					break;
				}//end if
				k++;
				
				//alert(oParent.nodeName + ' ' + oParent.id + ' ' + oParent.className);//DEBUG
				
				if(oParent.nodeName.toLowerCase() == 'ul'){
					if(oParent.id == oUl.id){
						break;
					}//end if
					
					oParent = oParent.parentNode;
					
				}//end if
				else{
					this.uncollapse(oParent);
					if(this.saveState){
						Cookies.write(this.cookiePrefix + oParent.id, 'open');
					}//end if
					oParent = oParent.parentNode;
				}//end else
				
			}while(true);
			
		}//end if
		
	}//end for
	
};//end function

/**
 *
 * @param Node oNode
 * @return void
 */
Treeview.prototype.collapse = function(oNode) {
	oNode.className = 'collapsed';
};//end function


/**
 *
 * @param Node oNode
 * @return void
 */
Treeview.prototype.uncollapse = function(oNode) {
	oNode.className = 'open';
};//end function

/**
 *
 * @param Node node
 * @return node
 */
Treeview.prototype.nextElement = function (node) {

	try {
		do {
			node = node.nextSibling;
		} while (node.nodeType != 1);
			
		return node;
	}//end try
	catch (e) {
		return false;
	}//end try
	
};//end function


