/***********************************************************************************
*
* $Workfile: tree.js $ 
* SafeFile : $Archive: /ContentSystem/HelpSystem/Source/HelpSystemWeb/Scripts/tree.js $
* 
* $Revision: 0 $ 
*
* Purpose : 
*
* Modified : $Author: $ $Modtime: $
*
* Initial version by Alexey Kropotin (Hound) on Oct, 2004
* Copyright 2005 Saxo Bank. All rights reserved.
***********************************************************************************/

// Definition of class Folder 
// *****************************************************************

function Folder(folderDescription, folderid, Link)//constructor 
{
	//constant data
	this.desc = folderDescription
	this.Link = Link
	this.folderid = folderid
	
	this.id = -1
	this.navObj = 0
	this.iconImg = 0
	this.nodeImg = 0
	this.isLastNode = 0
	
	//dynamic data
	this.isOpen = true
	this.iconSrc = GetFolderIcon //folderOpenImg
	this.children = new Array
	this.nChildren = 0
	this.parent = null
	
	//methods
	this.initialize = initializeFolder
	this.setState = setStateFolder
	this.addChild = addChild
	this.createIndex = createEntryIndex
	this.hide = hideFolder
	this.display = display
	this.renderOb = drawFolder
	this.totalHeight = totalHeight
	this.subEntries = folderSubEntries
	this.outputLink = outputFolderLink
	this.folderLink = BuildFolderLinkToSourse
	this.refToContent = BuildRefToContentPage
	this.isFirstNode = isFirstNode
}

function setStateFolder(isOpen)
{
	var subEntries
	var totalHeight
	var fIt = 0
	var i=0
	
	if (isOpen == this.isOpen) return
	
	this.isOpen = isOpen
	propagateChangesInState(this)
}
 
function propagateChangesInState(folder)
{
	var i=0
	
	if (folder.isOpen)
	{
	if(folder.nodeImg)
		if(folder.isLastNode)
			folder.nodeImg.src = mlastNodeImg
		else
			if(folder.isFirstNode())
				folder.nodeImg.src = mfirstNodeImg
			else
				folder.nodeImg.src = mnodeImg
			
		folder.iconImg.src = folder.iconSrc(true) //folder.iconSrc(true, folder.id) //folderOpenImg		
		for (i=0; i<folder.nChildren; i++)
		folder.children[i].display()
	}
	else
	{
		if (folder.nodeImg)
			if (folder.isLastNode)
				folder.nodeImg.src = plastNodeImg
			else
				if(folder.isFirstNode())
					folder.nodeImg.src = pfirstNodeImg
				else
					folder.nodeImg.src = pnodeImg
				
		folder.iconImg.src = folder.iconSrc(false) //folder.iconSrc(false, folder.id) //folderClosedImg
				
		for (i=0; i<folder.nChildren; i++)
			folder.children[i].hide()
	}
}

function hideFolder()
{
	if (this.navObj.style.display == "none")
		return
	this.navObj.style.display = "none"
	this.setState(0)
}

function initializeFolder(level, lastNode, leftSide)
{
	var j=0 
	var i=0 
	var numberOfFolders 
	var numberOfDocs 
	var nc 
    nc = this.nChildren 
   
	this.createIndex() 
	var auxEv = "" 
 
	auxEv = "<a href='javascript:clickOnNode("+this.id+")'>" 
	
	if (level>0)
	{
		if (lastNode) //the last 'brother' in the children array
		{
			this.renderOb(leftSide + auxEv + "<img name='nodeIcon" + this.id + "' src='"+mlastNodeImg+"' width=16 height=22 border=0></a>")
			leftSide = leftSide + "<img src='"+blankImg+"' width=16 height=22>"
			this.isLastNode = 1
		}
		else
		{
			this.renderOb(leftSide + auxEv + "<img name='nodeIcon" + this.id + "' src='"+mnodeImg+"' width=16 height=22 border=0></a>") 
			leftSide = leftSide + "<img src='"+vertLineImg+"' width=16 height=22>"
			this.isLastNode = 0
		}
	}
	else
		this.renderOb("")
	
	if (nc > 0)
	{
		level = level + 1 
		for (i=0 ; i < this.nChildren; i++)  
		{ 
			if (i == this.nChildren-1)
				this.children[i].initialize(level, 1, leftSide)
			else
				this.children[i].initialize(level, 0, leftSide)
		}
	}
}

