/*
  JScript library    - obj_tPopupMenu.js
  Author             - Brendon Matthews

History
  Jun 2004		BM				Started.
*/

BORDERTYPE_NONE = 0;
BORDERTYPE_OUTER = 1;
BORDERTYPE_FULL = 2;

ITEMTYPE_TEXT = 0;
ITEMTYPE_SEPARATOR = 1;
ITEMTYPE_IMAGE = 2;
ITEMTYPE_BUTTON = 3;

// ====================== tPopupMenuItem Class ====================== //

function tPopupMenuItem(parent,type,a1,a2,a3,a4) {
	// Fields
	this.m_parent = parent;
	this.m_type = type;
	if (this.m_type == ITEMTYPE_TEXT) {
		this.m_text = a1;
		this.m_onclick = a2;
		this.m_current = a3;
	}
	else if (this.m_type == ITEMTYPE_IMAGE) {
		this.m_src = a1;
		this.m_text = a2;
	}
	else if (this.m_type == ITEMTYPE_BUTTON) {
		this.m_src = a1;
		this.m_rollover_src = a2;
		this.m_text = a3;
		this.m_onclick = a4;
	}
	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 = tPopupMenuItem_getWidth;
	this.getHeight = tPopupMenuItem_getHeight;
	this.getBorderType = tPopupMenuItem_getBorderType;
	this.getBorderColor = tPopupMenuItem_getBorderColor;
	this.getColor = tPopupMenuItem_getColor;
	this.getColorOver = tPopupMenuItem_getColorOver;
	this.getBackgroundColor = tPopupMenuItem_getBackgroundColor;
	this.getBackgroundColorOver = tPopupMenuItem_getBackgroundColorOver;
	this.getCurrentColor = tPopupMenuItem_getCurrentColor;
	this.getCurrentColorOver = tPopupMenuItem_getCurrentColorOver;
	this.getAlign = tPopupMenuItem_getAlign;
	this.getStyle = tPopupMenuItem_getStyle;
}

function tPopupMenuItem_getWidth() {
	if (this.m_width != null) return this.m_width;
	else return this.m_parent.getWidth();
}

function tPopupMenuItem_getHeight() {
	if (this.m_height != null) return this.m_height;
	else return this.m_parent.getHeight();
}

function tPopupMenuItem_getBorderType() {
	if (this.m_borderType != null) return this.m_borderType;
	else return this.m_parent.getBorderType();
}

function tPopupMenuItem_getBorderColor() {
	if (this.m_borderColor != null && this.m_borderColor != '') return this.m_borderColor;
	else return this.m_parent.getBorderColor();
}

function tPopupMenuItem_getColor() {
	if (this.m_color != null && this.m_color != '') return this.m_color;
	else return this.m_parent.getColor();
}

function tPopupMenuItem_getColorOver() {
	if (this.m_colorOver != null && this.m_colorOver != '') return this.m_colorOver;
	else return this.m_parent.getColorOver();
}

function tPopupMenuItem_getBackgroundColor() {
	if (this.m_backgroundColor != null && this.m_backgroundColor != '') return this.m_backgroundColor;
	else return this.m_parent.getBackgroundColor();
}

function tPopupMenuItem_getBackgroundColorOver() {
	if (this.m_backgroundColorOver != null && this.m_backgroundColorOver != '') return this.m_backgroundColorOver;
	else return this.m_parent.getBackgroundColorOver();
}

function tPopupMenuItem_getCurrentColor() {
	if (this.m_currentColor != null && this.m_currentColor != '') return this.m_currentColor;
	else return this.m_parent.getCurrentColor();
}

function tPopupMenuItem_getCurrentColorOver() {
	if (this.m_currentColorOver != null && this.m_currentColorOver != '') return this.m_currentColorOver;
	else return this.m_parent.getCurrentColorOver();
}

function tPopupMenuItem_getAlign() {
	if (this.m_align != null && this.m_align != '') return this.m_align;
	else return this.m_parent.getAlign();
}

