/* =============================================================================
Tabbed Content Type 2
================================================================================
Used to show and hide tabbed content in sequence without Ajax
----------------------------------------------------------------------------- */

// Wire up the tabbed content domready event handler
window.addEvent('domready', LoadTabbedContent);

/* Variables
---------------------------------------- */

// Hash table to contain the ids for each switchable content element
var _SwitchableContentHash = new Hash();

// Boolean to denote whether or not tabs are click disabled
var _IsDisabled = false;

// The ID for clearing the sequence
var _SequenceID;

// The tab that was previously activated
var _PreviousActiveTab;

// Determine whether to activate the animation sequence.
var _UseAnimation = true;

// Determine whether to activate the page title change.
var _PageTitleChange = true;

/* Methods
---------------------------------------- */

function UseAnimation(choice)
{
    _UseAnimation = choice;
}

function UsePageTitle(choice)
{
    _PageTitleChange = choice;
}

function ExtendSwitchableContentHash(switchableContent)
{
	_SwitchableContentHash.extend(switchableContent);
}

function LoadTabs()
{
	var tab;

	// Get the first tab
	tab = $('primary_tabs').getElement('li[class^=tab1]');
	
	SetActiveTab(tab);
}

function LoadTab(tabno)
{
    // Get the specified tab
	tab = $('primary_tabs').getElement('li[class^=tab' + tabno + ']');
	
	SetActiveTab(tab);
	LoadTabContent(tab);
}

function LoadTabEvents()
{
	var tabAnchors;

	// Pull back an array of every anchor tag for each tab
	tabAnchors = $('primary_tabs').getElements('a');
	
	// Loop through each anchor tag and wire up their click events
	tabAnchors.each(function(item)
	{
		item.setProperty('href', location.href);
		item.addEvent('click', TabClick);
	});
}

function LoadSwitchableContent()
{
	var switchableContent;
	var i = 0;
	var fxSlide;

	// Get the switchable content elements
	switchableContent = $('tab_content').getElements('div[id^=switchable_content]');
	
	// Hide all the switchable content elements apart from the first
	switchableContent.each(function(item)
	{
		if (i > 0)
		{
			//item.setStyle('display', 'none');
			fxSlide = new Fx.Slide(item);
			fxSlide.hide();
		}
		
		i++;
	});
}

function LoadTabNavigation()
{
	var divTabNavigation;
	var aPrevious;
	var aNext;
	
	divTabNavigation = new Element('div', {'id': 'tab_navigation', 'class': 'tab_navigation'});
	aPrevious = new Element('a', {'href': location.href, 'text': 'Previous'});
	aNext = new Element('a', {'href': location.href, 'text': 'Next'});
	
	divTabNavigation.adopt(aPrevious);
	divTabNavigation.appendText(' | ');
	divTabNavigation.adopt(aNext);
	
	$('tab_content').adopt(divTabNavigation);
}

function LoadTabNavigationEvents()
{
	var navigation;
	
	navigation = $('tab_navigation');
	navigation.getElement('a[text=Previous]').addEvent('click', PreviousTabNavigationClick);
	navigation.getElement('a[text=Next]').addEvent('click', NextTabNavigationClick);
}

function LoadSequence()
{
	// Switch tabs every 20 seconds
	_SequenceID = Switch.periodical(20000);
}

function Switch()
{
    if (_UseAnimation) {
	    var nextTab;

	    nextTab = GetNextTab();
	    SetActiveTab(nextTab);
    	
	    // Do a fade animation
	    nextTab.set('opacity', '.5');
	    nextTab.fade('in');
    	
	    SetPageTitle(nextTab);
	    LoadTabContent(nextTab);
    }
}

function GetIsDisabled()
{
	return _IsDisabled;
}

function SetIsDisabled(value)
{
	_IsDisabled = value;
}

function SetPageTitle(tab)
{
	var text;
	
	text = tab.getElement('a').get('text');
	if (_PageTitleChange) { $('page_title').set('text', text); }
}

function SetActiveTab(tab)
{
	var currentTab;
	
	currentTab = GetCurrentTab();
	
	if (currentTab == null)
	{
		currentTab = tab;
	}
	
	currentTab.removeClass('on');
	SetPreviousActiveTab(currentTab);
	tab.addClass('on');
}