function drawFolder(leftSide) 
{ 
	doc.write("<table id='folder" + this.id + "' style='position:block;' ") 
	doc.write(" border=0 cellspacing=0 cellpadding=0>") 
	doc.write("<tr valign='center'><td>") 
	doc.write(leftSide) 
	
	doc.write("<img name='folderIcon" + this.id + "'") 
	doc.write("src='" + this.iconSrc() + "' border=0></a>")	
	doc.write("</td><td nowrap=false>")
	this.outputLink() 
	
	doc.write("</td>")  
	doc.write("</table>") 
	
	this.navObj = doc.all["folder"+this.id]
	this.iconImg = doc.all["folderIcon"+this.id]
	this.nodeImg = doc.all["nodeIcon"+this.id] 
} 
 
function outputFolderLink() 
{ 
	auxEv = "<a href='javascript:clickOnNode("+this.id+")'>" 
	if(this.Link.length != 0)
	{
		var ref = this.refToContent(this.Link)
		doc.write(this.folderLink(ref, "<span id='span" + this.id + "' class = 'TOCLinkStyle'>" + this.desc + "</span>", this.id))
	}
	else
	{
	  if (this.nChildren > 0)
	  {
		doc.write(auxEv + "<span class = 'TOCLinkStyle'>" + this.desc + "</span></a>")
	  }
	  else
	  {
		doc.write("<span class = 'TOCLinkStyle'>" + this.desc + "</span>")
	  }
		
    }
//  doc.write("<a href='javascript:clickOnFolder("+this.id+")'>")
}

function BuildFolderLinkToSourse(href, description, id)
{
	return "<a href='"+ href +"' target='"+ contentframename +"'" +
	" onClick='javascript:clickOnFolder(" + id + ")'>"+ description +"</a>"
}

function addChild(parentFolder, childNode)
{
	this.children[this.nChildren] = childNode
	this.nChildren++
	childNode.parent = parentFolder
	return childNode
}

function folderSubEntries()
{
	var i = 0
	var se = this.nChildren

	for (i=0; i < this.nChildren; i++)
	{
		if (this.children[i].children) //is a folder
		se = se + this.children[i].subEntries()
    }
    return se
}

function GetFolderIcon(isFolderOpen)
{
	isClosedDefFld = (new String(this.iconImg.src)).search(defFolderClosedImg)
	isOpenDefFld = (new String(this.iconImg.src)).search(defFolderOpenImg)
	
	if((isClosedDefFld != -1) || (isOpenDefFld != -1))
	{
		if(isFolderOpen == true)
			return defImagesFolderPath + defFolderOpenImg
		else
			return defImagesFolderPath + defFolderClosedImg
	}
	try
	{
		return eval("item" + this.folderid + "icon")
	}
	catch(e)
	{
		return defImagesFolderPath + defFolderClosedImg
	}
}

function isFirstNode()
{
	return (this.id == 1)? 1 : 0;
}

// Definition of class Item (a document or link inside a Folder) 
// *************************************************************

function Item(itemDescription, itemid, Link) // Constructor
{
	// constant data
	this.desc = itemDescription
	this.Link = Link
	this.itemid = itemid
	
	this.id = -1 //initialized in initalize()
	this.navObj = 0 //initialized in render()
	this.iconImg = 0 //initialized in render()
	this.iconSrc = GetItemIcon
	this.parent = null
	
	// methods
	this.initialize = initializeItem
	this.createIndex = createEntryIndex
	this.hide = hideItem
	this.display = display
	this.renderOb = drawItem
	this.totalHeight = totalHeight
	this.outputLink = outputItemLink
	this.itemLink = BuildItemLinkToSourse
	this.refToContent = BuildRefToContentPage
	
	this.select = selectLink
	this.isFirstNode = isFirstNode
}

function hideItem()
{
	if (this.navObj.style.display == "none")
		return
	this.navObj.style.display = "none" 
} 
 
