
head = new Node("root","url","root");
stickyList = new Node("root","url","root");
parentList = new Node("root","url","root");
last=0;
lastElement=0;
function test(){
	document.getElementById('s1').className='high';	
}
function toggleSticky(){
	sticky=!sticky;	
}
function expandAll(){
	//expandall
	head.openAll();	
}
function collapseAll(){
	//collapseall
	stickyList = new Node("root","url","root");
	head.closeAll();
	elementOpen(current);	
}

function Node(n,l,i){
	this.prev=void(0); 
	this.next=void(0); 
	this.children="";
	this.name=n;
	this.open=false;
	this.link=l; 
	this.id=i;
	this.add=Add; 
	this.insert=Insert; 
	this.toString=ToString; 
	this.print=printAll;
	this.remove=Delete;
	this.buildHTML=buildhtml;
	this.findParents=findparents;
	this.isIn=isin;
	this.openAll=openall;
	this.closeAll=closeall;
	this.indent=menuindent;
	this.exdent=menuexdent;
	this.putImages=putimages;
	this.findElementAt=findelementat;
	this.findElement=findelement;
	this.handleIndent=handleindent;
	function ToString(){
 	var str=this.id.toString(); 
		if(this.next){ 
			str+=", "+this.next.toString();
 		} return str; 
	} 
	function Delete(info){
		var node=this; 
		while(node.id!=info&&node.next)
			node=node.next;
  		if(node.prev){
    		node.prev.next=node.next;
  		}
  		if(node.next){
    		node.next.prev=node.prev;
		} 
	}
	function Insert(n,l,i){
 		var node=this; 
		while(node.next){
 			node=node.next;
 		} 
		node.next=new Node(n,l,i);
 		node.next.prev=node;
	}
	function openall(){
		if(this.children&&this.id!='root')	
			elementOpen(this.id);
		if(this.children)
				this.children.openAll();
		if(this.next)
				this.next.openAll();
	}
	function closeall(){
		if(this.children&&this.id!='root')	
			elementClose(this.id);
		if(this.children)
				this.children.closeAll();
		if(this.next)
				this.next.closeAll();
	}
	function Add(n,l,i,p){
		var node=this; 
		if(node.id==p){
			if(!node.children)
				node.children = new Node(n,l,i);
			else{
				node = node.children;
				while(node.next)
					node=node.next;
				node.next = new Node(n,l,i);
			}
			return;
		}else{
			if(this.children)
				this.children.add(n,l,i,p);
			if(this.next)
				this.next.add(n,l,i,p);
		}	
	} 
	
	function putimages(id,cur,child,bro){
		if(!useimages)
			return;
		if(child){
			
			html+="<a href=\"Javascript:elementSwap('"+this.id+"');\">";
			if(id==cur&&child){
				html+="<img src='test3/minusstart.gif' border='0' id='im"+id+"'>";
				html+="<img src='test3/folderopen.gif' border='0' id='imf"+id+"'>";
			}else if(id==cur&&child){
				html+="<img src='test3/minus.gif' border='0' id='im"+id+"'>";
				html+="<img src='test3/folderopen.gif' border='0' id='imf"+id+"'>";
			}else if(id==cur&&!bro){
				html+="<img src='test3/minusend.gif' border='0' id='im"+id+"'>";
			}else if(bro&&head.children.id==id){
				html+="<img src='test3/plusstart.gif' border='0' id='im"+id+"'>";
				html+="<img src='test3/folder.gif' border='0' id='imf"+id+"'>";
			}else if(bro){
				html+="<img src='test3/plus.gif' border='0' id='im"+id+"'>";
				html+="<img src='test3/folder.gif' border='0' id='imf"+id+"'>";
			}else if(!bro){
				html+="<img src='test3/plusend.gif' border='0' id='im"+id+"'>";				
				html+="<img src='test3/folder.gif' border='0' id='imf"+id+"'>";
			}
			html+="</a>";
		}else if(head.children.id==id){
				html+="<img src='test3/spacerstart.gif' border='0' id='im"+id+"'>";		
		}else if(bro){
				html+="<img src='test3/spacer.gif' border='0' id='im"+id+"'>";	
		}else
			html+="<img src='test3/spacerend.gif' border='0' id='im"+id+"'>";	
	
	}
	
	function handleindent(){
		if(useimages){
			for(i=0;i<depth;i++)
				if(!relation[i])
					html+="<img src='test3/indent.gif' border='0'>";
				else
					html+="<img src='test3/indentblank.gif' border='0'>";
		}
			
	}
	function menuindent(next){
		if(useimages){
			if(!next)
				relation[depth]=1;
		} else
			html+="<blockquote>";
		depth++;
	}
	function menuexdent(){
		depth--;
		if(useimages){
			relation[depth]=0;
		} else
		html+="</blockquote>";
	}
	function putRightImages(id,cur){
		html+="</td><td>";
		html+="<a href=\"Javascript:elementSwap('"+id+"');\">";
		if(id==cur)
			html+="<img id='imr"+id+"' border='0' src='test3/down.gif'>";
		else
			html+="<img id='imr"+id+"' border='0' src='test3/right.gif'>";
		html+="</a>";		
	}	
	function buildhtml(){
		
		lastElement=this;
				
		html+="<table cellpadding='0' border='0' cellspacing='0'><tr><td>";
		this.handleIndent();	
		this.putImages(this.id,current,this.children,this.next,counter);			
		html+="</td><td valign='middle'>";
		html+="<table cellpadding='1' border='0' cellspacing='0'><tr><td>";
		html+="<a href=\"Javascript:handleClick('"+this.id+"',"+counter+",'"+this.link+"');\">";
		
		if(this.id==current&&!last){
			position=counter;	
			html+="<span id='"+this.id+"' class='high'>";
			loadLink(this.link);
		} else{
			html+="<span id='"+this.id+"' class='low'>";
		}
			
		html+="&nbsp;"+this.name+"&nbsp;</span></a></td></tr></table></td><td>";
		if(userightimages&&this.children)
			putRightImages(this.id,current);
		html+="</td></tr></table>";
	
		counter++;
		//document.write(html);
		//html="";
		if(this.children){
			this.indent(this.next);
			if(this.id==current&&!last)
					html+="<span id='parent"+this.id+"' style='display:block'>";
			else
				html+="<span id='parent"+this.id+"' style='display:none;'>";
			this.children.buildHTML();
			html+="</span>";
			this.exdent();
		}	
		if(this.next){
			addLine();	
			this.next.buildHTML();
		}
		if(!this.next)
			addLine();	
				
	}
	function addLine(){
		if(depth==0){
			html+="<img src='test3/whiteline.gif' width='200' height='1'><br>";
			}
	}
	function printAll(start){
		if(start.indexOf('?')>=0){
			var tempstr = start.substring(start.indexOf('last='),start.length);
			tempstr = tempstr.substring(5,6);
			if(tempstr==1)
				last=1;
		}
		html="";
		count=0;
		this.children.buildHTML();
		
		menusize=counter;
		//document.form1.textfield.value=html;
		document.write(html);
		if(last){
			current=0;
			handleClick(lastElement.id,counter-1,lastElement.link);
		}
		
	}
	
	function findelementat(pos){
		if(counter-1==pos){
			elementFound=this;
			found=1;
		}
		if(this.children&&!found){
			counter++;
			this.children.findElementAt(pos);
		}
		if(this.next&&!found){
			counter++;
			this.next.findElementAt(pos);
		}
	}
	function findelement(cur){
		if(this.id==cur){
			elementFound=this;
			found=1;
		}
		if(this.children&&!found){
			this.children.findElement(cur);
		}
		if(this.next&&!found){
			this.next.findElement(cur);
		}
	}
	
	function findparents(div){
		var next=0;
		if(this.id==div)
			found=1;
		counter++;
		if(this.children&&!found)
			this.children.findParents(div);
		if(this.next&&!found){
			next=1;
			this.next.findParents(div);
		}
		if(found){
			found++;
			if(!next)
				if(this.id!='root')
					parentList.insert(this.name,this.link,this.id);
		}
	}
	function isin(i){
		if(this.id == i)
			return true;
		var node = this;
		while(node.next){
			node = node.next;
			if (node.id == i)
				return true;
		}
	}
}

