/*
  JScript library    - obj_tMenuBar.js
  Author	     - Brendon Matthews

History
  Jun 2004		Started.
  Aug 2006	      Changed to run in IFRAME to fix zIndex issues
			Added text colors, and stylesheet
  Sep 2006	      Added fade effect with transparency
			Allowed IFRAME to be generated automatically when needed
			Changed to run in DIV control instead of IFRAME to improve performance
*/

// ====================== tMenuBar Class ====================== //

function tMenuBarRootItem(mb) {
	// Fields
	this.m_menubar = mb;
	this.m_items = new Array();
	this.m_popup_index = -1;
	// Construction Methods
	this.hidePopups = tPopupMenuItem_hidePopups;
	this.mouseoverParent = tPopupMenuItem_mouseoverParent;
	this.mouseoutParent = tPopupMenuItem_mouseoutParent;
	this.findItem = tPopupMenuItem_findItem;
	this.addItem = tPopupMenuItem_addItem;
	this.getPopupHTML = tPopupMenuItem_getPopupHTML;
	// Internal fields
	this.m_width = null;
	this.m_height = null;
	this.m_borderType = null;
	this.m_borderColor = null;
	this.m_backgroundColor = null;
	this.m_backgroundColorOver = null;
	this.m_currentColor = null;
	this.m_currentColorOver = null;
	this.m_color = null;
	this.m_colorOver = null;
	this.m_align = null;
	this.m_style = null;
	this.m_maxHeight = 0;
	// Field Retrieval Methods
	this.getWidth = tMenuBarRootItem_getWidth;
	this.getHeight = tMenuBarRootItem_getHeight;
	this.getBorderType = tMenuBarRootItem_getBorderType;
	this.getBorderColor = tMenuBarRootItem_getBorderColor;
	this.getColor = tMenuBarRootItem_getColor;
	this.getColorOver = tMenuBarRootItem_getColorOver;
	this.getBackgroundColor = tMenuBarRootItem_getBackgroundColor;
	this.getBackgroundColorOver = tMenuBarRootItem_getBackgroundColorOver;
	this.getCurrentColor = tMenuBarRootItem_getCurrentColor;
	this.getCurrentColorOver = tMenuBarRootItem_getCurrentColorOver;
	this.getAlign = tMenuBarRootItem_getAlign;
	this.getStyle = tMenuBarRootItem_getStyle;
}

function tMenuBarRootItem_getWidth() {
	if (this.m_width != null) return this.m_width;
	else return this.m_menubar.m_width;
}

function tMenuBarRootItem_getHeight() {
	if (this.m_height != null) return this.m_height;
	else return this.m_menubar.m_height;
}

function tMenuBarRootItem_getBorderType() {
	if (this.m_borderType != null) return this.m_borderType;
	else return this.m_menubar.m_borderType;
}

function tMenuBarRootItem_getBorderColor() {
	if (this.m_borderColor != null && this.m_borderColor != '') return this.m_borderColor;
	else return this.m_menubar.m_borderColor;
}

function tMenuBarRootItem_getColor() {
	if (this.m_color != null && this.m_color != '') return this.m_color;
	else return this.m_menubar.m_color;
}

function tMenuBarRootItem_getColorOver() {
	if (this.m_colorOver != null && this.m_colorOver != '') return this.m_colorOver;
	else return this.m_menubar.m_colorOver;
}

function tMenuBarRootItem_getBackgroundColor() {
	if (this.m_backgroundColor != null && this.m_backgroundColor != '') return this.m_backgroundColor;
	else return this.m_menubar.m_backgroundColor;
}

function tMenuBarRootItem_getBackgroundColorOver() {
	if (this.m_backgroundColorOver != null && this.m_backgroundColorOver != '') return this.m_backgroundColorOver;
	else return this.m_menubar.m_backgroundColorOver;
}

function tMenuBarRootItem_getCurrentColor() {
	if (this.m_currentColor != null && this.m_currentColor != '') return this.m_currentColor;
	else return this.m_menubar.m_currentColor;
}

function tMenuBarRootItem_getCurrentColorOver() {
	if (this.m_currentColorOver != null && this.m_currentColorOver != '') return this.m_currentColorOver;
	else return this.m_menubar.m_currentColorOver;
}

