
/***** Global variables, begins **********************/

// the icon's size
var g_LineWidth		= 16;
var g_IconWidth		= 18;
var g_IconHeight	= 24;

// g_rootFolder : root element, not visible
var g_rootNode	= new TreeNode( 3, "", 1,  1, "" , true, 1, false);
g_rootNode.Last	= true;
g_rootNode.IsExpanded = true;
window.objNode3	= g_rootNode;

// flag -- to determine if the prompt of loading displayed
var g_blnLoadingPrompty	= true;
// prompt when loading subtree
var g_htmlLoading		= "<div style='color: #999999;font-size:12px;'>&#32;&#32;&#27491;&#22312;&#36733;&#20837;&#65292;&#35831;&#31561;&#24453;&#46;&#46;&#46;</div>";

// flag -- record if there's something is loading now
var g_blnIsLoading		= false;

//
var g_SelectedNode;
// define the aided iframe for load subtree -- "ifmLoad"
document.writeln("<iframe name='ifrmLoad' id='ifrmLoad' width='100%' height='0'></iframe>");

/***** Global variables, ends ************************/


/***** Class TreeNode definition, begins ***************/
function newTreeNode( Id, Title, Index,  ChildrenCount, Icon, Visible, CreatedType, HaveContent) 
{
	treeNode = new TreeNode( Id, Title, Index, ChildrenCount, Icon, Visible, CreatedType, HaveContent);
	return treeNode;
}

function TreeNode( Id, Title, Index, ChildrenCount, Icon, Visible, CreatedType, HaveContent) //constructor
{

	if (Icon.length == 0)	// 得到默认的图标
		Icon = (ChildrenCount > 0) ? "iDefFolder.gif" : "iDefault.gif";

// properties
	this.ClassName		= "TreeNode";
	this.Id				= Id;
	this.Index			= Index;
	this.Title			= Title;
	this.ChildrenCount	= ChildrenCount;
	this.Icon			= Icon;
	this.Visible		= Visible;
	this.CreatedType	= CreatedType;
	this.HaveContent	= HaveContent;

	this.Children		= new Array();
	this.ParentNode		= null;
//status
	this.IsExpanded		= false;
	this.IsLoaded		= false;
	this.Last			= false;
// methods, begins
	// add child node object
	this.AddChild			= trNd_AddChild;
	// load the children
	this.Load				= trNd_Load;
	// generate the children
	this.LoadEnd			= trNd_LoadEnd;

	this.LoadAll			= trNd_LoadAll;
	this.LoadAllEnd			= trNd_LoadAllEnd;
	// get the leading html -- to display leadingimages, common to "Node" and "Leaf";
	this.GetHtmlLeading		= trNd_GetHtmlLeading;

	// toggle self's state  open <-> close
	this.Expand				= trNd_Expand;

	// get the children html element
	this.GetChildrenSpan	= trNd_GetChildrenSpan;

	// get the children html element's innerHTML
	this.GetChildrenHTML	= trNd_GetChildrenHTML;
	// set the children html element's innerHTML
	this.SetChildrenHTML	= trNd_SetChildrenHTML;
	
	this.SelectNode			= trNd_SelectNode;
// methods, ends

}

function trNd_AddChild( childNode )
{
	// Set the child's parent
	childNode.ParentNode = this;

	// Update the "Last" flag
	if ( this.Children.length > 0) {
		this.Children[ this.Children.length - 1 ].Last = false;
		childNode.Last = true;
	}else{
		childNode.Last = true;
	}


	// Add add it into arrays
	this.Children[ this.Children.length ] = childNode;
	// All Folder Object stored as window's children object
	eval( "window.objNode" + childNode.Id + " = childNode;");

	return childNode;
}

function trNd_Load()
{
	var l_iFrmLoad = document.frames("ifrmLoad");

	if ( !g_blnIsLoading && l_iFrmLoad )
	{
		// disable the all other events
		this.GetChildrenSpan().setCapture();

		// update the loading status flag
		g_blnIsLoading = true;

		if ( g_blnLoadingPrompty )
			this.SetChildrenHTML( g_htmlLoading );

	//	l_iFrmLoad.document.location.replace( "LoadNode" + this.Id + ".htm" );
		l_iFrmLoad.location.href = ( "LoadNode" + this.Id + ".htm" );
	}
}