function swapOpen(id){
	if(!useimages)
		return;
	//find element in main tree
	found=0;
	head.findElement(id);
	if(elementFound.next&&elementFound.id==head.children.id)
		document.getElementById('im'+id).src="test3/minusstart.gif";
	else if(elementFound.next)
		document.getElementById('im'+id).src="test3/minus.gif";
	else
		document.getElementById('im'+id).src="test3/minusend.gif";
	document.getElementById('imf'+id).src="test3/folderopen.gif";
}
function swapClose(id){
	if(!useimages)
		return;
	found=0;
	head.findElement(id);
	if(elementFound.next&&elementFound.id==head.children.id)
		document.getElementById('im'+id).src="test3/plusstart.gif";
	else if(elementFound.next)
		document.getElementById('im'+id).src="test3/plus.gif";
	else
		document.getElementById('im'+id).src="test3/plusend.gif";
	document.getElementById('imf'+id).src="test3/folder.gif";
}
function rightClose(id){
	if(!userightimages)
		return;
	if(document.getElementById('imr'+id))
		document.getElementById('imr'+id).src="test3/right.gif";
}
function rightOpen(id){
	if(!userightimages)
		return;
	if(document.getElementById('imr'+id))
		document.getElementById('imr'+id).src="test3/down.gif";
}
function loadLink(link){
	parent.mainFrame.document.location=link;
	//document.form1.url.value=link;
}
function handleClick(div,p,link){
	loadLink(link);
	position=p;
	if(current==div){
		var obj = document.getElementById('parent'+current);
		if(obj){	
			if(obj.style.display=='none'){
				swapOpen(div);
				rightOpen(div);
				obj.style.display='block';
			}else{
				stickyList.remove(0,0,div);
				swapClose(div);
				rightClose(div);
				obj.style.display='none';
			}
		}
		return;
	}
	elementClose(current,1)
	elementOpen(div,1);
	current=div;
}
function elementSwap(div){
	if(document.getElementById('parent'+div).style.display=="none"){
		swapOpen(div);
		rightOpen(div);
		document.getElementById('parent'+div).style.display='block';
	}else{
		swapClose(div);
		rightClose(div);
		document.getElementById('parent'+div).style.display='none';
	}
}
function elementClose(div,highlight){
	if(div!=0){
		if(highlight)
			document.getElementById(div).className='low';
		parentList = new Node("root","url","root");
		found=0;
		head.findParents(div);
		var node = parentList;
		while(node){
			if(node.id!='root'&&!stickyList.isIn(node.id))
				if(document.getElementById('parent'+node.id)){
					swapClose(node.id);
					rightClose(node.id);
					document.getElementById('parent'+node.id).style.display='none';
				}
			node=node.next;
		}
	}
}

function elementOpen(div,highlight,nosticky){
		if(highlight)
			document.getElementById(div).className='high';
		if(sticky&&!nosticky)
			stickyList.insert(0,0,div);
		parentList = new Node("root","url","root");
		found=0;
		head.findParents(div);
		var node = parentList;
		while(node){
			if(node.id!='root')
					if(document.getElementById('parent'+node.id)){
						swapOpen(node.id);
						rightOpen(node.id);
						document.getElementById('parent'+node.id).style.display='block';
					}
			node=node.next;
		}
}
function next(){
	counter=0;
	if(position<menusize)
		position++;
	found=0;
	head.findElementAt(position);
	if(position<menusize)
		handleClick(elementFound.id,position,elementFound.link);
	else 
		handleNewUrl(1);
}
function previous(){
	counter=0;
	if(position>=0)
		position--;
	found=0;
	head.findElementAt(position);
	if(position>=0)
		handleClick(elementFound.id,position,elementFound.link);
	else
		handleNewUrl(0);
}
function handleNewUrl(next){
	if(next)
		document.location=nextURL;
	else if(previousURL.indexOf('#')==-1)
		document.location=previousURL+'?last=1';
	if(nextURL=='#')
		position--;
	if(previousURL=="#")
		position++;
}
