var BETMENU_trans_pixel_src = "/images/layout/pixel_clear.gif";
var BETMENU_borderColor_pixel_src = "/images/layout/pixel_000000.gif";
var BETMENU_handleSpacer_pixel_src = "/images/layout/pixel_clear.gif";
var BETMENU_caret_src = "/images/layout/sub_ind_12x12.gif";
var NS4 = document.layers;
var NS6 = (document.getElementById && !document.all);
var IE4 = (navigator.userAgent.toLowerCase().indexOf("msie 4")!=-1);
var IE5 = (navigator.userAgent.toLowerCase().indexOf("msie 5")!=-1);
var IE6 = (navigator.userAgent.toLowerCase().indexOf("msie 6")!=-1);
var IE4UP = (IE4 || IE5 || IE6);

var MAC = (navigator.userAgent.toLowerCase().indexOf("mac")!=-1);
var MAC_IE4 = (IE4 && MAC);

var DYNAMIC = false;
var DOM1 = false;

if(NS4 || IE4UP || NS6) 
{
	DYNAMIC = true;
	if(NS6 || IE5 || IE6) DOM1 = true;
}
var HtmlContainerUniqueID = 1;
function HtmlContainer(markupString, width, height, left, top, parent)
{
	id = "sHC" + HtmlContainerUniqueID++;

	left = (left ? left : 0);
	top = (top ? top : 0);

	if(NS4)
	{
		markupString += '<div style="position:absolute; visibility:hidden; top:0px; left:0px;"><img width="'+width+'" height="'+height+'" border="0"></div>';
		if(!parent)newDomObj = new Layer(width);
		else newDomObj = new Layer(width, parent);
//		newDomObj.id = id;		why doesn't this work ?? dammit all
		newDomObj.visibility = "hidden";
		newDomObj.top = top;
		newDomObj.left = left;
		newDomObj.clip.bottom = height;
		newDomObj.document.open();
		newDomObj.document.write(markupString);
		newDomObj.document.close();
		newDomObj.height = height;
		newDomObj.markupString = markupString;
	}
	if(IE4)
	{
		if(!parent) parent = document.body;
		openTag = '<div id="'+id+'" style="top: '+top+'px; left: '+left+'px; width:'+width+'px; height:'+height+'px; position:absolute; visibility:hidden; overflow:hidden; clip:rect(0,'+width+','+height+',0);">';
		closeTag = '</div>';
		parent.insertAdjacentHTML("BeforeEnd",openTag + (markupString) + closeTag);
		newDomObj = document.all[this.id];
	}
	if(DOM1)
	{
		if(!parent) parent = document.getElementsByTagName("body")[0];
		
		newDomObj = document.createElement("div");
		newDomObj.setAttribute("id",id);
		newDomObj.style.position = "absolute";
		newDomObj.style.width = width + "px";
		newDomObj.style.height = height + "px";
		newDomObj.style.top = top + "px";
		newDomObj.style.left = left + "px";
		newDomObj.style.visibility = "hidden";
		newDomObj.style.overflow = "hidden";
		newDomObj.style.clip = "rect(0,"+width+","+height+",0)";
		
		newDomObj.innerHTML = markupString;
		parent.appendChild(newDomObj);
	}

	newDomObj.childContainers = new Array();
	
	newDomObj.show = HtmlContainer_show;
	newDomObj.hide = HtmlContainer_hide;
	newDomObj.isVisible = HtmlContainer_isVisible;	
	newDomObj.setBgColor = HtmlContainer_setBgColor;
	
	return newDomObj;
}

function HtmlContainer_show()
{
	if(NS4) this.visibility = 'inherit';
	else	this.style.visibility = 'inherit';
}

function HtmlContainer_hide()
{
	if(NS4) this.visibility = 'hidden';
	else	this.style.visibility = 'hidden';
}

function HtmlContainer_isVisible()
{
	if(NS4) return (this.visibility == 'inherit');
	else	return (this.style.visibility == 'inherit');
}

function HtmlContainer_setBgColor(hexStr)
{
	if(NS4) this.bgColor = hexStr;
	else this.style.backgroundColor = hexStr;
}


