/************************************************************************************************************
Static folder tree
Copyright (C) October 2005  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	
	
/*
	Update log:
	December, 19th, 2005 - Version 1.1: Added support for several trees on a page(Alf Magne Kalleland)
	January,  25th, 2006 - Version 1.2: Added onclick event to text nodes.(Alf Magne Kalleland)
	February, 3rd 2006 - Dynamic load nodes by use of Ajax(Alf Magne Kalleland)
*/
		
	var idOfFolderTrees = ['dhtmlgoodies_tree'];
	
	var imageFolder = 'img/';	// Path to images
	var plusImage = 'dhtmlgoodies_plus.gif';
	var minusImage = 'dhtmlgoodies_minus.gif';
	var initExpandedNodes = '';	// Cookie - initially expanded nodes;
	
	var nodeId = 1;
	
	/*
	These cookie functions are downloaded from 
	http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
	*/
	function Get_Cookie(name) { 
	   var start = document.cookie.indexOf(name+"="); 
	   var len = start+name.length+1; 
	   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
	   if (start == -1) return null; 
	   var end = document.cookie.indexOf(";",len); 
	   if (end == -1) end = document.cookie.length; 
	   return unescape(document.cookie.substring(len,end)); 
	} 
	
	function Set_Cookie(name,value,expires,path,domain,secure) { 
		expires = expires * 60*60*24*1000;
		var today = new Date();
		var expires_date = new Date( today.getTime() + (expires) );
	    var cookieString = name + "=" +escape(value) + 
	       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
	       ( (path) ? ";path=" + path : "") + 
	       ( (domain) ? ";domain=" + domain : "") + 
	       ( (secure) ? ";secure" : ""); 
	    document.cookie = cookieString; 
	} 
	
	function expandAll(treeId)
	{
        var name = 'ul#'+treeId+' li';
        var tree = $$(name);
        tree.each(function(s) {
			var subItems = s.getElementsByTagName('UL');
			if(subItems.length>0 && subItems[0].style.display!='block'){
				showHideNode(false,s.id.replace(/[^0-9]/g,''));
			}			
        });
	}
	
	function collapseAll(treeId)
	{
        var name = 'ul#'+treeId+' li';
        var tree = $$(name);
        tree.each(function(s) {
			var subItems = s.getElementsByTagName('UL');
			if(subItems.length>0 && subItems[0].style.display=='block'){
				showHideNode(false,s.id.replace(/[^0-9]/g,''));
			}			
        });	
	}

	
	function parseSubItems(ulId,parentId)
	{
		
		if(initExpandedNodes){
			var nodes = initExpandedNodes.split(',');
		}
		$$(ulId+' LI').each(function(menu_item) {
			if (menu_item.getElementsByTagName('IMG').length>0) return;
            
			nodeId++;
            
			var img = document.createElement('IMG');
			img.src = imageFolder + plusImage;
			img.onclick = showHideNode;
			if (!menu_item.getElementsByTagName('UL').length) img.style.visibility='hidden';
            
			menu_item.id = 'dhtmlgoodies_treeNode' + nodeId;
			menu_item.insert({top: '&nbsp;'});
			menu_item.insert({top: img});
		});	
	}
		
			
	function showHideNode(e,inputId)
	{
		if(inputId){
			if (!$('dhtmlgoodies_treeNode'+inputId)) return;
			thisNode = $('dhtmlgoodies_treeNode'+inputId).getElementsByTagName('IMG')[0]; 
		}
        else {
			thisNode = this;
			if (this.tagName=='A') thisNode = this.parentNode.getElementsByTagName('IMG')[0];
		}
		if (thisNode.style.visibility=='hidden') return;
		var parentNode = thisNode.parentNode;
		inputId = parentNode.id.replace(/[^0-9]/g,'');
        
		if(thisNode.src.indexOf(plusImage)>=0){
			thisNode.src = thisNode.src.replace(plusImage,minusImage);
			var ul = parentNode.getElementsByTagName('UL')[0];
			ul.style.display='block';
			if (!initExpandedNodes) initExpandedNodes = ',';
			if (initExpandedNodes.indexOf(',' + inputId + ',')<0) initExpandedNodes = initExpandedNodes + inputId + ',';
		}
        else{
			thisNode.src = thisNode.src.replace(minusImage,plusImage);
			parentNode.getElementsByTagName('UL')[0].style.display='none';
			initExpandedNodes = initExpandedNodes.replace(',' + inputId,'');
		}	
		Set_Cookie('dhtmlgoodies_expandedNodes',initExpandedNodes,500);
	}
	
	function initTree()
	{
		
		for(var treeCounter=0;treeCounter<idOfFolderTrees.length;treeCounter++){
			
            $$('.'+idOfFolderTrees[treeCounter]+' LI').each(function(menu_item) {
				nodeId++;
                
				var img = document.createElement('IMG');
				img.src = imageFolder + plusImage;
				img.onclick = showHideNode;
                if (!menu_item.getElementsByTagName('UL').length) img.style.visibility='hidden';
                
				if (!menu_item.id) menu_item.id = 'dhtmlgoodies_treeNode' + nodeId;
				menu_item.insert({top: '&nbsp;'});
				menu_item.insert({top: img});
			});
		
		}
		initExpandedNodes = Get_Cookie('dhtmlgoodies_expandedNodes');
		if(initExpandedNodes){
			var nodes = initExpandedNodes.split(',');
			for(var no=0;no<nodes.length;no++){
				if(nodes[no])showHideNode(false,nodes[no]);	
			}			
		}	
	}
