// Global variables
var COMBOBOXSELECTIONMENUS = new Array();

// ComboBoxSelectionMenu class definition
function ComboBoxSelectionMenu(name)
{
	// private members
    this._menuitemnames = new Array();
    this._menuitemindex = -1;
    this._menuitemcombobox = null;
    this._submenuitemcombobox = null;
    this._hyperlinklist = null;

	// add this instance to the global COMBOBOXSELECTIONMENUS variable and store its index
    this._globalindex = COMBOBOXSELECTIONMENUS.length;
    COMBOBOXSELECTIONMENUS[this._globalindex] = this;

    this.Name = name;
    this.MenuItems = new Array();
}
ComboBoxSelectionMenu.prototype.AddMenuItem = function(name)
{
    this._menuitemnames[this._menuitemnames.length] = name;
    this.MenuItems[name] = new MenuItem(name, this);
}
ComboBoxSelectionMenu.prototype.Render = function(parentcontrolid)
{
	document.write("<ul class=\"MenuItems\">");
	
	// Run through menu items	
	for (var menuitemindex = 0; menuitemindex < this._menuitemnames.length; menuitemindex++)
	{
		var menuitemname = this._menuitemnames[menuitemindex];
		var menuitem = this.MenuItems[menuitemname];

		document.write("<li>");
		document.write(RemoveTail(menuitem.Name));

		document.write("<ul class=\"SubMenuItems\">");
		
		// Run through sub menu items	
		for (var submenuitemindex = 0; submenuitemindex < menuitem._submenuitemnames.length; submenuitemindex++)
		{
			var submenuitemname = menuitem._submenuitemnames[submenuitemindex];
			var submenuitem = menuitem.SubMenuItems[submenuitemname];

			document.write("<li>");
			document.write(RemoveTail(submenuitem.Name));

			document.write("<ul class=\"Hyperlinks\">");

			// Run through hyperlink items	
			for (var hyperlinkindex = 0; hyperlinkindex < submenuitem._hyperlinknames.length; hyperlinkindex++)
			{
				var hyperlinkname = submenuitem._hyperlinknames[hyperlinkindex];
				var hyperlink = submenuitem.Hyperlinks[hyperlinkname];

				document.write("<li><a href=\"" + hyperlink.URL + "\" target=\"_self\">" + RemoveTail(hyperlink.Name) + "</a></li>");
			}

			document.write("</ul>");

			document.write("</li>");
		}
		
		document.write("</ul>");

		document.write("</li>");
	}

	document.write("</ul>");
}
ComboBoxSelectionMenu.prototype.Generate = function(parentcontrolid)
{
	if (parentcontrolid == null)
	{
		throw Error("Can not generate ComboBoxSelectionMenu. Parent control ID not supplied as function parameter.")
	}
	
	var parentcontrol = document.getElementById(parentcontrolid);
	if (parentcontrol == null)
	{
		throw Error("Can not generate ComboBoxSelectionMenu. Parent control does not exist.")
	}
	
	// generate menuitem combobox
	this._menuitemcombobox = document.createElement("select");
	this._menuitemcombobox.className = "MenuItemComboBox";
	parentcontrol.appendChild(this._menuitemcombobox);	
	
	// set ComboBoxSelectionMenuGlobalIndex attribute to be able to locate the comboboxselectionmenu instance later on
	this._menuitemcombobox.setAttribute("ComboBoxSelectionMenuGlobalIndex", this._globalindex);
	
	// add event handler
    this._menuitemcombobox.onchange = function ()
    {
		// NOTE: WERE IN THE COMBOBOX OBJECT SCOPE NOW!
		var comboboxselectionmenu = COMBOBOXSELECTIONMENUS[parseInt(this.getAttribute("ComboBoxSelectionMenuGlobalIndex"))];
		comboboxselectionmenu._menuitemindex = this.selectedIndex;

		var selectedmenuitemname = comboboxselectionmenu._menuitemnames[comboboxselectionmenu._menuitemindex];
		if (selectedmenuitemname == null) return;
		
		var selectedmenuitem = comboboxselectionmenu.MenuItems[selectedmenuitemname];
		if (selectedmenuitem == null) return;

		// reset the submenuitemindex for this item
		selectedmenuitem._submenuitemindex = -1;

		comboboxselectionmenu.OnChangeMenuItemComboBox();
    }
    if (this._menuitemcombobox.captureEvents)
    {
		this._menuitemcombobox.captureEvents(Event.ONCHANGE);
	}


	// generate sub menuitem combobox
	this._submenuitemcombobox = document.createElement("select");
	this._submenuitemcombobox.className = "SubMenuItemComboBox";
	parentcontrol.appendChild(this._submenuitemcombobox);	
	
	// set ComboBoxSelectionMenuGlobalIndex attribute to be able to find the comboboxselectionmenu later on
	this._submenuitemcombobox.setAttribute("ComboBoxSelectionMenuGlobalIndex", this._globalindex);

	// add event handler
    this._submenuitemcombobox.onchange = function ()
    {
		// NOTE: WERE IN THE COMBOBOX OBJECT SCOPE NOW!
		var comboboxselectionmenu = COMBOBOXSELECTIONMENUS[parseInt(this.getAttribute("ComboBoxSelectionMenuGlobalIndex"))];

		if (comboboxselectionmenu._menuitemindex == -1)
		{
			comboboxselectionmenu._menuitemindex = 0;
		}
		var selectedmenuitemname = comboboxselectionmenu._menuitemnames[comboboxselectionmenu._menuitemindex];
		if (selectedmenuitemname == null) return;
		
		var selectedmenuitem = comboboxselectionmenu.MenuItems[selectedmenuitemname];
		if (selectedmenuitem == null) return;

		selectedmenuitem._submenuitemindex = this.selectedIndex;

		comboboxselectionmenu.OnChangeSubMenuItemComboBox();
    }
    if (this._submenuitemcombobox.captureEvents)
    {
		this._submenuitemcombobox.captureEvents(Event.ONCHANGE);
	}


	// generate hyperlink layer
	this._hyperlinklist = document.createElement("ul");
	this._hyperlinklist.className = "Hyperlinklist";
	parentcontrol.appendChild(this._hyperlinklist);	
	

	// update the complete control
	this.OnChangeComboBoxSelectionMenu();
}
ComboBoxSelectionMenu.prototype.OnChangeComboBoxSelectionMenu = function()
{
	// clear the combobox first
	while (this._menuitemcombobox.hasChildNodes())
	{
		this._menuitemcombobox.removeChild(this._menuitemcombobox.firstChild);
	}
	
	// Run through menu items	
	for (var menuitemindex = 0; menuitemindex < this._menuitemnames.length; menuitemindex++)
	{
		var menuitemname = this._menuitemnames[menuitemindex];
		var menuitem = this.MenuItems[menuitemname];

		// add the menu item option element and set its value and text
		var menuitemoptionelement = document.createElement("option");
		menuitemoptionelement.className = "MenuItem";
		this._menuitemcombobox.appendChild(menuitemoptionelement);
		menuitemoptionelement.setAttribute("value", menuitemindex);
		menuitemoptionelement.appendChild(document.createTextNode(RemoveTail(menuitem.Name)));
	}

	// if needed, reset the menuitemindex
	if ((this._menuitemindex == -1) && (this._menuitemnames.length > 0))
	{
		this._menuitemindex = 0;
	}
	this._menuitemcombobox.selectedIndex = this._menuitemindex;
	
	this.OnChangeMenuItemComboBox();
}
ComboBoxSelectionMenu.prototype.OnChangeMenuItemComboBox = function()
{
	// clear the submenuitem combobox first
	while (this._submenuitemcombobox.hasChildNodes())
	{
		this._submenuitemcombobox.removeChild(this._submenuitemcombobox.firstChild);
	}
	
	// get the selected menuitem
	var selectedmenuitemname = this._menuitemnames[this._menuitemindex];
	if (selectedmenuitemname == null) return;
	
	var selectedmenuitem = this.MenuItems[selectedmenuitemname];
	if (selectedmenuitem == null) return;
	
	// Run through sub menu items	
	for (var submenuitemindex = 0; submenuitemindex < selectedmenuitem._submenuitemnames.length; submenuitemindex++)
	{
		var submenuitemname = selectedmenuitem._submenuitemnames[submenuitemindex];
		var submenuitem = selectedmenuitem.SubMenuItems[submenuitemname];

		// add the menu item option element and set its value and text
		var submenuitemoptionelement = document.createElement("option");
		submenuitemoptionelement.className = "MenuItem";
		this._submenuitemcombobox.appendChild(submenuitemoptionelement);
		submenuitemoptionelement.setAttribute("value", submenuitemindex);
		submenuitemoptionelement.appendChild(document.createTextNode(RemoveTail(submenuitem.Name)));
	}

	// if needed, reset the menuitemindex
	if ((selectedmenuitem._submenuitemindex == -1) && (selectedmenuitem._submenuitemnames.length > 0))
	{
		selectedmenuitem._submenuitemindex = 0;
	}
	this._submenuitemcombobox.selectedIndex = selectedmenuitem._submenuitemindex;
	
	this.OnChangeSubMenuItemComboBox();
}
ComboBoxSelectionMenu.prototype.OnChangeSubMenuItemComboBox = function()
{
	// clear the hyperlink layer first
	while (this._hyperlinklist.hasChildNodes())
	{
		this._hyperlinklist.removeChild(this._hyperlinklist.firstChild);
	}

	// get the selected menuitem
	var selectedmenuitemname = this._menuitemnames[this._menuitemcombobox.selectedIndex];
	if (selectedmenuitemname == null) return;

	var selectedmenuitem = this.MenuItems[selectedmenuitemname];
	if (selectedmenuitem == null) return;
	
	// get the selected submenuitem
	var selectedsubmenuitemname = selectedmenuitem._submenuitemnames[this._submenuitemcombobox.selectedIndex];
	if (selectedsubmenuitemname == null) return;

	var selectedsubmenuitem = selectedmenuitem.SubMenuItems[selectedsubmenuitemname];
	if (selectedsubmenuitem == null) return;
	
	// Run through hyperlink items	
	for (var hyperlinkindex = 0; hyperlinkindex < selectedsubmenuitem._hyperlinknames.length; hyperlinkindex++)
	{
		var hyperlinkname = selectedsubmenuitem._hyperlinknames[hyperlinkindex];
		var hyperlink = selectedsubmenuitem.Hyperlinks[hyperlinkname];
		
		var hyperlinklistelement = document.createElement("li");
		this._hyperlinklist.appendChild(hyperlinklistelement);
		
		// add the hyperlinkelement and set its value and text
		var hyperlinkelement = document.createElement("a");
		
		if (hyperlinkindex == selectedsubmenuitem._hyperlinkindex)
		{		
			hyperlinkelement.className = "ActiveHyperlink";
		}
		else
		{
			hyperlinkelement.className = "Hyperlink";
		}

		hyperlinklistelement.appendChild(hyperlinkelement);
		hyperlinkelement.setAttribute("href", hyperlink.URL);
		hyperlinkelement.setAttribute("target", "_self");
		hyperlinkelement.appendChild(document.createTextNode(RemoveTail(hyperlink.Name)));
	}
}
ComboBoxSelectionMenu.prototype.Select = function(selectionname)
{
	// Run through menu items	
	for (var menuitemindex = 0; menuitemindex < this._menuitemnames.length; menuitemindex++)
	{
		var menuitemname = this._menuitemnames[menuitemindex];
		var menuitem = this.MenuItems[menuitemname];
		
		if (menuitemname == selectionname)
		{
			menuitem.Select();
			return;
		}

		// Run through sub menu items	
		for (var submenuitemindex = 0; submenuitemindex < menuitem._submenuitemnames.length; submenuitemindex++)
		{
			var submenuitemname = menuitem._submenuitemnames[submenuitemindex];
			var submenuitem = menuitem.SubMenuItems[submenuitemname];

			if (submenuitemname == selectionname)
			{
				submenuitem.Select();
				return;
			}

			// Run through hyperlink items	
			for (var hyperlinkindex = 0; hyperlinkindex < submenuitem._hyperlinknames.length; hyperlinkindex++)
			{
				var hyperlinkname = submenuitem._hyperlinknames[hyperlinkindex];
				var hyperlink = submenuitem.Hyperlinks[hyperlinkname];

				if (hyperlinkname == selectionname)
				{
					hyperlink.Select();
					return;
				}
			}
		}
	}
}