function LoadTabContent(tab)
{
	var previousActiveTab;
	var switchableContentIDToHide;
	var switchableContentIDToShow;
	var fxSlide;

	previousActiveTab = GetPreviousActiveTab();
	switchableContentIDToHide = _SwitchableContentHash.get(previousActiveTab.getProperty('id'));
	switchableContentIDToShow = _SwitchableContentHash.get(tab.getProperty('id'));
	
	//$(switchableContentIDToHide).setStyle('display', 'none');
	//$(switchableContentIDToShow).setStyle('display', 'block');
	
	// Do a slide out followed by a slide in animation
	fxSlide = new Fx.Slide($(switchableContentIDToHide));
	fxSlide.slideOut().chain(function(){
		fxSlide = new Fx.Slide($(switchableContentIDToShow));
		fxSlide.slideIn().chain(function(){
			SetIsDisabled(false);
			$(switchableContentIDToShow).setStyle('visibility', 'visible');
		});
	});
	
	$(switchableContentIDToHide).setStyle('visibility', 'hidden');
    //$(switchableContentIDToShow).setStyle('display', 'block');
	/*fxSlide = new Fx.Slide($(switchableContentIDToHide));
	fxSlide.hide();
	
	fxSlide = new Fx.Slide($(switchableContentIDToShow));
	fxSlide.slideIn();*/
}

function GetCurrentTab()
{
	var currentTab;
	
	currentTab = $('primary_tabs').getElement('li[class$=on]');
	
	return currentTab;
}

function GetPreviousTab()
{
	var currentTab;
	var previousTab;
	
	currentTab = GetCurrentTab();
	previousTab = currentTab.getPrevious('li');
	
	// Check if there was a previous tab, otherwise get the last tab
	if (previousTab == null)
	{
		previousTab = $$('#primary_tabs li').getLast();
	}
	
	return previousTab;
}

function GetNextTab()
{
	var currentTab;
	var nextTab;

	currentTab = GetCurrentTab();
	nextTab = currentTab.getNext('li');
	
	// Check if there was a next tab, otherwise get the first tab
	if (nextTab == null)
	{
		nextTab = $('primary_tabs').getElement('ul').getFirst();
	}
	
	return nextTab;
}

function GetPreviousActiveTab()
{
	return _PreviousActiveTab;
}

function SetPreviousActiveTab(tab)
{
	_PreviousActiveTab = tab;
}

function ResetSequence()
{
	// Stop the sequence
	$clear(_SequenceID);
	
	// Restart the sequence
	LoadSequence();
}

/* Event Handlers
---------------------------------------- */

function LoadTabbedContent()
{
	LoadTabs();
	LoadTabEvents();
	LoadSwitchableContent();
	LoadTabNavigation();
	LoadTabNavigationEvents()
	LoadSequence();
}

function TabClick(event)
{
	var target;
	var tab;
	
	target = $(event.target);
	tab = target.getParent('li');
	
	// Remove focus from the clicked tab
	tab.getElement('a').blur();
		
	if (!GetIsDisabled())
	{
		SetIsDisabled(true);
		SetPageTitle(tab);
		SetActiveTab(tab);
		LoadTabContent(tab);
		ResetSequence();
	}
	
	return false;
}

function PreviousTabNavigationClick(event)
{
	var previousTab;
	
	$(event.target).blur();
	previousTab = GetPreviousTab();
	
	if (!GetIsDisabled())
	{
		SetIsDisabled(true);
		SetPageTitle(previousTab);
		SetActiveTab(previousTab);
		LoadTabContent(previousTab);
		ResetSequence();
	}
	
	return false;
}

function NextTabNavigationClick(event)
{
	if (!GetIsDisabled())
	{
		var nextTab;
		
		SetIsDisabled(true);
		
		$(event.target).blur();
		nextTab = GetNextTab();
		
		SetPageTitle(nextTab);
		SetActiveTab(nextTab);
		LoadTabContent(nextTab);
		ResetSequence();
	}
	
	return false;	
}