﻿function homeMessage(_heroUrl, _contentLinkUrl, _videoUrl)
{
	this.heroUrl = _heroUrl;
	this.contentLinkUrl = _contentLinkUrl;
	this.videoUrl = _videoUrl;

	this.getHtml = function()
	{
		html = "";
		
		if (this.heroUrl.indexOf('.swf') > -1)
		{
			return this.getFlashObject();
		}

		if (this.videoUrl != '')
		{
			html += '<a href="' + this.videoUrl + '" class="playVideo video-overlay">';
			html += '<img src="' + this.heroUrl + '" class="hero" />';
			html += '</a>';
		}
		else
		{
			html += '<a href="' + this.contentLinkUrl + '" class="viewContent">';
			html += '<img src="' + this.heroUrl + '" class="hero" />';
			html += '</a>';
		}

		return html;
	}

	this.getFlashObject = function()
	{
		var params = {
			allowFullScreen: "true",
			allowscriptaccess: "always",
			wmode: "transparent"
		}

		var attributes = {
			allowfullscreen : "true",
			allowscriptaccess : "always",
			wmode : "transparent"
		}

		swfobject.embedSWF(this.heroUrl, 'currentMessageContent', '680', '264', '9.0.0', this.heroUrl, null, params, attributes);
		return html;
	}
}

function messagePanel()
{
	var me = this;
	
	// constant fading durations and timer identification
	var FADE_IN_DURATION = 800;
	var FADE_OUT_DURATION = 800
	var CHANGE_DURATION = 10000;
	var TIMER_LABEL = 'MessagingPanel_TimerMain';
	
	// the current index of the active message
	var currentIndex = 0;

	// the overlay, content and anchors html objects
	var overlay = null;
	var content = null;
	var anchors = null;

	// collection of messages
	var messages = new Array();

	// constructs the message panel
	this.init = function(autoTransition)
	{
		var body = $('body');
		if (body.hasClass('IE6'))
		{
			return;
		}

		overlay = $('#currentMessage #messageOverlay');
		content = $('#currentMessage #currentMessageContent');
		anchors = $('#messagePanel .messageNavigation ul a');

		currentIndex = anchors.index($('#messagePanel .messageNavigation ul a.active')[0]);

		registerNavigationLinks();

		if (!autoTransition) return;
		$(document).everyTime(CHANGE_DURATION, TIMER_LABEL, function()
		{
			me.gotoNext();
		});
	}

	// adds a message to the list of
	this.addMessage = function(heroUrl, contentLinkUrl, videoUrl)
	{
		messages.push(new homeMessage(heroUrl, contentLinkUrl, videoUrl));
	}

	// registers the navigation anchor links and links them to their relavent message
	function registerNavigationLinks()
	{
		anchors.click(function()
		{
			$(document).stopTime(TIMER_LABEL);

			var fixedIndex = anchors.index(this);
			
			anchors.removeClass('active');
			$(anchors[fixedIndex]).addClass('active');
			
			me.gotoIndex(fixedIndex);
			return false;
		});
	}

	// 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 >= (messages.length - 1) ? 0 : currentIndex + 1);
	}
	
	// transitions the message to the selected index
	this.gotoIndex = function(index)
	{
		if (index < 0 || index >= messages.length)
			return;

		currentIndex = index;

		// fade in the overlay and register the changing callback functions
		overlay.fadeIn(FADE_IN_DURATION, function()
		{
			anchors.removeClass('active');
			updateContent();
			$(anchors[currentIndex]).addClass('active');
			
			fadeOutOverlay();
			currentIndex = index;
		});
	}

	// fades out the overlay after updating the content
	function fadeOutOverlay()
	{
		overlay.fadeOut(FADE_OUT_DURATION);
	}

	// updates the content of the current message
	function updateContent()
	{
		content.children().remove();
		content.append(messages[currentIndex].getHtml());

		$('.video-overlay', content).colorbox();
	}
}
