/*
Sites where this is used:
	acs.ucsd.edu
	podcast.ucsd.edu
	sixth college practicum
	software.ucsd.edu
	*add your sites here, and update all copies, even if you only change the list*
*/
var Tabber = new Class( 
	{
	initialize: function(MenuTabList)
		{
		// Locate the list of tabs
		this.MenuTabs = $(MenuTabList); 

		if(this.MenuTabs == null) return false; 

		// Add the CSS style to it
		this.MenuTabs.addClass("tablist"); 

		// Find all links in tabber
		this.Tabs = this.MenuTabs.getElements("li"); 
		this.TabLinks = this.MenuTabs.getElements("a"); 
		this.AllPageLinks = $$('a[href]'); 

		// If Mootools doesn't work on the user's browser,
		// by this time it would have failed. So, now I choose to 
		// scroll to the top of the page, to fix browsers
		// that choose to jump to the #target immediately on pageload
		
		this.TabLinks.each(function(s)
			{
			var Hash = s.hash.substring(1); 

			// For each tab, add the Click handler 
			s.addEvent('mouseup', handleClickEvent.bind(this)); 
			s.addEvent('keyup', handleClickEvent.bind(this)); 
			s.addEvent('click', handleClickEvent.bind(this)); 

			var initContentDiv = $(Hash /* + "_div"*/); 
			initContentDiv.addClass("tab-panel");
			initContentDiv.id = Hash + "_div"; 

			// Position the <a name=""> targets, so that the browser doesn't jump when you click a tab.
			var contentNames = initContentDiv.getElements('a[name="' + Hash + '"]'); 
			if(contentNames.length == 0)
				{
				var tabTarget = new Element("a", {
					'name': Hash
					}); 
				tabTarget.inject(initContentDiv, "top"); 
				contentNames[0] = tabTarget;
				}

			contentNames.each(function(s)
				{
				s.setStyles({
					'position': 'fixed',
					'top': '0px',
					'left': '0px'
					}); 
				}); 
			
			// Find all links to this tab and add the same event handler
			this.AllPageLinks.each(function(pageLink)
				{
				if(pageLink.hash == s.hash && pageLink != s) 
					{
					pageLink.addEvent('click', handleClickEvent.bind(this)); 
					}
				}.bind(this));

			// Hide the target content
			initContentDiv.setStyle('display', 'none'); 
			}.bind(this)); 

		// Initialize the 'open tab' to be null
		this.OpenTab = null; 

		this.checkCurrentUrl.periodical(100, this);
		}, 

	lastTargetHash: "", 

	checkCurrentUrl: function()
		{
		// If user is returning from a bookmark or link, open
		// the tab referred to by its hash
		var found = 0;
		var targetHash = window.location.hash; 

		if(targetHash != this.lastTargetHash || this.OpenTab == null)
			{
			this.TabLinks.each(function(link, i){
				if (targetHash == link.hash) found = i;
				});
			
			this.openSection(this.TabLinks[found]); 

			this.lastTargetHash = targetHash; 
			}
		},

	// Open the tab whose link was sent in
	openSection: function(clickedLink)
		{
		// Hide whatever tab was previously showing, if any
		if(this.OpenTab != null)
			this.OpenTab.setStyle('display', 'none'); 

		var sourceHash = clickedLink.hash.substring(1); 
		var showDiv = $(sourceHash  + "_div"); 

		showDiv.setStyle('display', 'block'); 

		// The tab is now open
		this.OpenTab = showDiv; 

		// Fix the class on the tabs
		this.Tabs.each(function(s)
			{
			s.removeClass("current"); 
			if(s.getChildren()[0].href == clickedLink)
				s.addClass("current"); 
			}); 

		// fix minor niggling browser detail where tab is focused
		clickedLink.blur();  

		// Attempt to focus on tab content:
		// 		Unneccesary in Firefox
		// 		Necessary in IE
		// 		Doesn't help in Safari
		var linkTarget = $$('a[name="' + sourceHash + '"]')[0];
		if(linkTarget)
			{
			linkTarget.tabIndex = -1; 
			linkTarget.focus(); 
			}
		}
	}); 


// Given a Mouse Event, pass the clicked link to openSection()
function handleClickEvent(e)
	{
	var ClickEvent = new Event(e); 
	var key = ClickEvent.key;

	if(key == null || key == 'enter' || key == 'return')
		this.openSection(ClickEvent.target);
	}