function trNd_LoadEnd()
{
	var l_htmlTemp	= "";

	for ( var i = 0; i < this.Children.length; i++ )
	{
		var l_nodeChild		= this.Children[i];
		var l_lngFileId		= l_nodeChild.Id;

		var l_blnFolder		= l_nodeChild.ChildrenCount > 0 ;

		// Table Begins
		l_htmlTemp += "<table border='0' cellspacing='0' cellpadding='0' style='font-size:12px;'><tr>";

		// Leader Images
		l_htmlTemp += l_nodeChild.GetHtmlLeading();

		// last node's image sign
		var l_strLast = "";
		if ( i == (this.Children.length - 1) )
		    l_strLast = "last";

		// Node Image
		if ( l_blnFolder )
			l_htmlTemp += "<td><a href='javascript:ExpandNode(" + l_lngFileId + ")'><img id='fldImg" + l_lngFileId + "' src='../Images/TreeImages/Line_p" + l_strLast + "node.gif'"
							+ " openImg='../Images/TreeImages/Line_m" + l_strLast + "node.gif' closeImg='../Images/TreeImages/Line_p" + l_strLast + "node.gif'"
							+ " width='" + g_LineWidth + "' height='" + g_IconHeight + "' border='0'></a></td>";
		else
			l_htmlTemp += "<td><img src='../Images/TreeImages/Line_" + l_strLast + "node.gif' width='" + g_LineWidth + "' height='" + g_IconHeight + "' border='0'></td>";

		// Node Label
		l_htmlTemp += "<td><img id='nodeIcon" + l_lngFileId + "' src='../Images/BookNodeIcon/" + l_nodeChild.Icon + "' width='" + g_IconWidth + "' height='" + g_IconHeight + "' border='0'></td>";
		l_htmlTemp += "<td nowrap><a href='javascript: ExpandAndView(" + l_lngFileId + ");' class='theBook'><span id='TitleSpanOf" + l_lngFileId + "'>" + l_nodeChild.Title + "<span></a></td>";

		// Table Ends
		l_htmlTemp += "</tr></table>";

		// If the node is a folder, add the children element
		//if ( l_blnFolder )
			l_htmlTemp += "<span id='spanofNode" + l_lngFileId + "' style='display:none;'></span>";

		// New line character
		l_htmlTemp += "\n";

	}

	this.SetChildrenHTML( l_htmlTemp );


	// enable the all other events
	this.GetChildrenSpan().releaseCapture();

	//
	this.ChildrenCount = this.Children.length;
	this.IsLoaded = true;
	//

	var l_iFrmLoad = document.frames("ifrmLoad");
	if ( l_iFrmLoad )
		l_iFrmLoad.document.location.replace( "about:blank;" );


	// update the loading status flag
	g_blnIsLoading = false;
}

function trNd_LoadAll()
{
	var l_iFrmLoad = document.frames("ifrmLoad");

	if ( l_iFrmLoad )
	{
		// disable the all other events
		this.GetChildrenSpan().setCapture();

		// update the loading status flag
		g_blnIsLoading = true;

		if ( g_blnLoadingPrompty )
			this.SetChildrenHTML( g_htmlLoading );

		l_iFrmLoad.document.location.replace( "LoadNode.asp?NodeId=" + this.Id + "&LoadAll=1" );

	}

}

function trNd_LoadAllEnd()
{	
	if(!this.IsLoaded)
		this.LoadEnd();

	for(var i = 0; i < this.Children.length; i++)
	{
		this.Children[i].LoadAllEnd();
	}
	this.IsLoaded	 = true;
}

function trNd_GetHtmlLeading()
{

	var l_htmlTemp = "";

	for ( var l_objNode = this; l_objNode.ParentNode != g_rootNode; l_objNode = l_objNode.ParentNode )
	{
		if ( l_objNode.ParentNode.Last )
			l_htmlTemp = "<td><img src='../Images/TreeImages/Node_blank.gif' width='" + g_LineWidth + "' height='" + g_IconHeight + "' border='0'></td>" + l_htmlTemp;		    
		else
			l_htmlTemp = "<td><img src='../Images/TreeImages/Line_vertline.gif' width='" + g_LineWidth + "' height='" + g_IconHeight + "' border='0'></td>" + l_htmlTemp;
	}
	return l_htmlTemp;
}

function trNd_GetChildrenSpan()
{
	return ( document.all("spanofNode" + this.Id) );
}

function trNd_Expand()
{
	var l_elemChildren = this.GetChildrenSpan();
	var l_imgfld	= document.all("fldImg" + this.Id);
	var l_iconfld	= document.all("nodeIcon" + this.Id);

	if ( !this.IsExpanded )
	{
		l_elemChildren.style.display = "";
		l_imgfld.src	= l_imgfld.openImg;
		l_iconfld.src	= "../Images/BookNodeIcon/Opened" + this.Icon;

		this.IsExpanded				= true;
	}		
	else
	{
		l_elemChildren.style.display = "none";

		l_imgfld.src	= l_imgfld.closeImg;
		l_iconfld.src	= "../Images/BookNodeIcon/" + this.Icon;

		this.IsExpanded				= false;
	}
}

function trNd_GetChildrenHTML()
{
	return this.GetChildrenSpan().innerHTML;
}