var addSpacer = /(<br>)/gi	;

function countLines(inputStr)
{
	arr = inputStr.match(addSpacer);
	if(arr)
	{
		return(arr.length + 1);
	}
	else return 1;
}


var BETMENUS = [];
var ACTIVE_MENU = null;
var DROP_TIMER = null;
var DEFAULT_TIMER = null;

function startMenu(menu)
{
	if(menu && menu.activate) menu.activate();
}

function eventInActiveMenu(menu,x,y)
{
	if(menu)
	{
		minX = menu.x + menu.mw;
		maxX = menu.x + menu.w - menu.mw;
		minY = menu.y + menu.handle_h + menu.handle_vSpace + menu.mh;
		maxY = menu.y + menu.masterHeight - menu.mh;
		
		if(x>minX && x<maxX && y>minY && y<maxY) return true;
	}
	return false;
}

function START_BETMENUS()
{
	for(var i=0; i<BETMENUS.length; i++)
	{
		BETMENUS[i].init();
	}

	if(NS4) document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT | Event.MOUSEDOWN);
	if(!NS6) document.onmouseover = BET_mouseover;
	if(!NS6) document.onmouseout = BET_mouseout;
	document.onmousedown = BET_mousedown;
}

function IE_findParentByProperty(thisObj,strSearchProperty)
{
		if(!thisObj.parentElement) return false;
		else 
		{
			if (thisObj.parentElement[strSearchProperty]) return thisObj.parentElement;
			else return (IE_findParentByProperty(thisObj.parentElement,strSearchProperty));
		}
}

function BET_mouseover(evt)
{
	targetObj = (IE4UP ? window.event.srcElement : evt.target);
	if(IE4UP && !targetObj.menu)
	{
		newTestObj = IE_findParentByProperty(targetObj,"menu");
		if(newTestObj)
		{
			window.event.returnValue = false;
//			window.event.cancelBubble = true;
			targetObj = newTestObj;
		}
	}

	if(targetObj.menu)
	{
//		if(IE4UP && MAC && ACTIVE_MENU && targetObj.menu.inline) return false;
		if(targetObj.menu.popup) clearDropTimer();
		if(targetObj.menu!=ACTIVE_MENU && targetObj.menu.popup) activateMenu(targetObj.menu);
		if(targetObj.isOffButton) setActiveItem(targetObj.menu, targetObj.hilite);
		return false;
	}

	else
	{
		if(IE4UP && ACTIVE_MENU && !DROP_TIMER) hideActiveItem(ACTIVE_MENU);		
		if(NS4) routeEvent(evt);
	}
	return true;
}

function BET_mouseout(evt)
{
	targetObj = (IE4UP ? window.event.srcElement : evt.target);
	if(IE4UP && targetObj.id.indexOf("betmenu")!=-1)
	{
		if(window.event.toElement && window.event.toElement.isOnButton) return false;
		targetObj = targetObj.parentElement;
	}

	if(targetObj.menu)
	{
		if(targetObj.menu.popup) clearDropTimer();

		if(targetObj.isBgPanel)
		{
			if (NS4 || (IE4UP && window.event.toElement && !window.event.toElement.menu)) startDropTimer(1000);
			return false;
		}

		if(targetObj.isOnButton)
		{
			if(IE4UP && window.event.toElement && window.event.toElement.id.indexOf("betmenu")!=-1) return false;
			hideActiveItem(targetObj.menu);
			return false;
		}
	}

	else
	{
		if(NS4) routeEvent(evt);
	}
	
	return true;
}

