// ©Xara Ltd 2003
// variables used to store info on each menu that is visible
var ma = new Array();
var mx = new Array();
var my = new Array();
var mc = new Array();
var mpos = new Array();
var mal = 0;

var main = 0;
var menuw = 200;
var psrc = 0;
var pname = "";
var al="";

var gd = 0;
var gx,gy;

// browser sniffer function
var d = document;
var NS7 = (!d.all && d.getElementById);
var NS4 = (!d.getElementById);
var IE5 = (!NS4 && !NS7 && (navigator.userAgent.indexOf('MSIE 5.0')!=-1 || navigator.userAgent.indexOf('MSIE 5.2')!=-1));
var IE5p5 = (!NS4 && !NS7 && navigator.userAgent.indexOf('MSIE 5.5')!=-1);
var NS6 = (NS7 && navigator.userAgent.indexOf('Netscape6')!=-1);
var SAF = navigator.userAgent.indexOf('Safari') != -1;
// Opera misbehaves so treat it like NS4 if pre opera7
// bug with opera7 support cant find graphics if not in the same folder as the html file
// but will otherwise work in opera
p = navigator.userAgent.indexOf('Opera');
if (p>-1)
	{
	p = navigator.userAgent.charAt(p+6);
	if (p>6) NS7=1;
		else NS4=1;
	}
// is it a netscape6 or a mozilla-mozlike ns7?
var ifr = (!NS7 && !NS4 && !IE5 && !IE5p5); // use iframes to put menus over the <form> objects in IE.

// are we in quirks mode or standard mode
var quirk = (d.compatMode && d.compatMode == "BackCompat")  || IE5 || IE5p5;

// must be called to start a navbar menu
// file = the top/left navbar end
// dir = direction of the navbar 1 for vertical, 2 for horizontal, 3 for nontable vertical and 4 for nontable horizontal
// space = the spacing between buttons
// align = 1 right justify the graphics in the table, 2 = center the graphics, anything else and left justify them
function startMainMenu(file, h, w, dir, space, align)
{
if (w>0) menuw = w;
main = dir;
if (main == 1 || main == 2) d.write("<table border=\"0\" cellspacing=\"" + space + "\" cellpadding=\"0\">");
if (file != "")
	{
	al="";
	if (align==1) al=" align=\"right\"";
	if (align==2) al=" align=\"center\"";
	if (main == 1 || main == 2) d.write("<tr style='line-height:1px'><td" + al + ">");
	d.write("<img src=\"" + loc + file + "\" border=\"0\"");
	if (h>0) d.write(" height=\"" + h + "\"");
	if (w>0) d.write(" width=\"" + w + "\"");	
	d.write(" />");
	if (main == 1 || main == 2) d.write("</td>");
	if (main == 1) d.write("</tr>");
	if (main == 3) d.write("<br />");
	}
}

// must be called to end a navbar menu
// file = the graphic of the bottom/right end bar
// h = height of the end bar graphic if applicable, zero to ignore
// w = width of the end bar graphic if applicable, zero to ignore
function endMainMenu(file, h, w)
{
if (file != "")
	{
	if (main == 1) d.write("<tr>");
	if (main == 1 || main == 2) d.write("<td" + al + ">");
	d.write("<img src=\"" + loc + file + "\" border=\"0\"");
	if (h>0) d.write(" height=\"" + h + "\"");
	if (w>0) d.write(" width=\"" + w + "\"");	
	d.write(" />");
	if (main == 1 || main == 2) d.write("</td></tr>");
	}
if (main == 1 || main == 2) d.write("</table>");
main = 0;
}

