var imgNode = "'../image/node.gif'";
var imgLastNode = "'../image/lastnode.gif'";
var imgVertLine = "'../image/vertline.gif'";
var imgEmpty = "'../image/empty.gif'";

var gTree;
var gCurrNode;
var undefined;		// compatible with IE5(Javascript1.1?)

//-----------------------------------------------------------------------------------------------
//   tree
//-----------------------------------------------------------------------------------------------
function tree(root, treeFrame,targetFrame)
{
	this.root = root;
	this.currentNode = root;
	this.treeFrame = treeFrame;
	this.targetFrame = targetFrame;

	this.addNode = tree_addNote;
	this.redraw = tree_redraw;
	this.removeEmptyFolders = tree_removeEmptyFolders;
	this.getNodePath = tree_getNodePath;
	this.open = tree_open;

	gTree = this;
	gCurrNode = this.root;
}

function tree_addNote(parentID,node)
{
	if(gCurrNode == undefined)
		gCurrNode = this.root.findNode(parentID);
	else if(gCurrNode.id != parentID)
		gCurrNode = this.root.findNode(parentID);
	if(gCurrNode != undefined)
	{
		gCurrNode.appendNode(node);
	}
}

function tree_redraw()
{
	var doc = this.treeFrame.window.document;
    doc.clear();
	doc.write("<html>");
	doc.write("<head>");
	doc.write('<meta http-equiv="content-type" content="text/html;charset=SHIFT-JIS">');
	doc.write("</head>");
    doc.write("<body bgcolor='white'>");
	doc.write("<style>");
	doc.write("<!-- #Small {font-size:11px;} -->");
	doc.write("<!-- #Normal{font-size:12px;} -->");
	doc.write("</style>");
    this.root.redraw(doc, 0, 1, "");
    doc.linkColor = 'black';
    doc.alinkColor = 'black';
    doc.vlinkColor = 'black';
    doc.write("</body>");
    doc.write("</html>");
    doc.close();
}

function tree_removeEmptyFolders()
{
	this.root.removeEmptyFolders();
}

function tree_getNodePath(id)
{
	return this.root.getNodePath(id);
}

function tree_open(id)
{
	return this.root.open(id);
}


//-----------------------------------------------------------------------------------------------
//   node
//-----------------------------------------------------------------------------------------------
var conTPFolder = 0;
var conTPLeaf = 1;
var conSTClose = 0;
var conSTOpen = 1;

function node(id,			// unique id for every node
			  name,			// disp name for every node
			  type,			// node type : 0:folder,1:leaf, -1:empty node
			  openICON,		// image url for node open display	
			  closeICON,	// image url for node close display	
			  linkUrl )		// a link url to pass to the targetFrame when is clicked
{
	this.id = id;
	this.name = name;
	this.type = type;
	this.openICON = openICON;
	this.closeICON = closeICON;
	this.linkUrl = linkUrl;
	this.status = conSTClose;
	this.children = new Array();
	this.appendNode = node_append;
	this.findNode = node_find;
	this.removeEmptyFolders = node_removeEmptyFolders;
	this.redraw = node_redraw;
	this.displayIconAndLabel = node_displayIconAndLabel;
	this.getNodePath = node_getNodePath;
	this.open = node_open;
}

function node_append(node)
{
	this.children[this.children.length] = node;
}

function node_find(id)
{
	var found;

	if(this.id == id)
		found = this;
	else
	{
		for(var i=0; i<this.children.length; i++)
		{
			found = this.children[i].findNode(id);
			if(found != undefined)	break;
		}
	}
	return found;
}

function node_removeEmptyFolders()
{
	if(this.type == conTPLeaf)	// this is a leaf
		return;

	for(var i=this.children.length-1;i>=0;i--)
	{
		var child = this.children[i];
		child.removeEmptyFolders();
		if(child.type!=conTPLeaf && child.children.length == 0)
//			this.children.splice(i,1);
			removeArray(this.children, i, 1);
	}
}

