var menus;

var menuson = false;



var arrow = new Image(); arrow.src = '../Images/menuarrow.gif';

var arrowhover = new Image(); arrowhover.src = '../Images/menuarrowhover.gif';



var Op = navigator.userAgent.indexOf('Opera') != -1;

var IE = navigator.userAgent.indexOf('MSIE') != -1 && !Op;



function openBareWindow(url){

	newWindow = window.open("", "Screenshot", "width=730,height=510,resizable");

	newWindow.document.open();

	newWindow.document.write("<html><body><head><title>Screenshot</title></head><center>", "<img src = '" + url + "'>", "<br>", "<a href = 'javascript:this.close()'>close</a>", "</center></body></html>");

	newWindow.document.close();

	}



function Menu(activator, list, showbelow)

{

	this.activator = activator;

	this.list = list;

	this.siblings = new Array(); // must be defined later

	this.children = new Array(); // same

	this.showbelow = showbelow;

	

	if (this.list)

	{

		this.list.style.position = 'absolute';

		this.activator.onclick = function(e) {this.associatedMenu.show(); return Menu.noclickeffect(e);};

		

		if (!showbelow)

		{

			this.activator.style.paddingRight = '3px';

			this.activator.style.width = '150px';

			var img = document.createElement('img');

			img.src = arrow.src;

			this.activator.insertBefore(img, this.activator.firstChild);

			this.arrow = img;

		}

		this.shown = false;

	}

	else

	{

		this.activator.onclick = Menu.finished;

	}



	this.activator.associatedMenu = this;

	this.activator.onmouseover = function() {flag=true;if (menuson) {this.associatedMenu.show()}};

	//this.activator.onmouseout  = function() {this.associatedMenu.hide()};

}

Menu.removeall = function() {

	document.onclick = null;

	menuson = false;

	for (var a=0; a < menus.length; a++)

		menus[a].hide();

	return false;

}

Menu.finished = function() {

	document.onclick = null;

	menuson = false;

}

Menu.noclickeffect = function(e) {

	if (IE) e = event;

	e.cancelBubble = true;

	return false;

}

Menu.prototype.toggleMenus = function(e) {

	menuson = !menuson;

	if (menuson)

		this.show();

	else

		this.hide();



	return Menu.noclickeffect(e);

}

Menu.prototype.updatelistpos = function()

{

	if (!this.list)

		return;

	//this.activator = this.activator.offsetParent;

	var left = getLeft(this.activator);

	var top = getTop(this.activator);

	if (this.showbelow)

	{

		//alert(getTop (this.activator));

		this.list.style.left = left + 'px';

		this.list.style.top  = top+1 + this.activator.offsetHeight + 'px';

	}

	else

	{

		if (getFullLeft(this.activator) + this.activator.offsetWidth + 160 > getWindowRight()) {

			this.list.style.left = '';

			this.list.style.right = this.activator.offsetWidth+2  + 'px';

		}

		else

			this.list.style.left = left+1 + this.activator.offsetWidth  + 'px';

		this.list.style.top  = top-2 + 'px';

	}

}

Menu.prototype.show = function()

{

	this.updatelistpos();

	

	for (var a=0; a < this.siblings.length; a++)

	{

		if (this.siblings[a] != this)

			this.siblings[a].hide();

	}

	if (this.list && !this.shown)

	{

		this.list.style.display = 'block';

		this.activator.className = 'open';

		if (!this.showbelow)

			this.arrow.src = arrowhover.src;

		this.shown = true;

	}



	if (document.onclick != Menu.removeall) // (speedup)

		document.onclick = Menu.removeall;

}

Menu.prototype.hide = function()

{

	if (this.list && this.shown)

	{

		for (var a=0; a < this.children.length; a++)

		{

			this.children[a].hide();

		}

		this.list.style.display = 'none';

		this.activator.className = '';

		if (!this.showbelow)

			this.arrow.src = arrow.src;

		this.shown = false;

	}

}



function getLeft(element)

{

	var toreturn = 0;

	if (element.additionalBorderWidth && IE)

		toreturn += element.additionalBorderWidth;

	if (element.offsetParent && !element.positionedAbsolutely)

		toreturn += element.offsetLeft + getLeft(element.offsetParent);

	return toreturn;

}

function getFullLeft(element)

{

	var toreturn = 0;

	if (element.additionalBorderWidth && IE)

		toreturn += element.additionalBorderWidth;

	if (element.offsetParent)

		toreturn += element.offsetLeft + getFullLeft(element.offsetParent);

	return toreturn;

}

function getTop(element)

{

	var toreturn = 0;

	if (element.additionalBorderHeight && IE)

		toreturn += element.additionalBorderHeight;

	if (element.offsetParent && !element.positionedAbsolutely)

		toreturn += element.offsetTop  + getTop (element.offsetParent);

	return toreturn;

}

function getWindowRight()

{

	var scrollLeft = 0;

	if (typeof(window.pageXOffset) != 'undefined')

		scrollLeft = window.pageXOffset;

	else if (typeof(document.documentElement.scrollLeft) != 'undefined')

		scrollLeft = document.documentElement.scrollLeft;



	if (IE || Op)

		return scrollLeft + document.documentElement.clientWidth;

	if (window.innerWidth)

		return scrollLeft + window.innerWidth;

	return Number.POSITIVE_INFINITY; // (really big window)

}



function setupmenu()

{

	var menuactivators = new Array();

	var menu = document.getElementById('menu');

	menu.additionalBorderHeight = 6;

	var tds = menu.getElementsByTagName('td');

	for (var a=0; a < tds.length; a++)

	{

		menuactivators[menuactivators.length] = tds[a].getElementsByTagName('a')[0];

	}

	

	menus = constructMenus(menuactivators, true);

}

function constructMenus(menuactivators, showbelow)

{

	var menus = new Array();

	for (var a=0; a < menuactivators.length; a++)

	{

		var thislist = menuactivators[a].parentNode.getElementsByTagName('ul');

		if (thislist.length > 0)

		{

			thislist = thislist[0];

			thislist.positionedAbsolutely = true;

			var thismenu = menus.length;

			menus[thismenu] = new Menu(menuactivators[a], thislist, showbelow);

			

			var submenuactivators = new Array();

			for (var b=0; b < thislist.childNodes.length; b++)

			{

				if (thislist.childNodes[b].nodeName && thislist.childNodes[b].nodeName.toUpperCase() == 'LI')

				{

					submenuactivators[submenuactivators.length] = thislist.childNodes[b].getElementsByTagName('a')[0];

				}

			}

			menus[thismenu].children = constructMenus(submenuactivators, false);

			if (showbelow)

			{

				// override top-level onclick function

				menuactivators[a].onclick = function(e) {this.associatedMenu.toggleMenus(e); return false;};

			}

		}

		else

		{

			var thismenu = menus.length;

			menus[thismenu] = new Menu(menuactivators[a], null, showbelow);

		}

	}

	for (var a=0; a < menus.length; a++)

	{

		menus[a].siblings = menus;

	}

	return menus;

}



function toggleDisplay(id)

{

	var elem = document.getElementById(id);

	if (elem && elem.style) {

		if (elem.style.display != 'block') {

			elem.style.display = 'block';

		}

		else {

			elem.style.display = 'none';

		}

	}

	return false;

}