function BET_mousedown(evt)
{
	targetObj = (IE4UP ? window.event.srcElement : evt.target);
	
	if(IE4UP && targetObj.id.indexOf("betmenu")!=-1)
	{
		targetObj = targetObj.parentElement;
	}
	if(NS6 && ACTIVE_MENU)
	{
		//if(eventInActiveMenu(ACTIVE_MENU,evt.pageX,evt.pageY) && ACTIVE_MENU.activeItem)
		if(ACTIVE_MENU.activeItem)
		{
//			testObj(evt.target);
			targetObj = ACTIVE_MENU.activeItem;
		}
	}
	if(NS4 && ACTIVE_MENU)
	{
		if(eventInActiveMenu(ACTIVE_MENU,evt.pageX,evt.pageY) && ACTIVE_MENU.activeItem)
		{
//			testObj(evt.target);
			targetObj = ACTIVE_MENU.activeItem;
		}
	}
	if(targetObj.menu)
	{		
		if(targetObj.itemHref) window.location.href = targetObj.itemHref;
		else return false;
	}

	else
	{
		if(DROP_TIMER)clearDropTimer();
		if(ACTIVE_MENU)ACTIVE_MENU.drop();
		if(NS4) routeEvent(evt);
	}
	
	return true;
}

function testObj(obj)
{
	for(var p in obj)
	{
		alert(p+" "+obj[p]);
	}
}

function startMenu(menu)
{
	if(menu && menu.activate) menu.activate();
}

function BETMENU_POPUP_activate()
{
	clearDropTimer();
	if(this!=ACTIVE_MENU)
	{
		if(ACTIVE_MENU!=null) ACTIVE_MENU.drop();
		this.master.show();
		ACTIVE_MENU = this;
	}
}

function activateMenu(menu)
{
	if(ACTIVE_MENU!=null) ACTIVE_MENU.drop();
	if(menu.popup)menu.master.show();
	ACTIVE_MENU = menu;
}

function BETMENU_POPUP_drop()
{
	this.master.hide();
	ACTIVE_MENU = null;
}

function BETMENU_INLINE_drop()
{
	hideActiveItem(this);
	ACTIVE_MENU = null;
}


function setActiveItem(menu, hilite)
{
	if(NS6 && menu.popup && menu!=ACTIVE_MENU) activateMenu(menu);
	if(menu.activeItem && hilite != menu.activeItem) menu.activeItem.hide();
	hilite.show();
	menu.activeItem = hilite;
}

function hideActiveItem(menu)
{
	if(menu.activeItem)
	{
		menu.activeItem.hide();
		menu.activeItem = null;
	}
	if(menu.popup) startDropTimer(1000);
}

function BETMENU(type)
{
	this.index = BETMENUS.length;
	BETMENUS[BETMENUS.length]=this;
		
	type = type.toLowerCase();

	this.x = 0;	
	this.y = 0;
	this.w = 121;
	this.ih = 12;
	this.vs = 1;
	this.mw = 2;
	this.mh = 3;

	this.borderColor = "#000000";
	this.on_bgColor = "#ff0000";
	this.on_linkStyle = "color: #ffffff; font-family: verdana, arial, helvetica, sans-serif; font-size: 10px; font-weight: bold; text-decoration:none;";
	this.off_bgColor = "#ffffff";
	this.off_linkStyle = "color: #000000; font-family: verdana, arial, helvetica, sans-serif; font-size: 10px; font-weight: bold; text-decoration:none;";
	
	this.caret = false;
	this.caret_src = BETMENU_caret_src;
	
	this.items = [{text:"empty",href:"#"}];
	this.itemHTML = BETMENU_itemHTML;
	
	this.inline = false;
	this.popup = false;
	
	this.activeItem = null;
	this.timer = null;

	if(type=="inline")
	{
		this.inline = true;
		this.caret = true;
		
		this.activate = function() {return true;};
		this.init = BETMENU_INLINE_init;
		this.drawSpacer = BETMENU_INLINE_drawSpacer;
		this.drop = BETMENU_INLINE_drop;
	}
	if(type=="popup")
	{
		this.popup = true;	
		this.init = BETMENU_POPUP_init;
		this.activate = BETMENU_POPUP_activate;
		this.drop = BETMENU_POPUP_drop;
		
		this.handle_on_src = BETMENU_trans_pixel_src;
		this.handle_h = 15;
		this.handle_vSpace = 2;
		this.handleSpacer_pixel_src = BETMENU_handleSpacer_pixel_src;
		
		// this.handle_yOffset is calculated later
	}
}