function node_redraw(doc, level, lastNode, leftSide)
{
    doc.write('<table border=0 cellspacing=0 cellpadding=0>');
    doc.write('<tr><td valign = middle nowrap title="'+this.name+'">');
    doc.write(leftSide);
    if (level>0)
        if (lastNode) //the last 'brother' in the children array
        {
            doc.write('<img src=' +imgLastNode+ ' width=16 height=20>');
            leftSide = leftSide + '<img src=' +imgEmpty+ ' width=16 height=20>' ;
        }
        else
        {
           doc.write('<img src=' +imgNode+ ' width=16 height=20>');
           leftSide = leftSide + '<img src=' +imgVertLine+ ' width=16 height=20>';
        }
                
    this.displayIconAndLabel(doc);
    doc.write('</td></tr></table>');
        
    if (this.status==conSTOpen) // the folder is open, draw the children
    {
        for (var i=0; i<this.children.length;i++)
            if (i==this.children.length-1)
                this.children[i].redraw(doc, level+1, 1, leftSide);
            else
                this.children[i].redraw(doc, level+1, 0, leftSide);
    }
}

function node_displayIconAndLabel(doc)
{
	var bgcol;

	if(gCurrNode != undefined && this.id == gCurrNode.id){
		bgcol = "silver"
	}
	else {
		bgcol = "white";
	}

	doc.write('<A name="'+this.id+'"></A>');
    doc.write("<A href='javascript:parent.openBranch(\"" +this.id+ "\")'");
    if (this.status == conSTOpen)
    {
        doc.write("onMouseOver='window.status=\"Close folder\"; return true'>");
        doc.write("<img src=" +this.openICON+ " border=noborder></A>");
    }
    else
    {
        doc.write("onMouseOver='window.status=\"Open folder\"; return true'>");
        doc.write("<img src=" +this.closeICON+ " border=noborder></A>");
    }
    doc.write("</td><td valign=middle align=left nowrap bgcolor=" + bgcol + ">");
    doc.write("<A href='javascript:parent.openBranch(\"" +this.id+ "\")' style=\"TEXT-DECORATION:none\">");
    doc.write("<font id='Small' color=black>" +this.name+ "</font></A>");

}

function node_getNodePath(id)
{
	var nodepath = "";

	if (this.id == id)
		nodepath = this.name;
	else
	{
		for(var i=0; i<this.children.length; i++)
		{
			nodepath = this.children[i].getNodePath(id);
			if(nodepath.length>0)
			{
				nodepath = this.name + "/" + nodepath;
				break;
			}
		}
	}
	return nodepath;
}


function node_open(id)
{
	var opened;

	if (this.id == id)
	{
		this.status = conSTOpen;
		opened = this;
	}
	else
	{
		for(var i=0; i<this.children.length; i++)
		{
			opened = this.children[i].open(id);
			if(opened != undefined)
			{
				this.status = conSTOpen;
				break;
			}
		}
	}
	return opened;
}


function openBranch(id)
{
	if(gCurrNode == undefined || (gCurrNode != undefined && gCurrNode.id != id) ){
		gCurrNode = activeNodeLink(id);
	}

	if(gCurrNode.type==conTPFolder)
	{
		if(gCurrNode.status==conSTOpen)
			gCurrNode.status=conSTClose;
		else
			gCurrNode.status=conSTOpen;
	}
	gTree.redraw();
//	var win = this.treeFrame.window;		// in IE5,this will cause refresh and reload frames.
//	win.location.hash = id;

}

function activeNodeLink(id)
{
	var currNode = gTree.root.findNode(id);
	if(currNode != undefined && gTree.targetFrame)
	{
		var linkUrl = new String(currNode.linkUrl);
		if(linkUrl.length>0)
		{
			if(linkUrl.search('%%NODEPATH%%')>0)
			{
				linkUrl = linkUrl.replace('%%NODEPATH%%',escape(gTree.getNodePath(id)));
			}
			gTree.targetFrame.location = linkUrl;
		}
	}
	return currNode;
}

function removeArray(arr, start, count)
{
	if(start>=0 && count>0)
	{
		var cnt, len = arr.length;
		if(start>len) start = len;
		if(start+count>len) count=len-start;
		for(cnt=start;cnt<len-count;cnt++) arr[cnt]=arr[cnt+count];
		arr.length = len-count;
	}
}