function MenuItem(name, parentcomboboxselectionmenu)
{
	// private members
	this._parentcomboboxselectionmenu = parentcomboboxselectionmenu;
    this._submenuitemnames = new Array();
    this._submenuitemindex = -1;

	// public properties
    this.Name = name;
    this.SubMenuItems = new Array();
}
MenuItem.prototype.AddSubMenuItem = function(name)
{
    this._submenuitemnames[this._submenuitemnames.length] = name;
    this.SubMenuItems[name] = new SubMenuItem(name, this);
}
MenuItem.prototype.Select = function()
{
	this._parentcomboboxselectionmenu._menuitemindex = this._parentcomboboxselectionmenu._menuitemnames.indexOf(this.Name);
}

function SubMenuItem(name, parentmenuitem)
{
	// private members
	this._parentmenuitem = parentmenuitem;   
	this._hyperlinknames = new Array();
	this._hyperlinkindex = -1;

	// public properties
    this.Name = name;
	this.Hyperlinks = new Array();
}
SubMenuItem.prototype.AddHyperlink = function (name, url)
{
	this._hyperlinknames[this._hyperlinknames.length] = name;
	this.Hyperlinks[name] = new Hyperlink(name, url, this);
}
SubMenuItem.prototype.Select = function()
{
	this._parentmenuitem._parentcomboboxselectionmenu._menuitemindex = this._parentmenuitem._parentcomboboxselectionmenu._menuitemnames.indexOf(this._parentmenuitem.Name);
	this._parentmenuitem._submenuitemindex = this._parentmenuitem._submenuitemnames.indexOf(this.Name);
}

