// --------------------------------------------------------------------------------------------
//
//	JavaScript file defining objects for a dynamic tree-type menu
//	
//	designed by Logical Imagination, LLC
//	www.LogicalImagination.com
//	Copyright © 2007, All Rights Reserved
//
// --------------------------------------------------------------------------------------------

// -------------------------------------------------------------------------
//
// configuration variables
//
// -------------------------------------------------------------------------
var allowToggle = false;
var initialExpansion = true;

// -------------------------------------------------------------------------
//
// general variables and functions for working with the tree
//
// -------------------------------------------------------------------------
var nextNodeID = 0;
var currNodeID = -1;
var menu = new LIMenu("Products");

function ToggleExpansion(NodeID)
{
	if (allowToggle)
	{
		var target = menu.GetNode(NodeID);
		target.Expanded = !target.Expanded;
		
		var disp = document.getElementById("LINavTree");
		disp.innerHTML = menu.buildTree();
	}
}

// -------------------------------------------------------------------------
//
// menu item object
//
// -------------------------------------------------------------------------

//constructor for a menu item object
function LIMenuItem(id, label, url, tooltip, level)
{
	this.NodeID = nextNodeID++;
	this.ID = id;
	this.Label = label;
	this.URL = url;
	this.ToolTip = tooltip;
	this.Level = level;
	this.Expanded = initialExpansion;
	this.Children = new Array();
}

// functions to work with child menu items
LIMenuItem.prototype.AddChild = function(item)
{
	//add the supplied item to end of the array of child menu items
	this.Children[this.Children.length] = item;
};

LIMenuItem.prototype.CreateChild = function(id, label, url, tooltip)
{
	var newItem = new LIMenuItem(id, label, url, tooltip, this.Level+1);
	this.AddChild(newItem);
	return newItem;
};

LIMenuItem.prototype.GetChild = function(id)
{
	for (i=0;i<this.Children.length;i++)
	{
		var item = this.Children[i];
		if (item.ID == id)
		{
			return item;
		}
	}
	return null;
};

LIMenuItem.prototype.ClearChildren = function()
{
	this.Children = new Array();
};

LIMenuItem.prototype.ExpandChild = function(id)
{
	for (i=0;i<this.Children.length;i++)
	{
		var item =this.Children[i];
		if (item.ID == id)
		{
			item.Expanded = true;
			return item;
		}
	}
	return null;
}

LIMenuItem.prototype.GetNode = function(IDToFind)
{
	var i;
	for (i=0;i<this.Children.length;i++)
	{
		var item = this.Children[i];
		if (item.NodeID == IDToFind)
		{
			return item;
		}
		else
		{
			var testItem = item.GetNode(IDToFind);
			if (testItem != null)
			{
				return testItem;
			}
		}
	}
	return null;
}

LIMenuItem.prototype.ToHTML = function(isLast, prefix, showIcons, showLabel, showTopLevelIcons, isTopLevel)
{
	var strOut = '';
	
	strOut += prefix;
	
	if (showLabel || prefix != "" || (prefix == "" && showTopLevelIcons))
	{
		if (showIcons)
		{
			// customized for Bella Jewelaria
			if (isTopLevel)
			{
				strOut += '<img src="images/menuImages/menuIcon.jpg" onClick="ToggleExpansion(' + this.NodeID + ')" /><img src="images/spacer.gif" width="1" height="18" />';
			}
			else
			{
				strOut += '<img src="images/spacer.gif" width="14" height="18" />';
			}
			//if (this.Children.length > 0)
			//{
			//	if (this.Expanded)
			//	{
			//		if (isLast)
			//		{
			//			strOut += '<img src="images/menuImages/minusbottom.gif" onClick="ToggleExpansion(' + this.NodeID + ')" />';
			//		}
			//		else
			//		{
			//			strOut += '<img src="images/menuImages/minus.gif" onClick="ToggleExpansion(' + this.NodeID + ')"/>';
			//		}
			//	}
			//	else
			//	{
			//		if (isLast)
			//		{
			//			strOut += '<img src="images/menuImages/plusbottom.gif" onClick="ToggleExpansion(' + this.NodeID + ')"/>';
			//		}
			//		else
			//		{
			//			strOut += '<img src="images/menuImages/plus.gif" onClick="ToggleExpansion(' + this.NodeID + ')"/>';
			//		}
			//	}
			//}
			//else
			//{
			//	if (isLast)
			//	{
			//		strOut += '<img src="images/menuImages/joinbottom.gif"/>';
			//	}
			//	else
			//	{
			//		strOut += '<img src="images/menuImages/join.gif"/>';
			//	}
			//}
		}
		else
		{
			//we're not showing icons, so insert a space
			strOut += '<img src="images/spacer.gif" width="14" height="18"/>';
		}
	}
	
	
	if (this.URL != "")
	{
		if (this.NodeID == currNodeID)
		{
			strOut += '<a class="nodeSel" href="' + this.URL + '" id="' + this.NodeID + '"';
		}
		else
		{
			strOut += '<a class="node" href="' + this.URL + '" id="' + this.NodeID + '"';
		}
		if (this.ToolTip != "")
		{
			strOut += ' title="' + this.ToolTip + '"';
		}
		if (this.NodeID == currNodeID)
		{
			//customized for Bella Jewelaria
			strOut += '>' + this.Label + '</a><br>';
			//strOut += '>&raquo;' + this.Label + '</a><br>';
		}
		else
		{
			strOut += '>' + this.Label + '</a><br>';
		}
	}
	else
	{
		if (this.NodeID == currNodeID)
		{
			strOut += '<span class="nodeSel" id="' + this.NodeID + '">&raquo;' + this.Label + '</span><br>';
		}
		else
		{
			strOut += '<span class="node" id="' + this.NodeID + '">' + this.Label + '</span><br>';
		}
	}
	
	
	var newPrefix;
	if (!showIcons || (this.Children.length > 0 && this.Expanded))
	{
		if (isLast || !showIcons)
		{
			newPrefix = prefix + '<img src="images/spacer.gif" width="14" height="18"/>';
		}
		else
		{
			//customized for Bella Jewelaria
			newPrefix = prefix + '<img src="images/spacer.gif" width="14" height="18"/>';
			//newPrefix = prefix + '<img src="images/menuImages/line.gif"/>';
		}
		
		var i;
		for (i=0;i<this.Children.length;i++)
		{
			var item = this.Children[i];
			if (i == this.Children.length - 1)
			{
				strOut += item.ToHTML(true, newPrefix, showIcons, showLabel, showTopLevelIcons, false);
			}
			else
			{
				strOut += item.ToHTML(false, newPrefix, showIcons, showLabel, showTopLevelIcons, false);
			}
		}
	}
	
	
	return strOut;
}



