/* =============================================================================
Tabbed Content Type 1
================================================================================
Used to Ajax-up the tabbed content pages
----------------------------------------------------------------------------- */

// Wire up the tabbed content domready event handler
window.addEvent('domready', LoadTabbedContent);

/* Variables
---------------------------------------- */

// Hash table to contain the html for all secondary tabs
var _SecondaryTabsHash = new Hash();

// Boolean to denote whether or not secondary tabs are ajaxed-up
var _SecondaryTabsAjaxDisabled = false;
var _PartiallyDisabledTabsAjax = false; var _PartiallyDisabledTabArray = new Array();

// Boolean to denote whether or not tabs are click disabled
var _IsDisabled = false;

// Html to be used to display the ajax loader graphic
var _AjaxLoaderHtml = '<div style="margin-bottom:20px;"><img src="/App_Themes/HLC000/Images/ajax_loader.gif" width="16" height="16" alt="Loading..." class="ajax_loader" /></div>';

/* Methods
---------------------------------------- */

function ExtendSecondaryTabsHash(secondaryTabs)
{
    try 
    {
	    _SecondaryTabsHash.extend(secondaryTabs); 
	} 
	catch(err)
	{
	}
}

function GetSecondaryTabsAjaxDisabled()
{
	return _SecondaryTabsAjaxDisabled;
}

function GetPartiallyDisabledTabsAjax()
{
	return _PartiallyDisabledTabsAjax;
}

function SetSecondaryTabsAjaxDisabled(value)
{
	_SecondaryTabsAjaxDisabled = value;
}

function SetPartiallyDisabledTabsAjax(value, ids)
{
	_PartiallyDisabledTabsAjax = value;
	//split the ids variable into an array.
	_PartiallyDisabledTabArray = ids.split(',');
}

function LoadPrimaryTabEvents()
{
	var tabAnchors;
	var tabFound;

	// Pull back an array of every anchor tag for each primary tab
	tabAnchors = $('primary_tabs').getElements('a');
	
	// Loop through each anchor tag and wire up their click events
	tabAnchors.each(function(item)
	{
	    //partially disabled tabs check.
	    if (_PartiallyDisabledTabsAjax) 
		{
	        tabFound = false;
	        for(i=0;i<=_PartiallyDisabledTabArray.length-1; i++)
	        {
	            if (item) {
	                if (NodeId(item.get('id')) == _PartiallyDisabledTabArray[i]) { tabFound = true; }
                }
            }
	        if (!tabFound) { item.addEvent('click', PrimaryTabClick); }
	    } else {
	        item.addEvent('click', PrimaryTabClick);
	    }
	});
}

function LoadSecondaryTabEvents()
{
	if (!_SecondaryTabsAjaxDisabled)
	{
		var tabAnchors;
		var tabFound;

		// Pull back an array of every anchor tag for each secondary tab
		tabAnchors = $('secondary_tabs').getElements('a');
		
		// Loop through each anchor tag and wire up their click events
		tabAnchors.each(function(item)
		{
		    //partially disabled tabs check.
		    if (_PartiallyDisabledTabsAjax) 
		    {
	            tabFound = false;
	            for(i=0;i<=_PartiallyDisabledTabArray.length-1; i++)
	            {
	                if (item) {
	                    if (NodeId(item.get('id')) == _PartiallyDisabledTabArray[i]) { tabFound = true; }
                    }
                }
			    if (!tabFound) { item.addEvent('click', SecondaryTabClick); }
			} else {
			    item.addEvent('click', SecondaryTabClick);
			}
		});
	}
}

function LoadSwitchableContent()
{
	var fxSlide;

	// Do a slide in animation for the default content
	fxSlide = new Fx.Slide($('switchable_content'));
	fxSlide.hide();
	fxSlide.slideIn();
}

function LoadAnalyticsEvents()
{
	var tabAnchors;
	
	tabAnchors = $$('#primary_tabs a').extend($$('#secondary_tabs a'));

	// _tag is the WebTrends javascript object
	if (typeof(_tag) != "undefined")
	{		
		tabAnchors.each(function(item)
		{
			item.addEvent('click', dcsAjax);
		});
	}
	
	// _imgtag is the ImageTracking javascript object
	if (typeof(_imgtag) != "undefined")
	{		
		tabAnchors.each(function(item)
		{
			item.addEvent('click', imgAjax);
		});
	}
}

function GetIsDisabled()
{
	return _IsDisabled;
}

function SetIsDisabled(value)
{
	_IsDisabled = value;
}

function SetPageTitle(tab)
{
	var text;
	
	text = tab.getElement('a').get('text');
	$('page_title').set('text', text);
}

function SetActivePrimaryTab(tab)
{
	var currentTab;
	
	currentTab = GetCurrentPrimaryTab();
	currentTab.removeClass('on');
	tab.addClass('on');
}