function BETMENU_INLINE_drawSpacer()
{
	var spacerHTML = "";

	for(i=0; i<this.items.length; i++)
	{
		thisItem = this.items[i];
		iw = this.w - (this.mw * 2);
		
		thisItem.lines = countLines(thisItem.text);	// add <br> search...

		spacerHTML += '\n<img src="'+BETMENU_trans_pixel_src+'" width="2" height="'+(this.ih * thisItem.lines)+'" border="0" alt=""><br>';
		if(i<this.items.length-1) spacerHTML += '<img src="'+BETMENU_borderColor_pixel_src+'" width="'+iw+'" height="1" hspace="2" vspace="0" border="0" alt=""><br>';
	}
	document.write(spacerHTML); 
};

function BETMENU_INLINE_init()
{
	iw = this.w - (this.mw * 2);

	offsetX = this.x + this.mw;
	offsetY = this.y + this.mh;

	for(i=0; i<this.items.length; i++)
	{
		thisItem = this.items[i];
		if(!thisItem.lines) thisItem.lines = 1;	// maybe remove
		
		offMarkupString = this.itemHTML(thisItem, (thisItem.defaultItem ? 1 : 0));
		onMarkupString = this.itemHTML(thisItem, 1);

		off = HtmlContainer(offMarkupString,iw,(this.ih * thisItem.lines),offsetX,offsetY);
		off.setBgColor(thisItem.defaultItem ? this.on_bgColor : this.off_bgColor);
		off.show();
			
		on = HtmlContainer(onMarkupString,iw,(this.ih * thisItem.lines),offsetX,offsetY);
		on.setBgColor(this.on_bgColor);		
		
		off.hilite = on;
		off.menu = this;
		off.index = i;
		if(NS6) off.onmouseover = function(){if(this.hilite!=this.menu.activeItem)hideActiveItem(this.menu);setActiveItem(this.menu, this.hilite)};

		if(NS6) on.onmouseout = function(){hideActiveItem(this.menu)};
		on.menu = this;
		on.index = i;

		off.itemHref = thisItem.href;
		off.isOffButton = true;
		on.itemHref = thisItem.href;
		on.isOnButton = true;
		
		if(i!=this.items.length-1) offsetY += (this.ih * thisItem.lines) + this.vs;
	}
}

var uniqueLinkID = 0;

function BETMENU_itemHTML(item,state)
{
	// remember to consider item.lines...

	HTML = "";
	
	linkStyle = (state==1 ? this.on_linkStyle : this.off_linkStyle);
	
	spWidth = (this.caret ? this.ih : 2);
	spSrc = ((this.caret && state==1) ? this.caret_src : BETMENU_trans_pixel_src);

	HTML += '<img src="'+spSrc+'" width="'+spWidth+'" height="'+this.ih+'" hspace="0" vspace="0" border="0" align="left" alt="">';
	HTML += '<a href="'+item.href+'" id="betmenulink'+(uniqueLinkID++)+'" style="'+linkStyle+ '">';

	if(item.lines==1) HTML += item.text;
	else
	{
		spacer = '<br clear="all"><img src="'+BETMENU_trans_pixel_src+'" width="'+spWidth+'" height="'+this.ih+'" hspace="0" vspace="0" border="0" align="left" alt="">';
		HTML += item.text.replace(addSpacer,spacer);
	}
	
	HTML += '</a>';

	return HTML;
}

function BETMENU_itemOpenDivTag(id,w,h,x,y,bgColor,boolVisible)
{
	visibility = (boolVisible==1 ? "inherit" : "hidden");
	return '<div id="'+id+'" style="top: '+y+'px; left: '+x+'px; width:'+w+'px; height:'+h+'px; position:absolute; visibility:'+visibility+'; background-color:'+bgColor+'; overflow:hidden; clip:rect(0,'+w+','+h+',0);">';
}