function tPopupMenuItem_getStyle() {
	if (this.m_style != null && this.m_style != '') return this.m_style;
	else return this.m_parent.getStyle();
}

function tPopupMenuItem_hidePopups() {
	for (var i=0; i<this.m_items.length; i++) {
		if (this.m_items[i].m_popup) this.m_items[i].m_popup.hide();
	}
}

function tPopupMenuItem_mouseoverParent() {
	if (this.m_parent && this.m_parent.m_popup) {
		this.m_parent.m_popup.onmouseover();
		for (var i=0; i<this.m_parent.m_items.length; i++) {
			if (this.m_parent.m_items[i] != this && this.m_parent.m_items[i].m_popup) {
				this.m_parent.m_items[i].m_popup.hide();
			}
		}
	}
}

function tPopupMenuItem_mouseoutParent() {
	if (this.m_parent && this.m_parent.m_popup) {
		this.m_parent.m_popup.onmouseout();
	}
}

function tPopupMenuItem_findItem(popupIndex) {
	var item;
	for (var i=0; i<this.m_items.length; i++) {
		if (this.m_items[i].m_popup_index == popupIndex) return this.m_items[i];
		item = this.m_items[i].findItem(popupIndex);
		if (item) return item;
	}
	return null;
}

function tPopupMenuItem_addItem(type,a1,a2,a3,a4) {
	var item = new tPopupMenuItem(this,type,a1,a2,a3,a4);
	this.m_items[this.m_items.length] = item;
	return item;
}

