


//// Called to parse the config and initialize/draw the menuing elements
function _init(){

	//alert(document.all.tags("IMG").length);
	//for(var i=0;i<document.all.length;i++){
	//	var identify=document.all[i].getAttribute("id");
    if(!ns4)
    {
	for(var i=0;i<document.all.tags("IMG").length;i++){
		var identify=document.all.tags("IMG")[i].getAttribute("id");
		if(identify){

			if(identify.indexOf('src:menusrc')>=0)
			{
			var document_all_i=document.all.tags("IMG")[i];			
			
			countall++;

			parentMenuTop=getPageCoordinates(document_all_i).y;
			parentMenuLeft=getPageCoordinates(document_all_i).x;
						
			var attribs = _eat_attrib(identify);
			
			var tree = new Array();
			var src = document.all.item(attribs["src"]).innerText;	// get the config info from the contents of the designated source element

			src = _strip(src);	// remove whitespace informatin
			tree = _compile(tree,src);	// turn it into a multilevel array

			// at this time, associate the element the
			// menu is attached to with the created menuing elements
			var menuId = _menus.length;	
		
			document_all_i.menuId = menuId;
			document_all_i.onmouseover = _rootmouseover;
			document_all_i.onmouseout = _rootmouseout;

			
			// get and preset designer preferences
			//var attribs = _eat_attrib(document.all[i].getAttribute("id"));
			attribs["border-width"] = (attribs["border-width"]!=null?attribs["border-width"]:0);
			attribs["border-color"] = (attribs["border-color"]!=null?attribs["border-color"]:"black");
			attribs["background"] = (attribs["background"]!=null?attribs["background"]:"#cccccc");
			attribs["highlight"] = (attribs["highlight"]!=null?attribs["highlight"]:"#DCDCDC");
			attribs["font-size"] = (attribs["font-size"]!=null?attribs["font-size"]:"7pt");
			attribs["font-family"] = (attribs["font-family"]!=null?attribs["font-family"]:"Verdana,Arial");
			attribs["font-color"] = (attribs["font-color"]!=null?attribs["font-color"]:"#000000");
			attribs["icon-color"] = (attribs["icon-color"]!=null?attribs["icon-color"]:"black");
			attribs["arrow"] = (attribs["arrow"]!=null?attribs["arrow"]:true);
			attribs["showrow"] = (attribs["showrow"]!=null?attribs["showrow"]:true);
			attribs["height"] = (attribs["height"]!=null?attribs["height"]:22);
			attribs["width"] = (attribs["width"]!=null?attribs["width"]:150);
			attribs["arrow-offset"] = (attribs["arrow-offset"]!=null?Number(attribs["arrow-offset"]):170);
			attribs["menu-offset"] = (attribs["menu-offset"]!=null?Number(attribs["menu-offset"]):1);
			attribs["menu-topoffset"] = (attribs["menu-topoffset"]!=null?Number(attribs["menu-topoffset"]):0);
			attribs["wait"] = (attribs["wait"]!=null?Number(attribs["wait"]):50);
			if(attribs["top"]!=null)
			{
				menuRootTop[menuId]=Number(attribs["top"]);
			}
			else
			{
				menuRootTop[menuId]=parentMenuTop+menuRootHeight;
				parentMenuTop=0;
			}
			
			if(attribs["left"]!=null)
			{
				menuRootLeft[menuId]=Number(attribs["left"]);
			}
			else
			{
				menuRootLeft[menuId]=parentMenuLeft+5;
				parentMenuLeft=0;
			}
			
			document_all_i.attribs = attribs;

			// create all of the needed elements for this menu
			treeArray[menuId]=tree;
			attribArray[menuId]=attribs;
			
			_create(document_all_i,tree);
		}
				
		
		}

	}
   }
   //alert("countall is "+countall);  //count is 66
		
}
window.onload = _init;
/////////////////////////

function getPageCoordinates (element) {
  var x = y = 0;
  do {
    x+= element.offsetLeft;
    y += element.offsetTop;
  }
  while ((element = element.offsetParent));
  return {x: x, y: y};
}



