var openImg = new Image();
openImg.src = "../layout/open.gif";
var closedImg = new Image();
closedImg.src = "../layout/closed.gif";


function IsNs() { 
	var b = navigator.appName;
		
	if (b == "Netscape") {
		return true;
	} else {
		return false;
	}
}

function tree(){
	this.branches = new Array();
	this.add = addBranch;
	this.write = writeTree;
}

function branch(id, text, link, level, parent){
	this.id = id;
	this.text = text;
	this.link = link;
	this.write = writeBranch;
	this.add = addLeaf;
	this.level = level;
	this.parent= parent;
	this.leaves = new Array();
}

function leaf(text, link, level, parent){
	this.text = text;
	this.link = link;
	this.level = level;
	this.parent = parent;
	this.write = writeLeaf;
}

function writeTree(){
	var treeString = '';
	var numBranches = this.branches.length;
	for(var i=0;i<numBranches;i++)
		treeString += this.branches[i].write();
	document.write(treeString);
}

function addBranch(branch){
	this.branches[this.branches.length] = branch;
}

function writeBranch(){
	if (this.level > 0) {
		var branchString = '<div class="branch" onClick="closeLeaves('+this.parent+');closeLeaves('+this.id+');showBranch(\'' + this.id + '\')"';
	} else {
		var branchString = '<div class="branch" onClick="closeLeaves('+this.id+');closeBranches(myTree);showBranch(\'' + this.id + '\')"';
	}
	branchString += '><a href="../output/'+this.link+'" TARGET="mid"';
	branchString += 'class="sub'+this.level+'">';
//	branchString += '<img src="../layout/closed.gif" valign="middle" id="I' + this.id + '" BORDER=0>';
	branchString += this.text;
//	branchString += this.level;
	branchString += '</a></div>';
	if (this.level == null) 
		branchString += '<span class="leaf" id="';
	else
		branchString += '<span class="leaf'+this.level+'" id="';
	branchString += this.id + '">';
	var numLeaves = this.leaves.length;
	for(var j=0;j<numLeaves;j++)
		branchString += this.leaves[j].write();
	branchString += '</span>';
	return branchString;
}

function addLeaf(leaf){
	this.leaves[this.leaves.length] = leaf;
}

function writeLeaf(){
	var leafString = '<a href="../output/' + this.link + '" TARGET="mid"';
	if (this.level == 0) {
		leafString += 'onClick="closeBranches(myTree)" ';
	} else {
		leafString += 'onClick="closeLeaves('+this.parent+')" ';
	}
	leafString += 'CLASS="sub'+this.level+'">';
//	leafString += '<img src="../layout/doc.gif" border="0" valign="middle">';
	leafString += this.text;
//	leafString += this.level;
	if (IsNs()) {
		leafString += '</a>';
	} else {
		leafString += '</a><br>';
	}	
	return leafString;
}

function showBranch(branch){
	var objBranch = document.getElementById(branch).style;
	if(objBranch.display=="block")
		objBranch.display="none";
	else
		objBranch.display="block";
//	swapFolder('I' + branch);
}

function swapFolder(img){
	objImg = document.getElementById(img);
	if(objImg.src.indexOf('closed.gif')>-1)
		objImg.src = openImg.src;
	else
		objImg.src = closedImg.src;
}

function closeBranches(tree) {
	var numBranches = tree.branches.length;
	for(var i=0;i<numBranches;i++) {
		var branchID = tree.branches[i].id;
		if (branchID != null) {
			var objBranch = document.getElementById(branchID).style;
			objBranch.display="none";
		}
	}
}

function closeLeaves(branch) {
	var numBranches = branch.leaves.length;
	for(var i=0;i<numBranches;i++) {
		var branchID = branch.leaves[i].id;
		if (branchID != null) {
			var objBranch = document.getElementById(branchID).style;
			objBranch.display="none";
		}
	}
}