var timer = 0;
var downmenu = null;
var onsubmenu = false;
function externalLinks() {
	if (!document.getElementsByTagName)
		return;
	var anchors = document.getElementsByTagName("a");
	for ( var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href")
				&& anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

// Loop thru menu items, applying flyover behvaiour to each
function initiateDropDown() {
	var mainmenu = document.getElementById('main_menu').childNodes;
	for ( var i = 0; i < mainmenu.length; i++) {
		if(typeof mainmenu[i].tagName != 'undefined' && mainmenu[i].tagName.toUpperCase() == 'UL')
		{
			menus = mainmenu[i].childNodes;
			for ( var j = 0; j < menus.length; j++) {
				if(typeof menus[j].tagName != 'undefined' && menus[j].tagName.toUpperCase() == 'LI')
				{
					menus[j].onmouseover = function()
					{
						if(downmenu != null) // Menu down already, just switch to this menu.
						{
							document.getElementById(downmenu).className = '';
							this.className = 'flyout';
							downmenu = this.id;
						}
						else // No menu down, start timer before displaying menu
						{
							if(timer) clearTimeout(timer);
							timer = setTimeout("downmenu='"+this.id+"';document.getElementById('"+this.id+"').className='flyout';", 250);
						}
					};
					menus[j].onmouseout = function()
					{
						if(timer) clearTimeout(timer);
						if(!onsubmenu) setTimeout("if(!onsubmenu) {document.getElementById('"+this.id+"').className='';downmenu=null;}", 250);
					};
					
					// Set a variable when we are in/out the submenu to stop the flyout
					// closing when user is in submenu
					sublistitems = menus[j].getElementsByTagName('li');
					for ( var k = 0; k < sublistitems.length; k++) {
						sublistitems[k].onmouseover=function()
						{
							onsubmenu = true;
						}
						sublistitems[k].onmouseout=function()
						{
							onsubmenu = false;
						}
					}
				}
			}
			break;
		}
	}	
}


window.onload = function() {
	externalLinks();
	initiateDropDown();
}