/////////////////////////
//// takes a position and a string,
//// returns the position of the next unpaired "]" character
function _pair(after,str){
	while(1){
		var nextA = str.indexOf("[",after + 1);
		var nextB = str.indexOf("]",after + 1);
		if(nextA == -1){
			return nextB;
		}
		if(nextA > nextB){
			return nextB;
		}
		after = _pair(nextA,str);
	}
}

/////////////////////////
//// transforms raw text input into a multilevel array
//// returns an array based on the string input
function _compile(ary,str){
	while(1){ // keep circling and eating the str

		// when the str is empty, return the built array
		if(str.length == 0){
			return ary;
		}

		// is there any more sub-arrays?
		var nextA = str.indexOf("[");
		if(nextA == -1){
			var go = str;
			var spawn = "";
			str = "";
		}else{
			var go = str.substring(0,nextA);
			var spawn = str.substring(str.indexOf("[") + 1,_pair(str.indexOf("["),str))
			str = str.substring(_pair(str.indexOf("["),str) + 1,str.length);
		}

		// build a flat array of key/value pairs and strip out the empty elements
		var A = go.split(";");
		var A2 = new Array();
		for(var j=0;j<A.length;j++){
			if(A[j].indexOf("!") != -1){
				A2[A2.length] = A[j];
			}
		}
		A = A2;

		// parse the flat array
		for(var i = 0;i < A.length;i++){
			var tmpA = A[i].split("!");
			var thisary = ary.length;
			ary[thisary] = new Array();

			// attach a sub array if needed
			if((i+1)==A.length && spawn.length != ""){
				ary[thisary] = _compile(ary[thisary],spawn);
			}
			// assign properties to this array based on the parsed array key/value pairs
			ary[thisary].name = tmpA[0];
			ary[thisary].url = tmpA[1];
			ary[thisary].desc = tmpA[2];
			ary[thisary].icon = tmpA[3];
		}
	}
}
///////////////////////


///////////////////////
//// Actually draw and initialize elements from the array
function _create(e,ary){

	var str = new String();
	var menuId = _menus.length;	// save the ID for this menu
	str 	= '<DIV ID="_div_'
		+ menuId		// give it a unique name
		+ '" STYLE="visibility:hidden;position:absolute;top:0;left:0;width:'
		+ e.attribs["width"]
		+ ';height:10;background-color:black;">';
	e.insertAdjacentHTML("AfterEnd",str);	// paste it in below the root element
	var thismenu = document.all.item("_div_" + menuId);	// save a reference to the element object
	_menus[menuId] = thismenu;	// save a reference to the element object in a global array for later use
	thismenu.menuId = menuId;	// tell the element object where it is in the global array
	thismenu.close = _close;	// assign a close function to it
	thismenu.menuParent = null;	// initialize some variables to null
	thismenu.menuChild = null;
	thismenu.highlightBG = e.attribs["highlight"];
	thismenu.normalBG = e.attribs["background"];
	thismenu.menuOffset = e.attribs["menu-offset"];
	thismenu.menuTopoffset = e.attribs["menu-topoffset"];
	thismenu.waitDelay = e.attribs["wait"];
	thismenu.realtime=1;
	var prevBottom = 0;		// initialize where the option elements should start drawing

	thismenu.style.pixelHeight = prevBottom;	// set the height on the menu element
}
/////////////////////