function SetActiveSecondaryTab(tab)
{
	var currentTab;
	
	currentTab = GetCurrentSecondaryTab();
	currentTab.removeClass('on');
	tab.addClass('on');
}

function GetCurrentPrimaryTab()
{
	var currentTab;
	
	currentTab = $('primary_tabs').getElement('li[class$=on]');
	
	return currentTab;
}

function GetCurrentSecondaryTab()
{
	var currentTab;
	
	currentTab = $('secondary_tabs').getElement('li[class$=on]');
	
	return currentTab;
}

function LoadSecondaryTabs(primaryTab)
{
	var primaryTabID;

	primaryTabID = NodeId(primaryTab.getProperty('id'));

	// Check if the tab has secondary tabs
	if (_SecondaryTabsHash.has(primaryTabID))
	{		
		// Pull back the secondary tabs html for the tab selected		
		$('secondary_tabs').set('html', _SecondaryTabsHash.get(primaryTabID));
		$('secondary_tabs').setStyle('display', 'block');
		
		// A new set of secondary tabs has been loaded, so wire up their events
		LoadSecondaryTabEvents();
	}
	else
	{
		$('secondary_tabs').set('html', '');
		$('secondary_tabs').setStyle('display', 'none');
	}
}

function LoadTabContent(tab)
{
	var fxSlide;

	// Do a slide out followed by a slide in animation
	fxSlide = new Fx.Slide($('switchable_content'));
	fxSlide.slideOut().chain(function()
	{
		ShowAjaxLoader();
		
		fxSlide = new Fx.Slide($('switchable_content'));
		fxSlide.slideIn().chain(function()
		{		
			var tabID;
			var id;
	
			tabID = NodeId(tab.getProperty('id'));
			
			// Check if the tab has secondary tabs, if it does then load 
			// the content for it's first secondary tab
			if (_SecondaryTabsHash.has(tabID))
			{
				id = NodeId(GetCurrentSecondaryTab().getElement('a').getProperty('id'));
			}
			else
			{
				id = NodeId(tab.getElement('a').getProperty('id'));
			}
			
			LoadRequest(id);
		});
	});
}

function LoadRequest(id)
{
	var request;
	var fxSlide;
	
	// Create the request object
	request = new Request.HTML({method:'get', url:'/SiteInformation/Ajax/Content/', evalScripts:true});
	
	// Wire up the event handlers
	//request.onComplete = LoadTabContentSuccess;
	request.onSuccess = LoadTabContentSuccess;
	//request.onException = LoadTabContentFailure;
	request.onFailure = LoadTabContentFailure;
	
	// Send the request
	request.send('action=get_tab_content&id=' + id);
}

function ShowAjaxLoader()
{
	$('switchable_content').set('html', _AjaxLoaderHtml);
}

function SetTabContent(html)
{
	var divText;
	divText = new Element('div', {'class': 'text'});
	divText.adopt(html);
	
	$('switchable_content').set('html', '');
	$('switchable_content').adopt(divText);
	
	//search the div for any popup classes.
	$epopext($('switchable_content'), 0, 0, true);
	
	//social bookmarking.
	//find the current tab.
	var currentTab = GetCurrentSecondaryTab();
	if (!currentTab) { currentTab = GetCurrentPrimaryTab(); }
	
	//find hyperlink within tab.
	var tabHyperLink = currentTab.getElements('a')[0];
	
	var divSocialBoomarks;
	divSocialBoomarks = new Element('div', {'class': 'social_bookmarks'});
	divSocialBoomarks.set('html', SocialBookmarking(tabHyperLink.get('href'), currentTab.get('text'),
	NodeId(tabHyperLink.get('id'))));
	
	$('switchable_content').adopt(divSocialBoomarks);
}

/* Event Handlers
---------------------------------------- */

function LoadTabbedContent()
{
	LoadPrimaryTabEvents();
	LoadSecondaryTabEvents();
	LoadSwitchableContent();
	LoadAnalyticsEvents();
}

function PrimaryTabClick(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);
		SetActivePrimaryTab(tab);
		LoadSecondaryTabs(tab);
		LoadTabContent(tab);
	}
	
	return false;
}

function NodeId(id)
{
    //remove the keyword from the node id. ("node_")
    id = id.replace('node_', '');
    return id;
}

function SecondaryTabClick(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);
		SetActiveSecondaryTab(tab);
		LoadTabContent(tab);
	}
	
	return false;
}

function LoadTabContentSuccess(html)
{
	SetTabContent(html);
	
	// Do a slide in animation
	fxSlide = new Fx.Slide($('switchable_content'));
	fxSlide.slideIn().chain(function(){
		SetIsDisabled(false);
	});
}

function LoadTabContentFailure()
{
	SetTabContent('<p>Sorry, there was an error processing your request. Please try clicking the tab again.</p>');

	// Do a slide in animation
	fxSlide = new Fx.Slide($('switchable_content'));
	fxSlide.slideIn().chain(function(){
		SetIsDisabled(false);
	});
}