function tPopupMenuItem_getPopupHTML(handle) {
	// Load the parameters
	var width = this.getWidth();
	var height = this.getHeight();
	var borderType = this.getBorderType();
	var borderColor = this.getBorderColor();
	var color = this.getColor();
	var colorOver = this.getColorOver();
	var backgroundColor = this.getBackgroundColor();
	var backgroundColorOver = this.getBackgroundColorOver();
	var currentColor = this.getCurrentColor();
	var currentColorOver = this.getCurrentColorOver();
	var align = this.getAlign();
	var style = this.getStyle();
	// Generate styles
	var tableBorder,cellBorder;
	if (borderType == BORDERTYPE_OUTER) {
		if (borderColor == null) borderColor = 'black';
		tableBorder = 'border:1px solid '+borderColor+';';
		cellBorder = ';border:none';
	}
	else if (borderType == BORDERTYPE_FULL) {
		if (borderColor == null) borderColor = 'black';
		tableBorder = 'border-left:1px solid '+borderColor+';border-top:1px solid '+borderColor+';border-bottom:none;border-right:none;';
		cellBorder = ';border-left:none;border-top:none;border-bottom:1px solid '+borderColor+';border-right:1px solid '+borderColor;
	}
	else {
		tableBorder = 'border:none;';
		cellBorder = ';border:none';
	}
	style = tableBorder+style;
	// Generate html
	var html = '<table cellpadding=2 cellspacing=0 border=0';
	if (width > 0) html += ' width="'+width+'"';
	if (style != '') html += ' style="'+style+'"';
	html += '>\n';
	var mouseoverParams;
	for (i=0; i<this.m_items.length; i++) {
		html += '  <tr valign=middle';
		if (height > 0) html += ' height="'+height+'"';
		html += '>\n';
		if (this.m_items[i].m_type == ITEMTYPE_SEPARATOR) {
			html += '    <td align="center" style="cursor:pointer';
			html += cellBorder;
			if (backgroundColor != null) html += ';background-color:'+backgroundColor;
			html += '"><hr noshade size=\"1\"';
			if (color != null) html += ' style="color:'+color+'"';
			else html += ' style="color:black"';
			html += ' width="90%"></td>\n';
		}
		else if (this.m_items[i].m_type == ITEMTYPE_IMAGE) {
			html += '    <td align="center" style="cursor:pointer;padding:0px';
			html += cellBorder;
			if (backgroundColor != null) html += ';background-color:'+backgroundColor;
			html += '"><img src="'+this.m_items[i].m_src+'" border=0';
			if (this.m_items[i].m_text != null) html += ' alt="'+this.m_items[i].m_text+'"';
			html += '></td>\n';
		}
		else if (this.m_items[i].m_type == ITEMTYPE_BUTTON) {
			html += '    <td align="center" style="cursor:pointer;padding:0px';
			html += cellBorder;
			if (backgroundColor != null) html += ';background-color:'+backgroundColor;
			html += '"><img src="'+this.m_items[i].m_src+'" border=0';
			html += ' onclick="'+handle+'.clickItem('+this.m_popup_index+','+i+')"';
			if (this.m_items[i].m_text != null) html += ' alt="'+this.m_items[i].m_text+'"';
			
			html += ' onmouseover="'+handle+'.showMenu3('+this.m_popup_index+','+i+',this';
			if (this.m_items[i].m_rollover_src != null) html += ',\''+this.m_items[i].m_rollover_src+'\'';
			html += ')" onmouseout="'+handle+'.hideMenu3('+this.m_popup_index+','+i+',this)"';
			html += '></td>\n';
		}
		else {
			html += '    <td onclick="'+handle+'.clickItem('+this.m_popup_index+','+i+')"';
			if (align != null) html += ' align="'+align+'"';
			if (!(width > 0)) html += ' nowrap';
			html += ' style="cursor:pointer';
			html += cellBorder;
			if (color != null) html += ';color:'+color;
			if (this.m_items[i].m_current && currentColor != null) html += ';background-color:'+currentColor;
			else if (backgroundColor != null) html += ';background-color:'+backgroundColor;
			html += '"';
			mouseoverParams = '';
			if (this.m_items[i].m_current && currentColorOver != null) {
				mouseoverParams = ',\''+currentColorOver+'\'';
			}
			else if (backgroundColorOver != null) {
				mouseoverParams = ',\''+backgroundColorOver+'\'';
			}
			if (colorOver != null) {
				if (mouseoverParams == '') mouseoverParams = ',null';
				mouseoverParams += ',\''+colorOver+'\'';
			}
			html += ' onmouseover="'+handle+'.showMenu2('+this.m_popup_index+','+i+',this'+mouseoverParams+')"';
			html += ' onmouseout="'+handle+'.hideMenu2('+this.m_popup_index+','+i+',this)"';
			html += '>'+this.m_items[i].m_text+'</td>\n';
		}
		html += '  </tr>\n';
	}
	html += '</table>\n';
	return html;
}

// ====================== tPopupMenu Class ====================== //

// tPopupMenu Constructor
function tPopupMenu(handle,id) {
	
	// Fields
	this.m_id = id;
	this.m_handle = handle;
	this.m_items = new Array();
	this.m_popups = new Array();
	
	// Styles
	this.m_borderType = BORDERTYPE_OUTER;
	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_align = 'left';
	this.m_class = null;
	this.m_cssfile = null;
	
	// Field Retrieval Methods
	this.getWidth = tPopupMenu_getWidth;
	this.getHeight = tPopupMenu_getHeight;
	this.getBorderType = tPopupMenu_getBorderType;
	this.getBorderColor = tPopupMenu_getBorderColor;
	this.getColor = tPopupMenu_getColor;
	this.getColorOver = tPopupMenu_getColorOver;
	this.getBackgroundColor = tPopupMenu_getBackgroundColor;
	this.getBackgroundColorOver = tPopupMenu_getBackgroundColorOver;
	this.getCurrentColor = tPopupMenu_getCurrentColor;
	this.getCurrentColorOver = tPopupMenu_getCurrentColorOver;
	this.getAlign = tPopupMenu_getAlign;
	this.getStyle = tPopupMenu_getStyle;
	
	// Construction Methods
	this.hidePopups = tPopupMenuItem_hidePopups;
	this.mouseoverParent = tPopupMenuItem_mouseoverParent;
	this.mouseoutParent = tPopupMenuItem_mouseoutParent;
	this.findItem = tPopupMenu_findItem;
	this.addItem = tPopupMenuItem_addItem;
	this.getPopupHTML = tPopupMenuItem_getPopupHTML;
	
        // Display Methods
        this.clickItem = tPopupMenu_clickItem;
        this.closePopup = tPopupMenu_closePopup;
        this.openPopup = tPopupMenu_openPopup;
        this.openPopup2 = tPopupMenu_openPopup2;
        this.showMenu2 = tPopupMenu_showMenu2;
        this.hideMenu2 = tPopupMenu_hideMenu2;
        this.showMenu3 = tPopupMenu_showMenu3;
        this.hideMenu3 = tPopupMenu_hideMenu3;
        
        // Internal methods
        this.getBodyHTML = tPopupMenuItem_getPopupHTML;
}