function _realtimecreate(e,ary, thismenu, menuId)
{
	var str = new String();
	var prevBottom = 0;
	
	// create a span element for every option
	for(var i=0;i<ary.length;i++){
		var tempS=ary[i].name;
		
		eval('urlArray['+menuId+''+i+'] = ary['+i+'].url;');
		
		///alert("  URL "+i+"  ="+ary[i].url);
		
		if(tempS.indexOf("menu_division")>=0)
		{
			str 	= '<SPAN ID="_link_d'	// assign a unique name
				+ menuId
				//+ '_'
				+ i
				+ '" STYLE="position:absolute;top:'
				+ prevBottom		// position it correctly
				+ ';left:0;height:'
				+ e.attribs["height"]
				+ ';overflow:hidden;width:'
				+ e.attribs["width"]
				+ ';background:'
				+ e.attribs["background"]
				+ ';border-width:'
				+ 0
				+ ';border-style:solid;border-color:'
				+ e.attribs["border-color"]
				+ ';';
				if(e.attribs["showrow"]!=true && i != 0){
					str	+='border-top-width:0;';
				}
				str	+='text-align:left;font-size:'
					+ e.attribs["font-size"]
					+ ';padding-left:4;">';
				if(ary[i].icon!=null){
						str	+='<SPAN><img border=0 src="'+ ary[i].icon+'">'
					+ '</SPAN>';
			}
			str	+='<hr style="color: #aaaaaa" width=90% align=center> '
			str	+='</SPAN>';

		}
		else
		{
		str 	= '<SPAN ID="_link_'	// assign a unique name
			+ menuId
			//+ '_'
			+ i
			+ '" STYLE="position:absolute;top:'
			+ prevBottom		// position it correctly
			+ ';left:0;height:'
			+ e.attribs["height"]
			+ ';overflow:hidden;width:'
			+ e.attribs["width"]
			+ ';background:'
			+ e.attribs["background"]
			+ ';border-width:'
			+ e.attribs["border-width"]
			+ ';border-style:solid;border-color:'
			+ e.attribs["border-color"]
			+ ';';
		if(e.attribs["showrow"]!=true && i != 0){
			str	+='border-top-width:0;';
		}
		str	+='text-align:left;font-size:'
			+ e.attribs["font-size"]
			+ ';padding-left:4;">';
		if(ary[i].icon!=null){
		
			str	+='<SPAN><img border=0 src="'+ ary[i].icon+'">'
				+ '</SPAN>';
		}
		str	+='<A HREF="'
			+ ary[i].url
			+ '" TITLE="'
			+ ary[i].desc
			+ '" STYLE="font-family:'
			+ e.attribs["font-family"]
			+ ';color:'
			+ e.attribs["font-color"]
			+ ';font-weight: bold; text-decoration: none'
			+ ';padding-left:3;"'
			+ ' ID="_link_a'	// assign a unique name
			+ menuId
			//+ '_'
			+ i
			+ '"'
			+'>'
			+ ary[i].name		// fill in the content/name
			+ '</A>';
		if(ary[i].length > 0 && e.attribs["arrow"]){
			str	+='<SPAN STYLE="font-family:webdings;position:absolute;top:0;left:'
				+ e.attribs["arrow-offset"]
				+ ';width:'
				+ (e.attribs["width"] - e.attribs["arrow-offset"])
				+ ';overflow:hidden;color:'
				+ e.attribs["icon-color"]
				+ '">&#052;</SPAN>';
		}
		str	+='</SPAN>';
		}
		
		thismenu.insertAdjacentHTML("BeforeEnd",str);	// insert it inside the menu element
	
		if(tempS.indexOf("menu_division")>=0)
		{
			thisoption = document.all.item('_link_d' + menuId  + i);//'_'
		}
		else
		{
			thisoption = document.all.item('_link_' + menuId  + i);//'_'
		}

		thisoption.menuId = menuId;	// save some references and initialize some variables

		thisoption.onmouseover = _optionmouseover;//Division;
		thisoption.onmouseout = _optionmouseout;//Division;

		if(tempS.indexOf("menu_division")<0)
		{
			thisoption.onclick = _optiononclick;
		}
		
		if(thisoption.style!=null)
		{
			prevBottom = thisoption.style.pixelTop + (e.attribs["height"] - e.attribs["border-width"]);	// where should the next one draw?
		}
		else
		{
			prevBottom = (e.attribs["height"] - e.attribs["border-width"]);	// where should the next one draw?
		}
		if(ary[i].length > 0){		// For submenu here, better draw it!
			alert("ERROR with submenu, not dynamic");
			thisoption.menuChild=_menus.length;	// tell this option that it has a child
			_create(e,ary[i]);
		}
	}
	

}


///////////////////////////////////////////
///////////////////////////////////////////
//// All the following are used on the "fly"
//// as needed during the documents life
////
////

// Decide browser version
var ns4 = (document.layers)? true:false;
var ns6 = (document.getElementById)? true:false;
var ie4 = (document.all)? true:false;
var ie5 = false;



var _menus = new Array();	// global array of menus
var _currentMenu = null;	// temporary storage of the current and current root menu elements
var _rootMenu = null;
var _rootKludge = null;
var _zIndex = 1;
_menus[0]=_zIndex;