function initializeItem(level, lastNode, leftSide) 
{
	this.createIndex() 
 
	if (level>0)
	{
		if(lastNode) //the last 'brother' in the children array 
		{ 
			this.renderOb(leftSide + "<img src='"+lastnodeImg+"' width=16 height=22>") 
			leftSide = leftSide + "<img src='"+blankImg+"' width=16 height=22>"  
		}
		else 
		{
			if(this.isFirstNode())
			{
				this.renderOb(leftSide + "<img src='"+firstnodeImg+"' width=16 height=22>") 
				leftSide = leftSide + "<img src='"+blankImg+"' width=16 height=22>" 
			}
			else
			{
				this.renderOb(leftSide + "<img src='"+nodeImg+"' width=16 height=22>")
				leftSide = leftSide + "<img src='"+vertLineImg+"' width=16 height=22>"
			}
		}
	
	}
	else
	{
		this.renderOb("")
	}   
}
 
function selectLink()
{

} 

function clickItem(i)
{
	t = document.getElementById("span"+i)
	if (t != null && t != currentLink)
	{
		t.className = 'Selected'
		//t.style.backgroundColor = 'blue'
		if (currentLink != null)
		{
			currentLink.className = 'TOCLinkStyle'
		//  currentLink.style.backgroundColor = 'white'
		}
		currentLink = t
	}
}
 
function drawItem(leftSide)
{	
	doc.write("<table onclick='javascript:clickItem(" + this.id + ")'") 
	doc.write(" id='item" + this.id + "' style='position:block;' ")
	doc.write(" border=0 cellspacing=0 cellpadding=0>")
	doc.write("<tr><td>")
	doc.write(leftSide)
	
	if(this.Link.length != 0)
	{	
		doc.write("<a href=" + this.refToContent(this.Link) 
								+ " target="+ contentframename +" >")
		doc.write("<img id='itemIcon"+this.id+"' ")
	
		iconPath = "src='"+ this.iconSrc() + "' border=0>"
		doc.write(iconPath)
		doc.write("</a>")	
	}
	else
	{
		doc.write("<img id='itemIcon"+this.id+"' src='"+ this.iconSrc() + "' border=0>")
	}
	
	doc.write("</td><td valign=middle nowrap>")	
	this.outputLink()
    doc.write("</table>") 
	
	this.navObj = doc.all["item"+this.id]
	this.iconImg = doc.all["itemIcon"+this.id]
	
}

function BuildItemLinkToSourse(href, description)
{
	return "<a class='TOCLinkStyle' href='"+ href +"' target='"+ contentframename +"'>"+ description +"</a>"
}

function GetItemIcon()
{
	if(this.id != 0)
	{
		//path = eval("item" + this.id + "icon")
		//return eval("item" + this.itemLink + "icon")
		
		try
		{
			return eval("item" + this.itemid + "icon")
		}
		catch(e)
		{
			return defImagesFolderPath + defItemImg
		}
	}
	//return docImg
}

// Methods common to both objects (pseudo-inheritance) 
// ******************************************************** 

function outputItemLink()
{
	if(this.Link.length != 0)
	{
		var ref = BuildRefToContentPage(this.Link)
		doc.write(this.itemLink(ref, "<span id='span" + this.id + "' class = 'TOCLinkStyle'>" + this.desc + "</span>", this.id))
	}
	else
	{
		doc.write("<span id='span" + this.id + "' class = 'TOCLinkStyle'>" + this.desc + "</span>")
	}
}

function display(isNonVisible) 
{
	if(isNonVisible == false)
		this.navObj.style.display = "none"
	else
		this.navObj.style.display = "block"
}

function createEntryIndex()
{
	this.id = nEntries
	indexOfEntries[nEntries] = this
	nEntries++ 
}

// total height of subEntries open
function totalHeight() //used with browserVersion == 2
{
	var h = this.navObj.clip.height
	var i = 0
	
	if (this.isOpen) //is a folder and _is_ open
	{
		for (i=0 ; i < this.nChildren; i++)
		{
			h = h + this.children[i].totalHeight()
		}
	}
	return h
}

