/*
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.getElementsBySelector("li"); 
		this.TabLinks = this.MenuTabs.getElementsBySelector("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.scroller = new Fx.Scroll(window, {});
		// this.scroller.scrollTo(0,0);

		this.TabLinks.each(function(s)
			{
			var Hash = s.hash.substring(1); 

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

			var initContentDiv = $(Hash + "_div"); 

			// Position the <a name=""> targets, so that the browser doesn't jump when you click a tab.
			// Sometimes I'm smarter than I give myself credit for: fixed 0,0 is always in the window, so it will never jump anywhere
			var contentNames = initContentDiv.getElementsBySelector('a[name="' + Hash + '"]'); 
			contentNames.each(function(s)
				{
				s.setStyles({
					'position': 'fixed',
					'top': '0',
					'left': '0'
					}); 
				}); 
			
			// 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.bindAsEventListener(this)); 
					}
				}.bind(this));

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

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

		// 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; 

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

	// 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'); 

		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();  
		var linkTarget = $$('a[name="' + sourceHash + '"]')[0];
		linkTarget.tabIndex = -1; 
		linkTarget.focus(); 
		
		// Set the page location, for bookmarking
		// window.location.hash = sourceHash; 

		// Well what do you know -- if I don't stop the event, I don't need to set the hash. 
		// This also resolves the Safari loading bug. 

		this.OpenTab = showDiv; 
		//return false; 
		}
	}); 


// 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);
		e.preventDefault();
		}
	}