var menuRootTop=new Array();
var menuRootLeft=new Array();
var urlArray=new Array();
var treeArray=new Array();

var attribArray=new Array();

var parentMenuTop=0;
var parentMenuLeft=0;
var countall=0;

var menuRootHeight=10;
var shouldNotShowRoot=0;
var menu_offsetHeight=200;
var menu_offsetWidth=150;

/////////////////////
//// show the menu for this object
function _rootmouseover(){
	var e = window.event.srcElement;
	

	var thismenu = _menus[e.menuId];
	
	if(thismenu.realtime)
	{
		thismenu.realtime=0;
		e.attribs=attribArray[e.menuId];
		_realtimecreate(e, treeArray[e.menuId], thismenu, e.menuId);
		
	}
	
	if(_rootMenu != null){
		_menus[_rootMenu].close();
	}
	
	
	
	_rootMenu = e.menuId;	// store a temporary reference
	_rootKludge = e.menuId;
	//var attribs = _eat_attrib(e.getAttribute("id"));	// initialize its position based on the config
	
	var rightedge = document.body.clientWidth- event.screenX;//event.clientX;
	var bottomedge = document.body.clientHeight - event.screenY;//event.clientY;
		
	if (rightedge < menu_offsetWidth)
	rightedge = menuRootLeft[e.menuId] - menu_offsetWidth;//document.body.scrollLeft
	else
	rightedge =  menuRootLeft[e.menuId];
		
	if (bottomedge < menu_offsetHeight)
	bottomedge =  menuRootTop[e.menuId] - menu_offsetHeight;//document.body.scrollTop 
	else
	bottomedge = menuRootTop[e.menuId];
	
	
	_menus[e.menuId].style.pixelTop =bottomedge ;
	_menus[e.menuId].style.pixelLeft =rightedge;

	


	_menus[e.menuId].style.visibility = "visible";
	_menus[e.menuId].style.zIndex = _zIndex++;
}
////////////////////

////////////////////
//// if the mouse leaves the main element,
//// try to close the displayed menu
function _rootmouseout(){
	if(shouldNotShowRoot)
	{
		_rootKludge = null;
		window.setTimeout("if(_rootKludge != " + _rootMenu + "){_menus[" + _rootMenu + "].close();}",_menus[_rootMenu].waitDelay);
	}

}
////////////////////


////////////////////
//// nifty little IMPORTANT recursive function that 
//// returns true only if the id to check
//// is in the heritage of the current menu!
function _heritage(menu,check){
	if(_menus[menu].menuId == check){
		return true;
	}else{
		// if I have a parent, check it, otherwise the answer is no
		if(_menus[menu].menuParent != null){
			return _heritage(_menus[menu].menuParent,check);
		}else{
			return false;
		}
	}
}
///////////////////


///////////////////
//// associated with every menu element object,
//// close this menu unless it is still active
function _close(){
	if(_currentMenu != null){
		if(_heritage(_currentMenu,this.menuId)){
			return;
		}
	}
	if(this.menuChild != null){
		_menus[this.menuChild].close();
	}
	this.style.visibility = "hidden";
	this.menuChild = null;
	this.menuParent = null;
}
//////////////////



function _optiononclick()
{
	var thisID=window.event.srcElement.getAttribute("id");
	if(thisID.indexOf("_link_")>=0)
	{
		//close root window, otherwise with popup window showing, this menu still showing
		window.setTimeout("_menus[_rootMenu].close()",_menus[_rootMenu].waitDelay);
		_currentMenu = null;

		var dest=thisID.substring(6,thisID.length);
		if(dest.indexOf("a")>=0)
		{
			dest=dest.substring(1,dest.length);
		}
		

		//eval('window.location.href=urlArray['+thisID.substring(6,thisID.length)+'];');
		window.location.href=urlArray[dest];

	}
	// Do not let <A> tag process this event again
	window.event.returnValue = false;
	//window.event.cancelBubble = true;
}