function tPopupMenu_getWidth() {
	return this.m_width;
}

function tPopupMenu_getHeight() {
	return this.m_height;
}

function tPopupMenu_getBorderType() {
	return this.m_borderType;
}

function tPopupMenu_getBorderColor() {
	return this.m_borderColor;
}

function tPopupMenu_getColor() {
	return this.m_color;
}

function tPopupMenu_getColorOver() {
	return this.m_colorOver;
}

function tPopupMenu_getBackgroundColor() {
	return this.m_backgroundColor;
}

function tPopupMenu_getBackgroundColorOver() {
	return this.m_backgroundColorOver;
}

function tPopupMenu_getCurrentColor() {
	return this.m_currentColor;
}

function tPopupMenu_getCurrentColorOver() {
	return this.m_currentColorOver;
}

function tPopupMenu_getAlign() {
	return this.m_align;
}

function tPopupMenu_getStyle() {
	return this.m_style;
}

function tPopupMenu_findItem(popupIndex) {
	if (this.m_popup_index == popupIndex) return this;
	var item;
	for (var i=0; i<this.m_items.length; i++) {
		if (this.m_items[i].m_popup_index == popupIndex) return this.m_items[i];
		item = this.m_items[i].findItem(popupIndex);
		if (item) return item;
	}
	return null;
}