// -------------------------------------------------------------------------
//
// menu object
//
// -------------------------------------------------------------------------

//constructor for a menu object
function LIMenu(label)
{
	this.Label = label;
	this.MenuItems = new Array();
	this.DisplayLabel = true;
	this.DisplayIcons = true;
}

// functions to work with main menu items
LIMenu.prototype.AddMenuItem = function(item)
{
	//add the supplied item to end of the array of child menu items
	this.MenuItems[this.MenuItems.length] = item;
};

LIMenu.prototype.CreateMenuItem = function(id, label, url, tooltip)
{
	var newItem = new LIMenuItem(id, label, url, tooltip, 0);
	this.AddMenuItem(newItem);
	return newItem;
};

LIMenu.prototype.GetMenuItem = function(id)
{
	for (i=0;i<this.MenuItems.length;i++)
	{
		var item = this.MenuItems[i];
		if (item.ID == id)
		{
			return item;
		}
	}
	return null;
};

LIMenu.prototype.ClearMenuItems = function()
{
	this.MenuItems = new Array();
};

LIMenu.prototype.ExpandMenuItem = function(id)
{
	for (i=0;i<this.MenuItems.length;i++)
	{
		var item = this.MenuItems[i];
		if (item.ID == id)
		{
			item.Expanded = true;
			return item;
		}
	}
	return null;
}

LIMenu.prototype.GetNode = function(IDToFind)
{
	var i;
	for (i=0;i<this.MenuItems.length;i++)
	{
		var item = this.MenuItems[i];
		if (item.NodeID == IDToFind)
		{
			return item;
		}
		else
		{
			var testItem = item.GetNode(IDToFind);
			if (testItem != null)
			{
				return testItem;
			}
		}
	}
	return null;
}

LIMenu.prototype.SetCurrent = function(node)
{
	currNodeID = node.NodeID;
}

LIMenu.prototype.HasGrandchildren = function()
{
	var i;
	for (i=0;i<this.MenuItems.length;i++)
	{
		var item = this.MenuItems[i];
		if (item.Children.length > 0)
		{
			return true;
		}
	}
	return false;
}

//output the HTML necessary to display the menu tree
LIMenu.prototype.ToHTML = function()
{
	var strOut = '<div class="LITree" id="LINavTree">\n';
	
	strOut += this.buildTree();
	strOut += '</div>';
	return strOut;
}

LIMenu.prototype.buildTree = function()
{
	var strOut = "";
	
	if (this.DisplayLabel)
	{
		strOut = '<span class="LITreeNode">' + this.Label + '</span><br>';
	}
	
	//add the menu items
	var i;
	for (i=0;i<this.MenuItems.length;i++)
	{
		var item = this.MenuItems[i];
		
		if (i == this.MenuItems.length - 1)
		{
			strOut += item.ToHTML(true, '', this.DisplayIcons, this.DisplayLabel, this.HasGrandchildren, true);
		}
		else
		{
			strOut += item.ToHTML(false, '', this.DisplayIcons, this.DisplayLabel, this.HasGrandchildren, true);
		}
	}
	
	return strOut;
}