function tMenuBarRootItem_getAlign() {
	if (this.m_align != null && this.m_align != '') return this.m_align;
	else return this.m_menubar.m_align;
}

function tMenuBarRootItem_getStyle() {
	if (this.m_style != null && this.m_style != '') return this.m_style;
	else return this.m_menubar.m_style;
}

// tMenuBar Constructor
function tMenuBar(handle,id,horizontal) {
	
	// Fields
	this.m_id = id;
	this.m_handle = handle;
	this.m_horizontal = horizontal;
	this.m_menus = new Array();
	this.m_frame = null;
	this.m_popups = new Array();
	
	// Styles
	this.m_width = 0;
	this.m_height = 0;
	this.m_borderType = BORDERTYPE_FULL;
	this.m_borderColor = null;
	this.m_backgroundColor = null;
	this.m_backgroundColorOver = null;
	this.m_currentColor = null;
	this.m_currentColorOver = null;
	this.m_color = null;
	this.m_colorOver = null;
	this.m_style = null;
	this.m_offsetX = null;
	this.m_offsetY = null;
	this.m_align = 'left';
	
	// Costructors
	this.newMenu = tMenuBar_newMenu;
	
	// Display Methods
	this.findItem = tMenuBar_findItem;
	this.openPopup = tMenuBar_openPopup;
	this.openPopup2 = tPopupMenu_openPopup2;
	this.closePopup = tPopupMenu_closePopup;
	this.clickItem = tMenuBar_clickItem;
	this.showMenu = tMenuBar_showMenu;
	this.hideMenu = tMenuBar_hideMenu;
	this.showMenu2 = tPopupMenu_showMenu2;
	this.hideMenu2 = tPopupMenu_hideMenu2;
	this.showMenu3 = tPopupMenu_showMenu3;
	this.hideMenu3 = tPopupMenu_hideMenu3;
	
}

function tMenuBar_newMenu(index) {
	var item = new tMenuBarRootItem(this);
	this.m_menus[index] = item;
	return item;
}

function tMenuBar_findItem(popupIndex) {
	var item;
	for (var i=0; i<this.m_menus.length; i++) {
		if (this.m_menus[i]) {
			if (this.m_menus[i].m_popup_index == popupIndex) return this.m_menus[i];
			item = this.m_menus[i].findItem(popupIndex);
			if (item) return item;
		}
	}
	return null;
}

function tMenuBar_clickItem(popupIndex,index2) {
	this.closePopup();
	var item = this.findItem(popupIndex);
	if (!item) throw "item not found";
	if (index2 < 0 || index2 >= item.m_items.length) {
		throw "item index out of bounds";
	}
	if (item.m_items[index2].m_onclick) eval(item.m_items[index2].m_onclick);
}

function tMenuBar_openPopup(index,element) {
	if (index < 0 || index >= this.m_menus.length) return;
	var item = this.m_menus[index];
	if (!item || item.m_items.length == 0) return;
	this.closePopup();
	// Create the popup window
	var framedoc = document;
	if (this.m_frame != null) {
		eval('this.m_frame.'+this.m_handle+' = '+this.m_handle);
		framedoc = this.m_frame.document;
		if (item.m_popup_index >= 0) {
			var x = item.m_popup_index;
			item.m_popup = findPopup(this.m_id+'POP'+x,framedoc);
		}
	}
	this.openPopup2(item,element,framedoc,this.m_horizontal);
}

function tMenuBar_showMenu(index,cell,backColor,textColor) {
	this.openPopup(index,cell);
	if (cell) {
		cell.className = 'hilite';
		if (backColor != null) {
			if (cell.style.backgroundColor) cell.m_defaultBackground = cell.style.backgroundColor;
			cell.style.backgroundColor = backColor;
		}
		if (textColor != null) {
			if (cell.style.color) cell.m_defaultText = cell.style.color;
			cell.style.color = textColor;
		}
	}
}

function tMenuBar_hideMenu(index,cell) {
	if (index >= 0 && index < this.m_menus.length && this.m_menus[index] && this.m_menus[index].m_popup) try {
		this.m_menus[index].m_popup.onmouseout();
	}
	catch (e) {}
	if (cell) {
		cell.className = '';
		if (cell.m_defaultBackground) cell.style.backgroundColor = cell.m_defaultBackground;
		if (cell.m_defaultText) cell.style.color = cell.m_defaultText;
	}
}