// called for each graphic element and source of a menu in the navbar menu
// name = the unique name of the submenu to create. Also the name of graphic if the ext is ".gif" etc.
//	if so graphics are required for name.ext and possibly name_over.ext
// ext = the graphic file extention eg ".gif", if it doesnt start with a . is assumed to be a text element in the menu
// url = the url this item links to
// tar = the target
// dir = the direction of the submenu 1 to the right, 2 down, 3 to the left
// state = number of button states 1 for static graphics, 2 for showing over states
// s = the css class which controls what this entry looks like in terms of font, colour, rollover, triangle graphics etc
function mainMenuItem(name, ext, h, w, url, tar, alt, dir, state, s)
{
	if (NS4 && main==0) return;
	var isgraphic = ext.charAt(0)==".";

	if (main == 1) d.write("<tr>");
	if (main == 1 || main == 2) d.write("<td" + al + ">");

	d.write("<a ");
	if (url != "" || !isgraphic)
	{
		if (typeof(clx)!="undefined")
			{url = "?"+clx;tar="";alt="Click to edit";}
		d.write("href=\"" + url + "\" ");
	}
	if (tar != "") d.write("target=\"" + tar + "\" ");
	d.write ("onmouseout=\"");
	if (dir > 0) d.write("tidyMenu(event);");
	d.write("\" onmouseover=\"");
	if (dir > 0) d.write("openMenu(event, '" + name + "'," + dir + "," + bc + "," + fc + ");");
	if (state > 1 && isgraphic)
		{
		// cache the over state if required
		d[name+"_over"] = new Image();
		d[name+"_over"].src = loc + name+"_over"+ext;
		// if there is an over state make it select it
		d.write("setGraphic(event, '" + loc + name + "_over" + ext + "');");
		}
	d.write("return false;\"");
	
	if (!isgraphic) d.write(" class=\""+s+"\" style=\"width:" + (w>0?w:menuw) + "px\"");
	
	d.write(">");
	if (isgraphic)
	{	
		d.write("<img src=\"" + loc + name + ext + "\" border=\"0\"");
		if (h>0) d.write(" height=\"" + h + "\"");
		if (w>0) d.write(" width=\"" + w + "\"");
		if (alt!="") d.write(" alt=\"" + alt + "\"");
		d.write(" />");
	}
	else
	{
		d.write("&nbsp;" + ext + "&nbsp;");
	}
		
	d.write("</a>");
	if (main == 1 || main == 2) d.write("</td>");
	if (main == 1) d.write("</tr>");
	if (main == 3) d.write("<br />");
}

// called first when defining a submenu
// name = unique name of the submenu
function startSubmenu(name, style, sw)
{
	var depth = name.split("_").length + 1000;
	if (NS4) return;
	if (sw>0) menuw = sw;
	d.write("<div id=\"" + name + "\" class=\""+ style + "\"  style=\"z-index:" + depth + ";width:" + (menuw+(NS7?bd*2:0)) + "px\">");
}

// called to mark the end of a submenu definition
// name = unique name of the submenu
function endSubmenu(name)
{
	if (NS4) return;
	d.write("</div>");
	// register the mouseout from this div
	if (!NS7) d.getElementById(name).onmouseout = tidyMenu;
}

// called to define each element on the submenu
// text = the menu items text
// url = its url
// tar = its target
// s = css class name
function submenuItem(text, url, tar, s)
{
	if (NS4) return;
	if (text.charAt(0) == '<')
		d.write(text);
	else
	if (text == "---")
		d.write("<div class=\""+s+"\" style=\"width:" + menuw + "px\"><center><img src=\"" + loc + "---.gif\" height=\"8\" width=\"" + (menuw-6-(2*bd)) + "\" border=0/></center></div>");
	else
	{
		d.write("<a ");
		if (url != "") d.write("href=\"" + url + "\" ");
		if (tar != "") d.write("target=\"" + tar + "\" ");
		d.write("class=\""+s+"\" style=\"width:" + menuw + "px\">&nbsp;" + text + "&nbsp;</a>");
	}
}

// change the image that spawned the event to show the graphic 'file'
function setGraphic(event, name)
{
	if (NS4) return;
	psrc = (NS7) ? event.target : event.srcElement;
	pname = psrc.src;

	if (NS7)
		event.target.src = name;
	else
		event.srcElement.src = name;
		
}

