var oBrowserInfo = new BrowserInfo();
var oMenuBar = new MenuBar();
var oMenuTree = new MenuTree();

function BrowserInfo()
{
    this.agt=navigator.userAgent.toLowerCase();
    this.is_major = parseInt(navigator.appVersion);
    this.is_minor = parseFloat(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    this.is_nav  = ((this.agt.indexOf('mozilla')!=-1) && (this.agt.indexOf('spoofer')==-1)
		&& (this.agt.indexOf('compatible') == -1) && (this.agt.indexOf('opera')==-1)
		&& (this.agt.indexOf('webtv')==-1) && (this.agt.indexOf('hotjava')==-1));
    this.is_nav2 = (this.is_nav && (this.is_major == 2));
    this.is_nav3 = (this.is_nav && (this.is_major == 3));
    this.is_nav4 = (this.is_nav && (this.is_major == 4));
    this.is_nav4up = (this.is_nav && (this.is_major >= 4));
    this.is_navonly = (this.is_nav && ((this.agt.indexOf(";nav") != -1) ||
		(this.agt.indexOf("; nav") != -1)) );
    this.is_nav6 = (this.is_nav && (this.is_major == 5));
    this.is_nav6up = (this.is_nav && (this.is_major >= 5));
    this.is_gecko = (this.agt.indexOf('gecko') != -1);

	// MS IE
    this.is_ie     = ((this.agt.indexOf("msie") != -1) && (this.agt.indexOf("opera") == -1));
    this.is_ie3    = (this.is_ie && (this.is_major < 4));
    this.is_ie4    = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 4")!=-1) );
    this.is_ie4up  = (this.is_ie && (this.is_major >= 4));
    this.is_ie5    = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 5.0")!=-1) );
    this.is_ie5_5  = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 5.5") !=-1));
    this.is_ie5up  = (this.is_ie && !this.is_ie3 && !this.is_ie4);
    this.is_ie5_5up =(this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5);
    this.is_ie6    = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 6.")!=-1) );
    this.is_ie6up  = (this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5 && !this.is_ie5_5);
}

function MenuBar()
{
	// submenu items
	this.aMenuLib = new Array(
		null,
		null,
		"Lasers,Optics,Optomechanics",
		"Material Processing,Stereolithography,Holography,Biotechnology,Scientific Research,Avionics & Defense,FCD Lasers",
		null);
	this.activeMenu = null;

	// show the submenu items
	this.showMenu = function(oMenu, iLevel)
	{
		if (oMenu != this.activeMenu)
		{
			this.hideMenu(this.activeMenu);
			this.activeMenu = oMenu;
		}
		oMenu.className = "mouseover";
		if (oMenu.items)
		{
			var iLeft = 0;
			var oTag = oMenu;
			while (oTag && oTag.tagName != "BODY")
			{
				iLeft += oTag.offsetLeft;
				oTag = oTag.offsetParent;
			}
			oMenu.items.style.left = iLeft;
			oMenu.items.style.display = "";
		}
		else
		{
			var iMenuIdx = Number(oMenu.getAttribute("index"));
			if (this.aMenuLib[iMenuIdx] != null)
			{
				// create menu item container
				var oDiv = document.body.appendChild(document.createElement("DIV"));
				oDiv.style.position = "absolute";
				oDiv.style.backgroundColor = "#C9E0FF";
				oDiv.style.width = 120;
				
				// get offset left and top
				var iLeft = 0;
				var iTop = 0;
				var oTag = oMenu;
				while (oTag && oTag.tagName != "BODY")
				{
					iLeft += oTag.offsetLeft;
					iTop += oTag.offsetTop;
					oTag = oTag.offsetParent;
				}
				
				oDiv.style.top = iTop + oMenu.offsetHeight;
				oDiv.style.left = iLeft;
				var oTbl = oDiv.appendChild(document.createElement("TABLE"));
				oTbl.cellPadding = "3px";
				oTbl.cellSpacing = "1px";
				oTbl.className = "framebox";
				oTbl.width = "100%";
				var oTbody = oTbl.appendChild(document.createElement("TBODY"));
				
				// add menu items into the container
				var aItems = this.aMenuLib[iMenuIdx].split(',');
				for (var i=0; i<aItems.length; i++)
				{
					var oTr = oTbody.appendChild(document.createElement("TR"));
					oTr.className = "submenu";
					oTd = oTr.appendChild(document.createElement("TD"));
					oTd.onclick = function() {this.className = ""; oMenuBar.click(this)};
					oTd.onmouseover = function() { this.className = "mouseover"; }
					oTd.onmouseout = function() { this.className = ""; }
					oTd.setAttribute("index", i);
					oTd.parent = oMenu;
					oTd.noWrap = true;
					oTd.appendChild(document.createTextNode(aItems[i]));
				}
				oMenu.items = oDiv;
			}
		}
		document.onclick = function() { oMenuBar.hideMenu(); }
	}
	
	// hide the submenu items
	this.hideMenu = function(oMenu)
	{
		var oMenu = oMenu || this.activeMenu;
		if (oMenu)
		{
			oMenu.className = "inactive";
			if (oMenu.items)
				oMenu.items.style.display = "none";
		}
	}
	
	// find the corresponding menu tree item and click it
	this.click = function(oMenu)
	{
		var oNode = document.getElementById("menutreediv").firstChild;
		oNode = getNoneTextNode(oNode);
		var oParent = oMenu.parent;
		if (oParent)
		{
			oNode = this.doClick(oParent, oNode);
			if (!getNoneTextNode(oNode.firstChild).opened)
				oMenuTree.click(getNoneTextNode(oNode.firstChild));
			oNode = getNoneTextNode(oNode.lastChild.firstChild);
			this.hideMenu(oParent);
		}
		else
		{
			if (oMenu.items)
				oMenu.items.style.display = "none";
		}
		this.doClick(oMenu, oNode);
	}
	
	// click the node
	this.doClick = function(oMenu, oNode)
	{
		for (var i=0; i<oMenu.getAttribute("index"); i++)
			oNode = getNoneTextNode(oNode.nextSibling);
		oMenuTree.click(getNoneTextNode(oNode.firstChild));
		return oNode;
	}
}

function MenuTree()
{
	this.activeItems = new Array();
	
	// take actions when the folder clicked
	this.click = function(oFolder)
	{
		var oParent = oFolder.parentNode.parentNode.parentNode;
		var oParentImg = oParent.tagName == "DIV" ?
			getNoneTextNode(oParent.firstChild) : null;
		this.clean(oFolder, oParentImg);
		this.openClose(oFolder);
	}

	// clean previous active items
	this.clean = function(oFolder, oParentImg)
	{
		var oActiveImg = this.activeItems.pop()
		while (oActiveImg)
		{
			if (oActiveImg == oFolder)
				return;

			if (oActiveImg == oParentImg)
			{
				this.activeItems.push(oParentImg);
				break;
			}
			else
			{
				this.openClose(oActiveImg);
				oActiveImg = this.activeItems.pop();
			}
		}
		this.activeItems.push(oFolder);
	}

	// open close folders
	this.openClose = function(oImg)
	{
		var srcImg = oImg.src;
		var bOpened = oImg.opened;
		var oDiv = oImg.parentNode.lastChild;
		if (oDiv.tagName == "DIV")
			oDiv.style.display = bOpened ? "none" : "";
		oImg.src = oImg.getAttribute("altimg");
		oImg.setAttribute("altimg", srcImg);
		oImg.opened = !bOpened;
		if (!bOpened)
		{
			if (oImg.getAttribute("target"))
				window.open(oImg.getAttribute("url"));
			else
			{
				if (oBrowserInfo.is_ie5)
					submitframe.navigate(oImg.getAttribute("url"));
				else
				{
					var oIframe = document.getElementById("submitframe").contentWindow;
					oIframe.document.location = oImg.getAttribute("url");
				}
			}
		}
	}

	// find the corresponding menu tree item and click it
	this.link = function(sPath)
	{
		// clean all the active items
		var oActiveImg = this.activeItems.pop()
		while (oActiveImg)
		{
			this.openClose(oActiveImg);
			oActiveImg = this.activeItems.pop();
		}

		// activate the tree nodes specified in the path		
		var aPath = sPath.split(',');
		var oNode = document.getElementById("menutreediv").firstChild;
		oNode = getNoneTextNode(oNode);
		for (var i=0; i<aPath.length; i++)
		{
			for (var j=0; j<Number(aPath[i]); j++)
				oNode = getNoneTextNode(oNode.nextSibling);
			this.click(getNoneTextNode(oNode.firstChild));
			if (oNode.lastChild.tagName == "DIV")
				oNode = getNoneTextNode(oNode.lastChild.firstChild);
			else
				break;
		}
	}
}
function getNoneTextNode(oNode)
{
	while (oNode && oNode.nodeType == 3) // skip those blank TextNode
		oNode = oNode.nextSibling;
	return oNode;
}
function submitFrameInit()
{
	if (oBrowserInfo.is_ie5)
		parent.populate();
}
function populate()
{
	if (oBrowserInfo.is_ie5)
		contentdiv.innerHTML = submitframe.document.body.innerHTML;
	else
	{
		var oIframe = document.getElementById("submitframe").contentWindow;
		var oContentDiv = document.getElementById("contentdiv");
		oContentDiv.innerHTML = oIframe.document.body.innerHTML;
	}
}

Array.prototype.push = function(obj)
{
	if (Array.push)
		this.push(obj);
	else
		this[this.length] = obj;
}
Array.prototype.pop = function()
{
	var obj = null;
	if (Array.pop)
		obj = this.pop();
	else if (this.length > 0)
	{
		obj = this[this.length - 1];
		this.length = this.length - 1;
	}
	return obj;
}