function tPopupMenu_clickItem(popupIndex) {
	this.closePopup();
	var item = this.findItem(popupIndex);
	if (!item) throw "item not found";
	if (index < 0 || index >= 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 tPopupMenu_closePopup() {
	for (i=0; i<this.m_popups.length; i++) try {
		this.m_popups[i].hide();
	}
	catch (e) {}
}

function tPopupMenu_openPopup(element) {
	this.closePopup();
	this.openPopup2(this,element);
}

function tPopupMenu_openPopup2(item,element,framedoc,horizontal) {
	if (!item || item.m_items.length == 0) return;
	var popup;
	if (item.m_popup) {
		popup = item.m_popup;
	}
	else {
		var x;
		if (item.m_popup_index >= 0) {
			x = item.m_popup_index;
		}
		else {
			x = this.m_popups.length;
			item.m_popup_index = x;
		}
		popup = createPopup(this.m_id+'POP'+x,framedoc);
		popup.m_item = item;
		popup.className = 'popupmenu';
		popup.hide = tPopupMenuPopup_hide;
		popup.onmouseout = tPopupMenuPopup_mouseout;
		popup.onmouseover = tPopupMenuPopup_mouseover;
		item.m_popup = popup;
		this.m_popups[x] = popup;
	}
	// Update the popup
	if (item.m_maxHeight > 0) popup.maxHeight = item.m_maxHeight;
	if (item.m_width > 0) popup.width = item.m_width;
	else if (this.m_width > 0) popup.width = this.m_width;
	popup.innerHTML = item.getPopupHTML(this.m_handle);
	var offsetX = 0;
	var offsetY = 0;
	if (this.m_offsetX != null) offsetX += this.m_offsetX;
	if (this.m_offsetY != null) offsetY += this.m_offsetY;
	if (element) {
		var coords = getElementOffsets(element);
		if (this.m_frame != null) {
			if (horizontal) coords.y = 0;
			else coords.x = 0;
		}
		else {
			if (horizontal) coords.y += element.offsetHeight;
			else coords.x += element.offsetWidth;
		}
		offsetX += coords.x;
		offsetY += coords.y;
	}
	if (item.m_offsetX) offsetX = item.m_offsetX;
	if (item.m_offsetY) offsetY = item.m_offsetY;
	if (this.m_frame != null) {
		if (this.m_frame.document.body.scrollLeft) offsetX += this.m_frame.document.body.scrollLeft;
		if (this.m_frame.document.body.scrollTop) offsetY += this.m_frame.document.body.scrollTop;
	}
	popup.show(offsetX,offsetY);
}

function tPopupMenu_showMenu2(popupIndex,index2,element,backColor,textColor) {
	var item = this.findItem(popupIndex);
	if (!item) throw "item not found";
	element.className = 'hilite';
	if (backColor != null) {
		if (element.style.backgroundColor) element.m_defaultBackground = element.style.backgroundColor;
		element.style.backgroundColor = backColor;
	}
	if (textColor != null) {
		if (element.style.color) element.m_defaultText = element.style.color;
		element.style.color = textColor;
	}
	if (index2 < 0 || index2 >= item.m_items.length) {
		throw "item index out of bounds";
	}
	this.openPopup2(item.m_items[index2],element);
	if (index2 >= 0 && index2 < item.m_items.length && item.m_items[index2].m_popup) try {
		item.m_items[index2].m_popup.onmouseover();
	}
	catch (e) {}
}

function tPopupMenu_hideMenu2(popupIndex,index2,element,background) {
	var item = this.findItem(popupIndex);
	if (!item) throw "item not found";
	element.className = '';
	if (element.m_defaultBackground) element.style.backgroundColor = element.m_defaultBackground;
	if (element.m_defaultText) element.style.color = element.m_defaultText;
	if (index2 >= 0 && index2 < item.m_items.length && item.m_items[index2].m_popup) try {
		item.m_items[index2].m_popup.onmouseout();
	}
	catch (e) {}
}


function tPopupMenu_showMenu3(popupIndex,index2,element,rollover) {
	var item = this.findItem(popupIndex);
	if (!item) throw "item not found";
	if (rollover != null) {
		if (element.src) element.m_oldsrc = element.src;
		element.src = rollover;
	}
	if (index2 < 0 || index2 >= item.m_items.length) {
		throw "item index out of bounds";
	}
	this.openPopup2(item.m_items[index2],element);
	if (index2 >= 0 && index2 < item.m_items.length && item.m_items[index2].m_popup) try {
		item.m_items[index2].m_popup.onmouseover();
	}
	catch (e) {}
}

function tPopupMenu_hideMenu3(popupIndex,index2,element,background) {
	var item = this.findItem(popupIndex);
	if (!item) throw "item not found";
	if (element.m_oldsrc) element.src = element.m_oldsrc;
	if (index2 >= 0 && index2 < item.m_items.length && item.m_items[index2].m_popup) try {
		item.m_items[index2].m_popup.onmouseout();
	}
	catch (e) {}
}

function tPopupMenuPopup_hide() {
	if (this.m_interval) window.clearTimeout(this.m_interval);
	tPopup_setVisibility('hidden',this,this.m_shim);
	this.m_item.hidePopups();
}

function tPopupMenuPopup_mouseout() {
	if (this.m_interval) window.clearTimeout(this.m_interval);
	eval('document.POPUP_'+this.id+' = this');
	this.m_interval = window.setTimeout('document.POPUP_'+this.id+'.hide()',500);
	this.m_item.mouseoutParent();
}

function tPopupMenuPopup_mouseover() {
	if (this.m_interval) window.clearTimeout(this.m_interval);
	this.m_item.mouseoverParent();
}
