/* Detect shit browsers */
var winIE = false;
// improve browser sniffing!
if( document.all && (navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Opera') == -1 ) ){
	winIE = true;

	// write some CSS info into the header
	// do this instead of via DOM to make it
	// get applied before page loads
	document.writeln('<style type="text/css">');
	document.writeln('div#navigation { position: absolute; }');
	document.writeln('</style>');

}


/* Add some behaviours to DHTML browsers */
if( document.getElementById ){
	// browser can do DHTML

	// create function to show or hide an object
	function showHide( object, show ){
		if( show ){
			return function(){
				object.className = 'unhide';
			};
		}
		else{
			return function(){
				object.className = null;
			};
		}
	}


	// find menu items that have submenus and attach behaviour to them
	function doSubmenus( item, addOnMouse ){

		// found a menu?
		var found = false;

		// check item for submenu
		var submenu = item.firstChild;
		while( submenu != null ){
                        if( (submenu.nodeType == 1) && (submenu.nodeName.toLowerCase() == 'ul') ){
				// found a menu
				found = submenu;

				// go through items
				var subItem = submenu.firstChild;
				var lastLink;
				while( subItem != null ){
					if( (subItem.nodeType == 1) && (subItem.nodeName.toLowerCase() == 'li') ){
						// found an item of the submenu
						
						// get the main link of this item
						var link = subItem.firstChild;
						while( (link.nodeType != 1) && (link.nodeName.toLowerCase() == 'a') ){
							link = link.nextSibling;
						}						
						lastLink = link;

						// if subitem has a submenu attach behaviour to it	
						var subsub = doSubmenus( subItem, addOnMouse );
						if( subsub ){

							// fix this?
							link.onfocus = showHide( subsub, true);

							if( addOnMouse ){
								subItem.onmouseover = showHide( subsub, true);
							}
							subItem.onmouseout = showHide( subsub, false );

						}
					}
					subItem = subItem.nextSibling;
				}

				// if the last link in this list loses focus the menu should disappear again
				// ( but don't need to do this when item is a DIV )
				if( item.nodeName.toLowerCase() != "div" ){
					lastLink.onblur = showHide( submenu, false );
				}

				// no need to continue
				break;
			}
			submenu = submenu.nextSibling;
		}
		return found;
	}





	// attach behaviours when page has finished loading
	window.onload = function(){

		// find sub-menus and make them hide - unhide
		var navBar = document.getElementById('navigation');
		doSubmenus( navBar, winIE );

	}
}