function trNd_SetChildrenHTML( l_htmlChildren )
{
	this.GetChildrenSpan().innerHTML = l_htmlChildren;
}


function trNd_SelectNode(l_blnSelect)
{		
	var l_span = document.all("TitleSpanOf" + this.Id);
	l_span.style.backgroundColor = l_blnSelect ? "#ffffff" : "";	// 如果不是选中节点，使用默认颜色
}
/***** Class TreeNode definition, ends *****************/



// Get the folder object by id
function GetTreeNodeObject( l_lngFolderId )
{
	try
	{
		eval("var l_obj = window.objNode" + l_lngFolderId + ";");
		return l_obj;
	}
	catch ( e )
	{
		return false;
	}

}

// onclick event handler
function ExpandNode( l_lngFolderId )
{
	var l_objNode = GetTreeNodeObject( l_lngFolderId );

	if ( l_objNode )
	{
		if ( !l_objNode.IsLoaded )
		{
			l_objNode.Load();
		}

		l_objNode.Expand();
	}
}

function ExpandAndView( nNodeId )
{
	if(GetTreeNodeObject(nNodeId).ChildrenCount > 0)
		ExpandNode(nNodeId);

	ViewContent(nNodeId);
}



function SetSelectedNode(l_lngNodeId)
{	
	if(g_SelectedNode && g_SelectedNode != objNode3)
		g_SelectedNode.SelectNode(false);

	g_SelectedNode = GetTreeNodeObject(l_lngNodeId);

	if(g_SelectedNode && g_SelectedNode != objNode3)
	{
		g_SelectedNode.SelectNode(true);
	}
}

function ExpandToAndSelect(l_parentId, l_nodeId)
{	
	var l_objParent = GetTreeNodeObject( l_parentId );
	if(!l_objParent.IsExpanded)
	{
		ExpandNode(l_parentId);
	}

	var l_objNode = GetTreeNodeObject(l_nodeId);
	if(l_objNode)
	{
		SetSelectedNode(l_nodeId);
	}
	else
	{
		window.setTimeout("ExpandToAndSelect(" + l_parentId + "," +  l_nodeId + ")", 100);
	}
}

function SelectPreviousNode()
{
	if(!g_SelectedNode) g_SelectedNode = g_rootNode;

	if(g_SelectedNode == g_rootNode)
	{
		alert("对不起，当前已是第一个节点，无法往前翻页了！");
		return false;
	}

	var l_objNode = g_SelectedNode;
	if( l_objNode.Index == 1)	//如果该节点为兄弟节点中第一个，其Privious节点为其父节点。
	{		
			l_objNode = l_objNode.ParentNode;
	}
	else						//否则，为其上一个兄弟节点的最后一个子节点。
	{	
		l_objNode = l_objNode.ParentNode.Children[l_objNode.Index - 2];
		
		while(l_objNode.ChildrenCount > 0 )
		{
			if(!l_objNode.IsLoaded)
			{
				l_objNode.LoadAll();
				window.setTimeout("SelectPreviousNode()", 100);
				return;
			}
			else
			{
				if(!l_objNode.IsExpanded) l_objNode.Expand();
				l_objNode = l_objNode.Children[l_objNode.Children.length - 1];
			}
		}
	}			
	
	ViewContent(l_objNode.Id);
}

function SelectNextNode()
{
	if(!g_SelectedNode) g_SelectedNode = g_rootNode;
	var l_objNode = g_SelectedNode;
	
	if(l_objNode.ChildrenCount > 0)	//如果该节点有子节点，该节点的Next节点为其第一个子节点
	{	
		if(!l_objNode.IsLoaded)
		{	
			l_objNode.Load();
			window.setTimeout("SelectNextNode()", 100);
			return;
		}
		else
		{		
			if(!l_objNode.IsExpanded)l_objNode.Expand();
			l_objNode = l_objNode.Children[0];
		}
	}
	else
	{	
		if(l_objNode.Index == l_objNode.ParentNode.Children.length)	//如果该节点为兄弟节点中的最后一个节点，遍历寻找其不是最后一个节点的父节点的父接点的兄弟节点
		{	
			var l_objNotLastParent = l_objNode.ParentNode;

			while((l_objNotLastParent != g_rootNode) && (l_objNotLastParent.Index == l_objNotLastParent.ParentNode.Children.length))
				l_objNotLastParent = l_objNotLastParent.ParentNode;

			if(l_objNotLastParent == g_rootNode)
				alert("对不起，当前已是最后一个节点，无法往前翻页了！");
			else
				l_objNode = l_objNotLastParent.ParentNode.Children[l_objNotLastParent.Index];
		}
		else
		{	
			l_objNode = l_objNode.ParentNode.Children[l_objNode.Index];
		}
	}
	ViewContent(l_objNode.Id);
}