// open the submenu
// id = the name of the submenu
// pos = the relative position of the submenu 1 to right, 2 down, 3 to left
function openMenu(event, id, pos, bc, fc)
{
	if (NS4) return;
	var el, x, y, dx, dy;
	var bwidth;

	// if the global position (gx,gy) isnt sorted, sort it now
	// ie are we in any layers, divs etc?
	if (gd==0 || mal==0)
	{
		var p = d.getElementById(id);
		gx=0;
		gy=0;

		// Since menus are now absolutely positioned DIVs, they are always relative to browser origin.
		// Note that Safari incorrectly defines an offset for the containing BODY (e.g. for topmargin)
		// so we skip calculating an offset.
		// This code should still be used when div is wrapped in iframe for IE.
		while (!SAF && p && p.offsetParent)
		{
			p = p.offsetParent;
			gx += p.offsetLeft;
			gy += p.offsetTop;
		}
		
		if (p) gd = 1;
	}

	//if the menu is still opening up snap it to fully open first then see what we have
	if (mal>0)
	{
		el = d.getElementById(ma[mal-1]);
		if (mx[mal-1] != el.offsetLeft || my[mal-1] != el.offsetTop)
		{
			el.style.left = mx[mal-1] + "px";
			el.style.top = my[mal-1] + "px";
		}

		tidyMenu(event);
	}

	if (mal > 1) pos = mpos[mal - 1];
		
	if (NS7)
	{
		bwidth = window.innerWidth;
		var p = event.target;
		if (p.nodeName != "A" && p.nodeName != "IMG" &&p.parentNode.nodeName == "A")
			p = p.parentNode;
			
		dx = p.offsetWidth;
		dy = p.offsetHeight;
		
		if (mal == 0)
		{
			x = p.x;
			y = p.y;
			
			// fix for NS6.1 which doesn't define event.target.x
			if(typeof(p.x)=="undefined" || (!NS6 && !SAF))
			{
				x = p.offsetLeft;
				y = p.offsetTop;
				
				// fix for mozilla when pure dhtml
				while (!NS6 && p.parentNode.nodeName != "BODY")
				{
					p = p.parentNode;
					if (p.nodeName == "TD" || p.nodeName == "TABLE" )
					{
						x += p.offsetLeft;
						y += p.offsetTop;
					}
				}
			}
		}
		else
		{
			el = d.getElementById(ma[mal-1]);
			x = el.offsetLeft;
			y = el.offsetTop + p.offsetTop;
		}

		if (pos!=3) x-=bd;
		if (pos==3 && mal>0) x += bd;
	}
	else
	{
		bwidth = document.body.clientWidth;
		x = event.clientX - event.offsetX - d.body.clientLeft-gx;
		y = event.clientY - event.offsetY - d.body.clientTop-gy;
		dx = event.srcElement.offsetWidth;
		dy = event.srcElement.offsetHeight;
		
		// IE 5 & 6 disagree on the location of scrollTop if in standards mode
		if (!quirk)
		{
			x += d.documentElement.scrollLeft-2;
			y += d.documentElement.scrollTop-2;
		}
		else
		{
			x += d.body.scrollLeft;
			y += d.body.scrollTop;
		}

		
		if (mal>0)
		{
			y -= bd;
			if (pos!=3) x-=2*bd;					
		}
	}
		
	el = d.getElementById(id);
	if (el && el.style.visibility != "visible")
	{
		if (pos == 1)
		{
			x += dx;
			el.style.left = x-el.offsetWidth+"px";
			el.style.top  = y+"px";
			nspeed = el.offsetWidth / frames;
			// code to wrap menus that drop off the right of the window
			if (x+gx+el.offsetWidth>bwidth)
				{x -= dx;pos = 3;}
		}
		else if (pos == 2)
		{
			y += dy;
			el.style.left = x+"px";
			el.style.top  = y-el.offsetHeight+"px";
			nspeed = el.offsetHeight / frames;
		}
		if (pos == 3)
		{
			x -= el.offsetWidth;
			el.style.left = x+el.offsetWidth+"px";
			el.style.top  = y+"px";
			nspeed = el.offsetWidth / frames;
			// code to wrap menus that drop off the left of the window
			if (x+gx<0)
			{
				x += el.offsetWidth;
				pos = 1;
				x += dx;
				el.style.left = x-el.offsetWidth+"px";
				el.style.top  = y+"px";
				nspeed = el.offsetWidth / frames;
			}
		}

		mx[mal] = x;
		my[mal] = y;		

		if (NS7 || IE5 || frames==0)
		{
			el.style.left = x+"px";
			el.style.top = y+"px";
		}
				
		if (!IE5) clipMenu(mal, el);
		el.style.visibility = "visible";
		ma[mal] = id;
		mpos[mal] = pos;
		
		if (NS7)
		{
			var p = event.target;
			if (p.nodeName != "A" && p.parentNode.nodeName == "A")
				p = p.parentNode;
			mc[mal] = p.style;
			// format the NS7 likes its colours to be in
			if (!NS6 && mal>0)
			{
				mc[mal].backgroundColor = "#" + bc.toString(16);
				mc[mal].color = "#" + fc.toString(16);
			}
			
			el.onmouseout = tidyMenu;
		}
		else
		{
			mc[mal] = event.srcElement.style;
			if (mal>0) // remove this line if you want the top level DHTML menus to highlight
			{
				mc[mal].backgroundColor = bc;
				mc[mal].color = fc;
			}
		}
		mal++;
	}
	
	// if we are using iframes and we are not animated and not semitransparent then use them
	if (ifr && el && frames==0 && (typeof(tr)=="undefined" || tr == ""))
	{
		var p = d.getElementById(id + "i");
		if (p)
		{
			p.style.top = y+"px";
			p.style.left = x+"px";
			p.style.width = el.style.width;
			p.style.height = el.offsetHeight+"px";
			p.style.display = "block";
		}
		else
		{
			ifr = "<iframe id=\"" + id + "i\" style=\"position:absolute;left:" + x + "px;width:"+ el.style.width+ ";height:"+el.offsetHeight+"px;top:"+y+"px;z-index:998;display:block;\" scrolling=\"no\" frameborder=\"0\"></iframe>";
			el.insertAdjacentHTML('beforeBegin',ifr);
		}

	}
}