//////////////////
//// when the mouse passes over an option
function _optionmouseover(){
	if(window.event.srcElement==null||window.event.fromElement==null)
	{
		return;
	}

	// some ugly kludges to reduce flashing when the mouse passes through a subelement of the option
	if(window.event.srcElement.menuId==null && window.event.fromElement.id == window.event.srcElement.offsetParent.id){
		return;
	}
	if(window.event.fromElement.offsetParent!=null){
		if(window.event.fromElement.menuId==null && window.event.srcElement.id == window.event.fromElement.offsetParent.id){
			return;
		}
	}
	
	shouldNotShowRoot=0;
	
	if(window.event.srcElement.menuId==null){
		var e = window.event.srcElement.offsetParent;
	}else{
		var e = window.event.srcElement;
	}
	
	var thisID1=window.event.srcElement.getAttribute("id");
	if(thisID1.indexOf("_link_d")<0)
	{
		e.style.background = _menus[e.menuId].highlightBG;		// highlight it
	}
	
	_currentMenu = e.menuId;		// reset the current menu
	if(_menus[_currentMenu].menuChild != null){	// close any previous children
		_menus[_menus[_currentMenu].menuChild].close();
	}
	if(e.menuChild!=null){		// if this option has a submenu, display it
		_menus[_currentMenu].menuChild = e.menuChild;	// tell the menu it has a child
		_menus[e.menuChild].menuParent = _currentMenu;	// tell the child it has a parent
		_menus[e.menuChild].style.pixelTop = _menus[_currentMenu].style.pixelTop + e.style.pixelTop + _menus[_currentMenu].menuTopoffset;
		_menus[e.menuChild].style.pixelLeft = _menus[_currentMenu].style.pixelLeft + _menus[_currentMenu].style.pixelWidth + _menus[_currentMenu].menuOffset;
		_menus[e.menuChild].style.visibility = "visible";
		_menus[e.menuChild].style.zIndex = _zIndex++;
	}
}
/////////////////


/////////////////
//// when the mouse leaves the option
function _optionmouseout(){

	// some ugly kludges to reduce flashing when the mouse passes through a subelement of the option
	if(window.event.srcElement==null||window.event.toElement==null)
	{
		return;
	}
	
	if(window.event.srcElement.menuId==null && window.event.toElement.id == window.event.srcElement.offsetParent.id){
		return;
	}
	if(window.event.toElement.offsetParent!=null){
		if(window.event.toElement.menuId==null && window.event.srcElement.id == window.event.toElement.offsetParent.id){
			return;
		}
	}
	
		shouldNotShowRoot=1;
	
	if(window.event.srcElement.menuId==null){
		var e = window.event.srcElement.offsetParent;
	}else{
		var e = window.event.srcElement;
	}
	
	var thisID2=window.event.srcElement.getAttribute("id");
	if(thisID2.indexOf("_link_d")<0)
	{
		e.style.background = _menus[e.menuId].normalBG;		// unhighlight it
	}
	
	var to = window.event.toElement.menuId;		// greener pasture?
	if(to!=null){	// leaving to a menu element
		if(to!=e.menuId){	// not this menu
			_menus[_currentMenu].close();
		}else{			// this menu, but hide any existing children
			if(_menus[e.menuId].menuChild!=null){
				_menus[_menus[e.menuId].menuChild].close();
			}
		}
	}else{		// wait a bit, and close the whole menu
		window.setTimeout("_menus[_rootMenu].close()",_menus[_rootMenu].waitDelay);
	}
	_currentMenu = null;
}
//////////////////




///////////////////////////////////////////
///////////////////////////////////////////
//// Common Utility Functions
////
////
////


//////////////////
//// util for element attribute parsing
//// returns an array of all of the keys = values
function _eat_attrib(str){
	var chunks = new Array();
	var all = new Array();

	chunks=str.split(";");

	for(var i=0;i<chunks.length;i++){
		var tmpA = new Array();
		tmpA=chunks[i].split(":");
		all[tmpA[0]]=tmpA[1];
	}
	return all;
}
////////////////////


//////////////////////
//// util to remove characters from input string
//// and return flattened string
function _strip(str){
	var A = new Array();

	A = str.split("\t");
	str = A.join("");

	A = str.split("\n");
	str = A.join("");

	A = str.split("\r");
	str = A.join("");

	return str;
}
//////////////////

