/* fbkMenu
Copyright Sam Regan from Feb 6, 2006
Licensed to Snapsoft for Health By Design only
This script may not be duplicated or distributed in any form.
*/
function fbkMenu (_text, _href, _icon, _arrow, _target)
{
	// variables
	this.Name = 'menu';
	this.Theme = 'MenuTheme';
	this.Type = 'vert';
	this.Dir = 'left';
	this.Delay = 150;
	
	this.Text = '';
	this.Href = '';
	this.Icon = '';
	this.Arrow = '';
	this.Target = '_self';

	this.Root = true;
	this.Parent = null;
	this.Children = new Array();
	this.HtmlId = 'fbkMenu';
	this.Timer = null;
	
	// functions
	this.addChild = fbkMenuAddChild;
	this.draw = fbkMenuDraw;
	this.over = fbkMenuOver;
	this.out = fbkMenuOut;
	this.click = fbkMenuClick;
	
	this.getName = fbkMenuGetName;
	this.getTheme = fbkMenuGetTheme;
	this.getMenu = fbkMenuGetMenu;
	this.findMenu = fbkMenuFindMenu;
	this.hideMenusNot = fbkMenuHideMenusNot;
	this.hideAll = fbkMenuHideAll;
	this.startTimeout = fbkMenuStartTimeout;
	this.clearTimeout = fbkMenuClearTimeout;
	this.endTimeout = fbkMenuEndTimeout;
	
	this.getElementX = fbkMenuGetElementX;
	this.getElementY = fbkMenuGetElementY;
	this.getElementWidth = fbkMenuGetElementWidth;
	this.getElementHeight = fbkMenuGetElementHeight;
	
	// construct
	if (_text) {
		this.Text = _text;
	}
	
	if (_href) {
		this.Href = _href;
	}
	
	if (_icon) {
		this.Icon = _icon;
	}
	
	if (_arrow) {
		this.Arrow = _arrow;
	}
	
	if (_target) {
		this.Target = _target;
	}
}

function fbkMenuAddChild (_menu)
{
	var menu = _menu;
	menu.Root = false;
	menu.Parent = this;
	menu.Dir = this.Dir;
	
	this.Children[this.Children.length] = menu;
}

function fbkMenuDraw (_name, _theme, _depth)
{
	if (this.Root) {
		// set name and theme
		
		if (_name) {
			this.Name = _name;
			this.HtmlId = _name;
		}
		
		if (_theme) {
			this.Theme = _theme;
		}
	}
	
	var output = '';
	
	if (isNaN(_depth)) {
		_depth = 0;
	}
	
	// draw container start
	if (this.Root) {
		output = '<table border="0" cellspacing="0" cellpadding="0" class="' + this.getTheme() + 'Main" id="' + this.getName() + 'Main">' + "\n";

		if (this.Type == 'horiz') {
			output += '<tr class="Row">' + "\n";
		}
	} else {
		output = '<div class="' + this.getTheme() + 'Sub ' + this.getTheme() + 'Sub' + _depth + '" style="position: absolute; display: none;" id="' + this.HtmlId + 'Sub">' + "\n";
		output += '<table border="0" cellspacing="0" cellpadding="0" class="SubMenu">' + "\n";
	}
	
	// draw child link elements
	for (var i = 0; i < this.Children.length; i++) {
		var child = this.Children[i];
		child.HtmlId = this.HtmlId + '_' + i;
		
		if (!this.Root || this.Type == 'vert') {
			if (this.Root) {
				theClass = 'Row';
			} else {
				theClass = 'SubRow';
			}
			
			output += '<tr class="' + theClass + '" id = "' + child.HtmlId + 'Row">' + "\n";
		}
		
		var triggerCode = 'onMouseOver="' + this.getName() + '.over(this, \'' + child.HtmlId + '\');"';
		triggerCode += ' onMouseOut="' + this.getName() + '.out(this, \'' + child.HtmlId + '\');"';
		triggerCode += ' onClick="' + this.getName() + '.click(this, \'' + child.HtmlId + '\');"';
		
		if (child.Icon) {
			icon = child.Icon;
		} else {
			icon = '';
		}
		
		output += '<td class="Icon" id="' + child.HtmlId + 'Icon" ' + triggerCode + '>' + icon + '</td>';
		output += '<td class="Text" id="' + child.HtmlId + 'Text" ' + triggerCode + '>' + child.Text + '</td>';
		
		if (child.Arrow && child.Children.length > 0) {
			arrow = child.Arrow;
		} else {
			arrow = '';
		}
		
		output += '<td class="Arrow" id="' + child.HtmlId + 'Arrow" ' + triggerCode + '>' + arrow + '</td>' + "\n";
		
		if (!this.Root || this.Type == 'vert') {
			output += '</tr>' + "\n";
		}
	}
	
	// draw container end
	if (this.Root) {
		if (this.Type == 'horiz') {
			output += '</tr>' + "\n";
		}
		
		output += '</table>' + "\n";
	} else {
		output += '</table>' + "\n";
		output += '</div>' + "\n";
	}
	
	// draw child menus
	for (var i = 0; i < this.Children.length; i++) {
		var child = this.Children[i];
		output += child.draw(null, null, _depth + 1);
	}
	
	if (this.Root) {
		//alert(output);
		document.write(output);
		//document.write('<xmp>' + output + '</xmp>');
	} else {
		return output;
	}
}

function fbkMenuGetName ()
{
	if (this.Root) {
		return this.Name;
	} else {
		return this.Parent.getName();
	}
}