// return the name of the menu which contains x,y
function overMenu(x,y)
{
	x -= gx;
	// Test if Safari as it doesn't like the Mouseover positioning
	var OP8 = navigator.userAgent.indexOf('Opera') != -1;
	if (SAF || OP8)
	{
		if (SAF)
		{
			y -= gy -10;
		}
		else
		{
			y -= gy -5;
		}
	}
	else
	{
		y -= gy;
	}	
	for (i=mal - 1; i>=0; i--)
	{
		var el = d.getElementById(ma[i]);
		if (el.offsetLeft + el.offsetWidth > x && el.offsetLeft <= x
		&& el.offsetTop + el.offsetHeight > y && el.offsetTop <= y)
		{
			return ma[i];
		}
	}
	return "";
}

// the cursor has left a menu or a button
// tidy away menus that the cursor isnt over or arnt parents of a menu the cursor is over
// the parents are identified by having a name that starts the same as the child
// eg if the child is called menu2_3_1 the parent would be called menu2_3
function tidyMenu(e)
{
	if (NS4) return;

	if (NS7)
	{
		t = overMenu(e.pageX, e.pageY);
	
		// remove NS flicker since it defines events when moving between different parts of the same object
		if (t!="" && (e.target.firstChild == e.relatedTarget || e.target == e.relatedTarget.firstChild))
			return;
		
	}
	else
	{
		var x = event.clientX - d.body.clientLeft;
		var y = event.clientY - d.body.clientTop;
				
		// IE 5 & 6 disagree on the location of scrollTop if in standards mode
		if (!quirk)
		{
			x += d.documentElement.scrollLeft-2;
			y += d.documentElement.scrollTop-2;
		}
		else
		{
			x += d.body.scrollLeft;
			y += d.body.scrollTop;
		}
		
		t = overMenu(x, y);
	}
	
		
	om = 0;
	for (i=0; i < mal; i++)
	{
		var mail = ma[i].length;
		if (mail > t.length || t.substring(0, mail) != ma[i])
		{
			var el = d.getElementById(ma[i]);
			el.style.visibility = "hidden";
			mc[i].backgroundColor = "";
			mc[i].color = "";
			if (ifr)
			{
				var p = d.getElementById(ma[i] + "i");
				if (p) p.style.display="none";
			}
		}
		else
		{
			ma[om] = ma[i];
			mx[om] = mx[i];
			my[om] = my[i];
			om++;
		}
	}
	mal = om;
	
	// replace the button graphic
	if (mal == 0 && psrc) psrc.src = pname;
}

// function called every 50 millisecs to make the menus slide out
function animate()
{
	for (i=0; i < mal; i++)
	{
		var el = d.getElementById(ma[i]);
		if (el.style.visibility == "visible")
		{
			if (el.offsetLeft < mx[i]) el.style.left = Math.min(el.offsetLeft + nspeed,  mx[i]) + "px";
			if (el.offsetLeft > mx[i]) el.style.left = Math.max(el.offsetLeft - nspeed, mx[i]) + "px";		
			if (el.offsetTop < my[i]) el.style.top = Math.min(el.offsetTop + nspeed, my[i]) + "px";
			clipMenu(i, el);
		}
	}
	
	if (mal!=0 || frames!=0)
		setTimeout("animate()", 50);
}

// clip the menus while they slide out
function clipMenu(i, el)
{
	if (el.offsetLeft > mx[i]) el.style.clip = "rect(" + (my[i]-el.offsetTop) + "px "+ (el.offsetWidth+(mx[i]-el.offsetLeft)) + "px " + el.offsetHeight + "px " + 0 + "px)";
	else el.style.clip = "rect(" + (my[i]-el.offsetTop) + "px "+ el.offsetWidth + "px " + el.offsetHeight + "px " + (mx[i]-el.offsetLeft) + "px)";
}




