﻿var TIMER_LABEL = 'MessagingPanel_TimerMain';

function messagePanel()
{
	var me = this;
	
	// constant fading durations and timer identification
	var FADE_IN_DURATION = 1000;
	var FADE_OUT_DURATION = 1000
	var CHANGE_DURATION = 11000;
	
	// the current index of the active message
	var currentIndex = 0;

	// the overlay, content and anchors html objects
	var ACTIVE_SELECTOR = '.dockHero .activeDockContent';
	var NOT_ACTIVE_SELECTOR = '.dockHero > div:not(.activeDockContent)';
	var CONTENT_SELECTOR = '.heroBlurb, .heroImage img';
	
	var heroes = null;
	var controls = null;

	// collection of messages
	var messages = new Array();

	// constructs the message panel
	this.init = function(autoTransition)
	{
		heroes = $('.dockContent');
		controls = $('#heroControls a');

		currentIndex = heroes.index($('.activeDockContent')[0]);

		if (!autoTransition) return;
		$(document).everyTime(CHANGE_DURATION, TIMER_LABEL, function()
		{
			me.gotoNext();
		});

		// fade out inactive content, ready for fading in
		getContent($(NOT_ACTIVE_SELECTOR)).css('display', 'none');
	}

	// transitions the message to the next index
	this.gotoNext = function()
	{
		// goes to the 'next' index depending on the current index and the number of items
		this.gotoIndex(currentIndex >= (heroes.length - 1) ? 0 : currentIndex + 1);
	}
	
	// transitions the message to the selected index
	this.gotoIndex = function(index)
	{
		if (index < 0 || index >= heroes.length)
			return;

		currentIndex = index;
		var body = $('body');

		// fade out the content and register the changing callback functions	
		getActiveContent().fadeOut(FADE_OUT_DURATION, function()
		{
			// remove the active and selected css states
			heroes.removeClass('activeDockContent');
			controls.removeClass('selected');

			// update the selected css state
			$(heroes[currentIndex]).addClass('activeDockContent');
			$(controls[currentIndex]).addClass('selected');

			// fade in the new content and update the index
			getActiveContent().fadeIn(FADE_IN_DURATION);
			currentIndex = index;

			// if ie6, refresh the controls
			if (body.hasClass('IE6'))
			{
				controls.css('visibility', 'hidden');
				controls.css('display', 'none').css('display', 'block').css('display', 'inline-block');
				controls.css('visibility', 'visible');
			}
		});
	}

	// selects the active content
	function getActiveContent()
	{
		return getContent($(ACTIVE_SELECTOR));
	}

	// selects the content for the current context
	function getContent(context)
	{
		return $(CONTENT_SELECTOR, context);
	}
}

var Messages;

$(document).ready(function()
{
    Messages = new messagePanel();
    Messages.init(true);
});

function heroSpin(index, obj)
{
    Messages.gotoIndex(index);
    // need to stop it rotating ...
    $(document).stopTime(TIMER_LABEL);
    
    var heroTitle = $('.activeDockContent .heroBlurb .homepagehero').text();


    trackGAEvent('HomePageHero', 'Click ' + eval(eval(index)+1), heroTitle, '');
}
