var tabs = [];
var tab_to = null;
var tab_showing = false;
function registerTab(linkid, contentid)
{
        tabs.push({isOpen: false, link: $(linkid), content: $(contentid)});
        var tabid = tabs.length - 1;
        
	//Mouse-Over  Tab Event
        $(linkid).observe('mouseover', function(e) { doTab(tabid); e.stopPropagation(); });
        
	//Mouse-Over Content Event
	$(contentid).observe('mouseover', 
		function(e) {
			//Reset the timeout if the user moves their mouse back into the Content area, preventing the change from occuring
			if (tab_to != null) {
				clearTimeout(tab_to);
				tab_to = null;
			}
			e.stopPropagation(); //Prevent propagation to the Body event.
		});
        //Mouse-Over Anything But the Tab/Content Event
	Event.observe(document.body, 'mouseover', 
                function(e) {
			if (tab_showing) {
				doTab(-1); //Clear tabs
			} else {
				//Reset the timeout to prevent a tab that was rolled over briefly from showing
				if (tab_to != null) {
                                	clearTimeout(tab_to);
                                	tab_to = null;
                        	}
			}
                });
}

function showTab(tabid)
{
	var new_tab_showing = false;
	for(i = 0; i < tabs.length; i++)
        {
                if (i != tabid)
		{
			if (tabs[i].isOpen) {
				tabs[i].isOpen = false;
				tabs[i].link.removeClassName('current'); //Stop highlighting this tab
				tabs[i].content.style.display = 'none';
			}
                }
		else
		{
			new_tab_showing = true;
			if (!tabs[i].isOpen) {
				tabs[i].isOpen = true;
				tabs[i].link.addClassName('current'); //Highlight the current tab
				tabs[i].content.style.display = 'block';
			}
		}
        }
	tab_showing = new_tab_showing;

}

function doTab(tabid)
{
        if (tab_to != null) {
                clearTimeout(tab_to);
                tab_to = null;
        }
        tab_to = setTimeout(function() { tab_to = null; showTab(tabid) }, 200);
        
}