function fbkMenuGetTheme ()
{
	if (this.Root) {
		return this.Theme;
	} else {
		return this.Parent.getTheme();
	}
}

function fbkMenuOver (_element, _html_id)
{
	this.clearTimeout();
	_element.style.cursor = 'pointer';
	
	var menu = this.getMenu(_html_id);
	menu.Parent.hideMenusNot(_html_id);
	
	// highlight current item
	var iconEl = document.getElementById(_html_id + 'Icon');
	var textEl = document.getElementById(_html_id + 'Text');
	var arrowEl = document.getElementById(_html_id + 'Arrow');
	
	if (iconEl) {
		iconEl.className = 'IconOver';
	}
	
	if (textEl) {
		textEl.className = 'TextOver';
	}
	
	if (arrowEl) {
		arrowEl.className = 'ArrowOver';
	}
	
	// show sub menu
	var el = document.getElementById(_html_id + 'Sub');
	
	if (el) {		
		var x = 0;
		var y = 0;
		
		if (this.Type == 'vert') {
			var row = document.getElementById(_html_id + 'Row');
			
			if (this.Dir == 'right') {
				x = this.getElementX(row) + this.getElementWidth(row);
			} else {
				x = this.getElementX(row);
			}
			
			y = this.getElementY(row);
		} else {
			if (menu.Parent.Root) {
				var table = document.getElementById(this.getName() + 'Main');
				var icon = document.getElementById(_html_id + 'Icon');
				
				x = this.getElementX(icon);
				y = this.getElementY(table) + this.getElementHeight(table);
			} else {
				var row = document.getElementById(_html_id + 'Row');
			
				x = this.getElementX(row) + this.getElementWidth(row);
				y = this.getElementY(row);
			}
		}
		 
		el.style.left = x;
		el.style.top = y;
		el.style.display = 'inline';
		
		if (this.Type == 'vert' && this.Dir == 'left') {
			el.style.left = x - this.getElementWidth(el);
		}
	}
}

function fbkMenuOut (_element, _html_id)
{
	// clear timeout
	this.startTimeout();
	
	// unhighlight current item
	// highlight current item
	var iconEl = document.getElementById(_html_id + 'Icon');
	var textEl = document.getElementById(_html_id + 'Text');
	var arrowEl = document.getElementById(_html_id + 'Arrow');
	
	if (iconEl) {
		iconEl.className = 'Icon';
	}
	
	if (textEl) {
		textEl.className = 'Text';
	}
	
	if (arrowEl) {
		arrowEl.className = 'Arrow';
	}
}

function fbkMenuClick (_element, _html_id)
{
	var menu = this.getMenu(_html_id);
	
	if (menu.Href) {
		window.open (menu.Href, menu.Target);
	}
}

function fbkMenuGetMenu (_html_id)
{
	if (this.Root) {
		return this.findMenu(_html_id);
	} else {
		return this.Parent.getMenu(_html_id);
	}
}

function fbkMenuFindMenu (_html_id)
{
	for (var i = 0; i < this.Children.length; i++) {
		if (this.Children[i].HtmlId == _html_id) {
			return this.Children[i];
		} else {
			test = this.Children[i].findMenu(_html_id);
			
			if (test) {
				return test;
			}
		}
	}
	
	return null;
}

function fbkMenuHideMenusNot (_html_id)
{
	for (var i = 0; i < this.Children.length; i++) {
		if (this.Children[i].HtmlId != _html_id) {
			var el = document.getElementById(this.Children[i].HtmlId + 'Sub');
			
			if (el) {
				el.style.display = 'none';
			}
		}
		
		this.Children[i].hideMenusNot(_html_id);
	}
}

function fbkMenuHideAll ()
{
	for (var i = 0; i < this.Children.length; i++) {
		var el = document.getElementById(this.Children[i].HtmlId + 'Sub');
			
		if (el) {
			el.style.display = 'none';
		}
		
		this.Children[i].hideAll();
	}
}

function fbkMenuStartTimeout ()
{
	if (this.Root) {
		this.Timer = window.setTimeout(this.getName() + '.endTimeout()', this.Delay);
	} else {
		this.Parent.startTimeOut();
	}
}

function fbkMenuClearTimeout ()
{
	if (this.Root) {
		window.clearTimeout(this.Timer);
	} else {
		this.Parent.clearTimeout();
	}
}

function fbkMenuEndTimeout ()
{
	if (this.Root) {
		this.hideAll();
	} else {
		this.Parent.endTimeout();
	}
}

function fbkMenuGetElementX (el)
{
	var x = 0;

	do {
		x += el.offsetLeft;
		el = el.offsetParent;
	} while (el);
	
	return x;
}

function fbkMenuGetElementY (el)
{
	var y = 0;

	do {
		y += el.offsetTop;
		el = el.offsetParent;
	} while (el);
	
	return y;
}

function fbkMenuGetElementWidth (_el)
{
	var width = _el.offsetWidth;
	
	if (width > 0 || !_el.tagName || _el.tagName.toLowerCase() != 'tr')
		return width;
		
	if (!_el.firstChild)
		return 0;
	
	return _el.lastChild.offsetLeft - _el.firstChild.offsetLeft + this.getElementWidth(_el.lastChild);
}

function fbkMenuGetElementHeight (_el)
{
	var height = _el.offsetHeight;
	
	if (height > 0 || _el.tagName.toLowerCase() != 'tr')
		return height;
		
	if (!_el.firstChild)
		return 0;
		
	return _el.firstChild.offsetHeight;
}