function BETMENU_POPUP_init()
{
	popupHTML='<table width="125" class="'+this.lableCSS+'"><tr><td height="17" align="center"><p class="wt-menu-copy">'+ this.lableText +'</p></td></tr></table><br><img src="'+this.handleSpacer_pixel_src+'" width="'+this.w+'" height="'+this.handle_vSpace+'" border="0" alt="">';
	handle_yOffset = this.handle_h + this.handle_vSpace;
	iw = this.w - (this.mw * 2);

	newIds = [];
	
	offsetX = this.mw;
	offsetY = handle_yOffset + this.mh;
	all_lines = 0;
	
	for(i=0; i<this.items.length; i++)
	{
		thisItem = this.items[i];
	
		thisItem.lines = countLines(thisItem.text);// add <br> search...
		all_lines += thisItem.lines;
		
		idStem = 'BETM'+this.index+'_item'+i; 
	
		if(NS4) thisItem.y = offsetY;
	
		else
		{
			off_openTag = BETMENU_itemOpenDivTag(idStem+'_off', iw, (this.ih * thisItem.lines), offsetX, offsetY, this.off_bgColor, 1);
			off_markupString = this.itemHTML(thisItem, 0);
			popupHTML+= (off_openTag + off_markupString + '</div>');
	
			on_openTag = BETMENU_itemOpenDivTag(idStem+'_on', iw, (this.ih * thisItem.lines), offsetX, offsetY, this.on_bgColor, 0);	
			on_markupString = this.itemHTML(thisItem, 1);
			popupHTML+= (on_openTag + on_markupString + '</div>');

			newIds[newIds.length] = idStem;
		}
		if(i!=this.items.length-1) offsetY += (this.ih * thisItem.lines) + this.vs;
	}	

	masterHeight = (all_lines * this.ih) + ((this.items.length-1) * this.vs) + this.handle_h + this.handle_vSpace + (this.mh * 2);
	this.master = HtmlContainer(popupHTML,this.w,masterHeight,this.x,this.y-handle_yOffset);
	this.master.setBgColor(this.borderColor);
	if(IE4UP && MAC) this.master.style.zIndex = 500;
	if(NS6)
	{
		this.master.onmouseover = function(){clearDropTimer();};
		this.master.onmouseout = function(){clearDropTimer(); startDropTimer(1500)};
	}
	
	this.masterHeight = masterHeight;
	
	for(i=0; i<this.items.length; i++)
	{
		thisItem = this.items[i];

		if(NS4)
		{
			off_markupString = this.itemHTML(thisItem, 0);
			on_markupString = this.itemHTML(thisItem, 1);

			off = HtmlContainer(off_markupString,iw,(this.ih * thisItem.lines),offsetX,thisItem.y,this.master);
			off.setBgColor(this.off_bgColor);
			off.show();
			
			on = HtmlContainer(on_markupString,iw,(this.ih * thisItem.lines),offsetX,thisItem.y,this.master);
			on.setBgColor(this.on_bgColor);		
		}
		if(NS6)		
		{
			off = document.getElementById(newIds[i]+'_off');
			on = document.getElementById(newIds[i]+'_on');
		}
		if(document.all)
		{
			off = document.all[newIds[i]+'_off'];
			on = document.all[newIds[i]+'_on'];
		}
		
		off.hilite = on;
		off.menu = this;
		off.index = i;
		off.isOffButton = true;
		if(NS6) off.onmouseout = function(){clearDropTimer();};
		if(NS6) off.onmouseover = function(){clearDropTimer(); setActiveItem(this.menu, this.hilite);};
	
		on.show = HtmlContainer_show;
		on.hide = HtmlContainer_hide;
		on.isOnButton = true;
		on.menu = this;
		on.index = i;		
		if(NS6) on.onmouseover = function(){clearDropTimer();};
//		if(NS6) on.onmouseout = function(){hideActiveItem(this.menu); startDropTimer(1000);};
		
		off.itemHref = thisItem.href;
		on.itemHref = thisItem.href;
	}

	this.master.menu = this;
	this.master.isBgPanel = true;
}

function startDropTimer(msecs)
{
	DROP_TIMER = setTimeout("if(ACTIVE_MENU)ACTIVE_MENU.drop()",msecs);
}

function clearDropTimer()
{
	if(DROP_TIMER)
	{
		clearTimeout(DROP_TIMER);
		DROP_TIMER = null;
	}
}