function BuildRefToContentPage(contentid)
{
	var param = "Topic=" + String(contentid);
	var search = window.location.search;
	if (search.match(/topic=/i))
		search = search.replace(/topic=\d+/i, param);
	else
		search = search + "&" + param;
	
	return contentpageref + search;
}
// Events 
// ********************************************************* 
 
function clickOnFolder(folderId) 
{
	clickItem(folderId)

	var clicked = indexOfEntries[folderId]
	
	if (!clicked.isOpen) 
		clickOnNode(folderId)
	return
	
	if (clicked.isSelected)
	return 
}

function clickOnNode(folderId) 
{
	var clickedFolder = 0
	var state = 0
	
	clickedFolder = indexOfEntries[folderId]
	state = clickedFolder.isOpen
	clickedFolder.setState(!state) //open<->close  
}

function initializeDocument(nodeId)
{
	if (typeof folder0 != "undefined")
	{
		folder0.initialize(0, 1, "");
		folder0.display(false);
		
		clickOnNode(0);
	    
		if(nodeId.length != 0)
		{
			ExpandTree(nodeId);
		}
		else
		{
			clickOnNode(0);
		}
	}
} 
 
// Auxiliary Functions for Folder-Treee backward compatibility
// *********************************************************

function gFld(description, folderid, Link)
{
	return new Folder(description, folderid, Link)
}
 
function gLnk(description, itemid, Link)
{		
	return new Item(description, itemid, Link)
}
 
function insFld(parentFolder, childFolder)
{
	return (parentFolder != null) ? parentFolder.addChild(parentFolder, childFolder) : childFolder
}

function insDoc(parentFolder, document)
{
	parentFolder.addChild(parentFolder, document)
}

// Perform tree manipulations
// ***************************************************************
function ExpandTree(nodeid)
{
	var cn;
	cn = GetCurrent(nodeid);
	if (cn != null)
	{
		clickItem(cn.id);
		NodesToOpen = new Array()
		GetNodesToOpen(NodesToOpen, cn)
		var i = 0;
		for(i = 0; i < NodesToOpen.length; i++)
		{
			if((NodesToOpen[i]).isOpen != null)
			{
				clickOnNode((NodesToOpen[i]).id)
			}
		}
	}
	else
	{
		clickOnNode(0);
	}
}

function GetCurrent(nodeid)
{
	var currnode = null;
	for(i = 0; i < indexOfEntries.length; i++)
	{
		curr = indexOfEntries[i];
		if(curr.Link.toUpperCase() == nodeid.toUpperCase())
		{
			currnode = indexOfEntries[i];
			break;
		}
	}
	return currnode;
}

function GetNodesToOpen(NodesToOpen, currnode)
{
	var parentnode = null	
	
	if(currnode == null)
	{
		return NodesToOpen.push(indexOfEntries[0])
	}	
	do
	{
		NodesToOpen.push(currnode)
		currnode = currnode.parent
	}
	while(currnode != null)
	
	NodesToOpen.reverse()
	return NodesToOpen
}

// Global variables
// **************** 
defImagesFolderPath = "./images/"

plastNodeImg = defImagesFolderPath + "plastnode.gif"
mlastNodeImg = defImagesFolderPath + "mlastnode.gif"

pnodeImg = defImagesFolderPath + "pnode.gif"
mnodeImg = defImagesFolderPath + "mnode.gif"

pfirstNodeImg = defImagesFolderPath + "pfirstnode.gif"
mfirstNodeImg = defImagesFolderPath + "mfirstnode.gif"

blankImg = defImagesFolderPath + "blank.gif"
vertLineImg = defImagesFolderPath + "vertline.gif"

nodeImg = defImagesFolderPath + "node.gif"
firstnodeImg = defImagesFolderPath + "firstnode.gif"
lastnodeImg = defImagesFolderPath + "lastnode.gif"

defFolderOpenImg = "folderopen.gif"
defFolderClosedImg = "folderclosed.gif"
defItemImg = "doc.gif"

contentpageref = "Content.aspx"
contentframename = "content"

indexOfEntries = new Array
nEntries = 0 
doc = document
selectedFolder = 0

currentLink = null

