/*===============================================================================

					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 ImageFolder = new String("images/template");
var LeftIndent = 20;
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;
var alertCount=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 style = \"MARGIN-RIGHT: 0px; MARGIN-LEFT:" + LeftIndent + "px; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; DISPLAY: none;\">";
		}else{
			tmpHTML = "<blockquote style = \"MARGIN-RIGHT: 0px; MARGIN-LEFT:" + LeftIndent + "px; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px;\">";
		}
	}
	MenuCount++;
}

function AddStandardItem(Name,ItemData,StaticImg,ItemCursor,Event,oName)
{
	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 + "\" onclick = \"ParentFold(this)\"  class = \"ParentNode\" style =\"cursor: " + ItemCursor + ";\"><img alt = \"Parent Node\" src=\"" + ImageFolder + "/" + FoldIn + "\" abs = \"middle\"></span>&nbsp;<span id='text_" + oName +"' " + Event + ">" + ItemData + "</span><br />";
			tmpHTML += "%" + Name + "%";
		}else{
			tmpHTML += "<span id = \"" + Name + "\" onclick = \"ParentFold(this)\"  class = \"ParentNode\" style =\"cursor: " + ItemCursor + ";\"><img alt = \"Parent Node\" src=\"" + ImageFolder + "/" + FoldOut + "\" abs = \"middle\"></span>&nbsp;<span id='text_" + oName +"' " + Event + ">" + ItemData + "</span><br />";
			tmpHTML += "%" + Name + "%";
		}
	}else{
		tmpHTML += "<span id='text_" + oName +"' class = \"ChildNode\" " + Event + " style =\"cursor: " + ItemCursor + ";\"><img alt = \"Child Node\" src=\"" + StaticImg + "\" abs = \"middle\">&nbsp;" + ItemData + "</span><br />";
	}
}

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;
			
		}
	}
}		