/*===============================================================================

					Tree view menu created by Giles Papworth

===============================================================================*/

var FoldOut = new String("FoldOut.gif");
var FoldIn = new String("FoldIn.gif");
var StaticImage = new String("ListItem.gif");
var SelectedImage = new String("Selected.gif");
var ImageFolder = new String("Images/general");
var TreeStarted = false;
var TreeParentName = new String();
var TreeNames = new Array();
var tmpHTML = new String();
var TreeHTML = new String();
var HasAbsoluteParent = false;

var AllIn = false;

var MenuCount = 0;

function NewTree(ParentItem)
{
	if(!ParentItem || ParentItem == ""){
		ParentItem = "NoParent";
	}
	if(TreeStarted == true){
		alert("You are still building a tree, please end the tree before starting another one");
		return false;
	}
	if(ParentItem != "NoParent"){
		if(CheckTreeNames(ParentItem) == false){
			alert("The Parent you are trying to add this tree to does not exist, please make sure you have created it and then put the code AFTER the parent tree code");
			return false;
		}else{
			TreeParentName = ParentItem;
		}
	}
	TreeStarted = true;
	if(TreeParentName != ""){
		if(AllIn == true && MenuCount > 0){
			tmpHTML = "<blockquote class = \"NodeMargin\" style = \"DISPLAY: none;\">";
		}else{
			tmpHTML = "<blockquote class = \"NodeMargin\">";
		}
	}
	MenuCount++;
}

function AddStandardItem(Name,ItemData,StaticImg,ItemCursor,Event,IsSelected)
{
	var IsParent = false;
	if(ItemCursor == "" || !ItemCursor){
		ItemCursor = "default";
	}
	if(Event != ""){
		Event = "onclick = \"" + Event + "\"";
	}
	if(!ItemData || ItemData == ""){
		alert("You must enter in some data for the item!");
		return false;
	}
	if(StaticImg == "" || !StaticImg){
		StaticImg = ImageFolder + "/" + StaticImage;
	}else{
		StaticImg = ImageFolder + "/" + StaticImg;
	}
	if(!Name || Name == ""){
		IsParent = false;
	}else{
		if(CheckTreeNames(Name) == true){
			alert("A tree item with this name already exists, please rename it");
			return false;
		}else{
			IsParent = true;
			
		}	
	}
	if(IsParent == true){
		AddTreeName(Name);
		if(AllIn == true){
			tmpHTML += "<span id = \"" + Name + "\" class = \"ParentNode\" onmouseover=\"className='ParentNode_Hover'\" onmouseout=\"className='ParentNode'\" style =\"cursor: " + ItemCursor + ";\"><div class = \"NodeSpacing\"><img alt = \"\" src=\"" + ImageFolder + "/" + FoldIn + "\" align=\"top\" onclick = \"ParentFold(document.all('" + Name + "'));\"><label " + Event + " style =\"cursor: " + ItemCursor + ";\">";
			if(IsSelected == true){
				tmpHTML += "<b>" + ItemData + "</b>";
			}else{
				tmpHTML += ItemData;
			}			
			tmpHTML += "</label></div></span>";
			tmpHTML += "%" + Name + "%";
		}else{
			tmpHTML += "<span id = \"" + Name + "\" class = \"ParentNode\" onmouseover=\"className='ParentNode_Hover'\" onmouseout=\"className='ParentNode'\" style =\"cursor: " + ItemCursor + ";\"><div class = \"NodeSpacing\"><img alt = \"\" src=\"" + ImageFolder + "/" + FoldOut + "\" align=\"top\" onclick = \"ParentFold(document.all('" + Name + "'));\"><label " + Event + " style =\"cursor: " + ItemCursor + ";\">";
			if(IsSelected == true){
				tmpHTML += "<b>" + ItemData + "</b>";
			}else{
				tmpHTML += ItemData;
			}			
			tmpHTML += "</label></div></span>";
			tmpHTML += "%" + Name + "%";
		}
	}else{
		tmpHTML += "<span class = \"ChildNode\" onmouseover=\"className='ChildNode_Hover'\" onmouseout=\"className='ChildNode'\" " + Event + " style =\"cursor: " + ItemCursor + ";\"><div class = \"NodeSpacing\">";
		if(IsSelected == true){
			tmpHTML += "<img alt = \"\" src=\"" + ImageFolder + "/" + SelectedImage + "\" align=\"top\"><b>" + ItemData + "</b>";
		}else{
			tmpHTML += "<img alt = \"\" src=\"" + ImageFolder + "/" + StaticImage + "\" align=\"top\">" + ItemData;
		}
		tmpHTML += "</div></span>";
	}
}

function EndTree()
{
	var rExpression;
	var rConstruct;
	if(TreeStarted != true){
		alert("you have not started a tree yet, please start one before trying to finish one");
		return false;
	}
	if(TreeParentName != ""){
		tmpHTML += "</blockquote>";
	}
	//tmpHTML += "</ul>";
	if(TreeParentName != ""){
		rConstruct = "%" + TreeParentName + "%";
		rExpression = rConstruct;
		TreeHTML = TreeHTML.replace(rExpression, tmpHTML);
	}else{
		TreeHTML += tmpHTML;
	}
	TreeStarted = false;
	TreeParentName = "";
	tmpHTML = "";
}

function GenerateTreeCode(Owner){
	if(TreeHTML != ""){
		if(!Owner || Owner == ""){
			document.write(TreeHTML);
		}else{
			document.getElementById(Owner).innerHTML = TreeHTML;
		}
	}else{
		alert("NO TREE CODE CREATED!");
	}
	TreeHTML = "";
	MenuCount = 0;
}


function CheckTreeNames(TreeName)
{
	var FoundName = false;
	if(TreeNames.length > 0){
		for(var i = 0; i<TreeNames.length;i++)
		{
			if(TreeName == TreeNames[i])
			{
				FoundName = true;
			}
		}
	}
	return FoundName;
}

function AddTreeName(TreeName)
{
	if(TreeNames.length == 0)
	{
		TreeNames[0] = TreeName;
	}else{
		TreeNames[parseInt(TreeNames.length)] = TreeName;
	}
}

function ParentFold(Item)
{
	var ChildItem = new String();
	var TotalLoop = 0;
	var ImageConstructo = ImageFolder + "/" + FoldOut;
	var ImageConstructi = ImageFolder + "/" + FoldIn;
	ChildItem = Item.nextSibling;
	do{
		TotalLoop++;
		if(ChildItem.tagName != "BLOCKQUOTE"){
			ChildItem = ChildItem.nextSibling;
		}
		if(TotalLoop > 5){
			break;
		}
	}while(ChildItem.tagName != "BLOCKQUOTE");
	if(ChildItem.tagName == "BLOCKQUOTE"){
		if(ChildItem.style.display == "none"){
			ChildItem.style.display = "";
			Item.getElementsByTagName("IMG").item(0).src = ImageConstructo;
		}else{
			ChildItem.style.display = "none";
			Item.getElementsByTagName("IMG").item(0).src = ImageConstructi;
			
		}
	}
}

function Jump(psURL)
{
	window.location.href = psURL;
}			
