// This function will add an onload handler to the body.  It's fine to
// add a bunch of handlers, this keeps track of all of them.
function add_body_onload(func) {
  var old_body_onload=window.onload;
  window.onload=function() {
    if (old_body_onload) { old_body_onload(); }
    func();
  }
}

var isIE=(navigator.appName.indexOf('Microsoft')!=-1);
var ie_version=0;
if (isIE) {
	var p=navigator.userAgent.match(/MSIE (\d+\.\d+)/);
	if (p) {
		ie_version=parseFloat(p[1]);
	}
}

// If any submenus are "on", we'll clear that and save it as the default
// tab.  If any have are "hover", it's cleared.  Then this one is set to
// hover.
var all_submenus=[ 'first', 'browse-catalogue', 'my-projects', 'cue-sheet', 'faq', 'search-tips', 'contact', 'last'];
var default_tab_name=null;
function show_menu(submenu_name) {
	if (submenu_name) {
		var top_tab_el=$('top-tabs');
		if (!top_tab_el.className.match(/\bon\b/)) top_tab_el.className += ' on';
	}
	for (var i=0 ; i<all_submenus.length ; i++) {
		var tab_el=$(all_submenus[i]+'-tab');
		var submenu_el=$(all_submenus[i]+'-submenu');
		if (tab_el) {
			// if something is marked "on", unmark it
			if (tab_el.className.match(/\bon\b/)) {
				default_tab_name=all_submenus[i];
				tab_el.className = tab_el.className.replace(/ ?\bon\b/,'');
				if (submenu_el) submenu_el.className = submenu_el.className.replace(/ ?\bon\b/,'');
			}
			// now, mark or unmark each as "hover"
			if (submenu_name && all_submenus[i]==submenu_name) {
				if (!tab_el.className.match(/\bhover\b/)) tab_el.className += ' hover';
				if (submenu_el && !submenu_el.className.match(/\bhover\b/)) submenu_el.className += ' hover';
			} else {
				tab_el.className = tab_el.className.replace(/ ?\bhover\b/,'');
				if (submenu_el) submenu_el.className = submenu_el.className.replace(/ ?\bhover\b/,'');
			}
		}
	}
}

function default_menu() {
	show_menu();
	if (default_tab_name) {
		var tab_el=$(default_tab_name+'-tab');
		var submenu_el=$(default_tab_name+'-submenu');
		if (!tab_el.className.match(/\bon\b/)) tab_el.className += ' on';
		if (!submenu_el.className.match(/\bon\b/)) submenu_el.className += ' on';
	} else {
		var top_tab_el=$('top-tabs');
		if (top_tab_el) top_tab_el.className = top_tab_el.className.replace(/ ?\bon\b/,'');
	}
}