// Hyperlink class definition
function Hyperlink(name, url, parentsubmenuitem)
{
	// private members
	this._parentsubmenuitem = parentsubmenuitem;

	// public properties
    this.Name = name;
    this.URL = url;
}
Hyperlink.prototype.Select = function()
{
	this._parentsubmenuitem._parentmenuitem._parentcomboboxselectionmenu._menuitemindex = this._parentsubmenuitem._parentmenuitem._parentcomboboxselectionmenu._menuitemnames.indexOf(this._parentsubmenuitem._parentmenuitem.Name);
	this._parentsubmenuitem._parentmenuitem._submenuitemindex = this._parentsubmenuitem._parentmenuitem._submenuitemnames.indexOf(this._parentsubmenuitem.Name);
	this._parentsubmenuitem._hyperlinkindex = this._parentsubmenuitem._hyperlinknames.indexOf(this.Name)
}

// Helper methods
if (!Array.indexOf)
{
	Array.prototype.indexOf = function(obj)
	{
		for (var i=0; i<this.length; i++)
		{
			if (this[i] == obj)
			{
				return i;
			}
		}

		return -1;
	}
}


function RemoveTail(text)
{
	var newtext = text;
	var tailindex = text.indexOf("_");
	if (tailindex > 0)
	{
		newtext = text.substr(0, tailindex);
	}
	
	return newtext;
}
