/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  tac_videoPlayer.js
 * ##### VERSION: v1.0 (Eclipse Group)
/* ########################################################################### *

/* ########################################################################### *
/* ***** INDEX *************************************************************** *
/* ########################################################################### *
/* ##### Global JS configuration
/* ##### Initialisation
/* ##### Article Slider
/* ##### Media player
/* ##### Jquery & other utilities
/* ########################################################################### */

/* ########################################################################### *
/* ##### GLOBAL JS CONFIGURATION
/* ########################################################################### */
var ecl_flashVersion = "9.0.28.0";
var ecl_flashMediaPlayerSWF = "/static/flash/videoPlayer/flash/mediaPlayer.swf";
var ecl_flashExpressInstallSWF = "/static/flash/videoPlayer/flash/expressInstall.swf";
var ecl_videoPlayerWidth = "320px";
var ecl_videoPlayerHeight = "267px";
var ecl_defaultMediaImage = "";
var ecl_popupWidth = "750";
var ecl_popupHeight = "700";
var ECL_DEBUG = false;
var NO_FLASH_HTML = '<p><strong>Sorry, you require the <a href="http://www.adobe.com/go/getflash">Adobe Flash</a> player to view this content.</strong></p>';

/* ########################################################################### *
/* ##### INITIALISATION
/* ########################################################################### */

$(document).ready(ecl_init_global);

function ecl_init_global()
{
	// tac_manageVideoPlayer();	// No longer required
	tac_init_mediaPlayer();
	ecl_init_mediaPlayer();
	ecl_executeLoadEvents();
}


function tac_init_mediaPlayer()
{
	// Replace links with inline media player
	var count = 0;
	var pauseOnLoad = 1;
	
	$(".tac_mediaPlayer").each(function() 
		{		
			count++;
			
			var mediaTitle = $(this).attr("alt");
			mediaTitle = (mediaTitle == undefined ? "" : mediaTitle);	// Catch if no ALT is set.
			
			var mediaFile = $(this).attr("href");
			if (ECL_DEBUG) { if (mediaFile == undefined) alert("Cannot find media file name") };
			
			var imageFile = $(this).find("img").attr("src");
			if (ECL_DEBUG) { if (imageFile == undefined) alert("Cannot find image file name") };
			
			var containerId = "tac_mediaPlayer_" + count;
			$(this).replaceWith('<div id="' + containerId + '" class="tac_mediaPlayer">' + NO_FLASH_HTML + '</div>');
			
			// Insert video player(s)
			showVideo(mediaFile, imageFile, pauseOnLoad, containerId, ECL_DEBUG);
		});
}


/* ########################################################################### *
/* ##### Media Player
/* ########################################################################### */
var ecl_mediaPlayer_activeButton;
var int_mediaAutoPlay;

// Trigger the first item in the .mediaLinks
$(window).load(function() 
	{
		$(".mediaLinks a:first").trigger("click");
		ecl_mediaPlayer_autoPlay();
		ecl_mediaPlayer_mediaLinks();
	});

// Setup the mediaLinks slider
function ecl_mediaPlayer_mediaLinks()
{
	if ($(".mediaLinks").length > 0)
	{
		$(".mediaLinks a").click(function() 
			{
				ecl_mediaPlayer_mediaLinks_updateDescription(this)

			});
	}
}

function ecl_mediaPlayer_mediaLinks_updateDescription(linkNode)
{
	// Fade the description
	$(".mediaDescription .description").fadeOut(function()
		{ 
			// Set the description text
			$(".mediaDescription .description:first").html($(linkNode).find(".hidden").html());
			
			// Show the description
			$(".mediaDescription .description").fadeIn();
		});	
}

// Setup auto play
function ecl_mediaPlayer_autoPlay()
{
	var AUTO_PLAY_INTERVAL = 5000;
	
	if ($(".mediaPlayer .mediaLinks_autoPlay").length > 0)
	{
		int_mediaAutoPlay = setInterval(ecl_mediaPlayer_autoPlayNext, AUTO_PLAY_INTERVAL);
		
		// Clear intervals
		$("#flashMediaContainer").click(ecl_mediaPlayer_stopAutoPlay);  // On click of #flashMediaContainer
		$(".mediaLinks a").click(ecl_mediaPlayer_stopAutoPlay);			// On click of .mediaLinks a
	}
}

function ecl_mediaPlayer_autoPlayNext()
{
	var currentNode = $(".mediaLinks a.active:first");
	//$(currentNode).next("a").trigger("click");
	
	var nextNode = $(currentNode).next("a");
	
	if ($(nextNode).html() == null)
	{
		nextNode = $(".mediaLinks a:first");
	}
		
	ecl_launchMedia(nextNode);
	ecl_mediaPlayer_mediaLinks_updateDescription(nextNode);
}

// This function stops the auto-rotate / auo-play functionality
// Note this function is called from within the Flash for IE6. Consequently,
// if you change the name of the fucntion here you will need to modify the Flash file
// and recompile.
function ecl_mediaPlayer_stopAutoPlay()
{
	clearInterval(int_mediaAutoPlay);
}

function ecl_init_mediaPlayer()
{
	// Wire up the links
	$(".ecl_showMediaLink").click(function() 
		{
			ecl_launchMedia(this);
			return false;
		});
}

function ecl_launchMedia(node)
{
	// Grab the query string values from the <a> tag
	var queryVars = $.query($(node).attr("href"));
	
	// Assign variables / flash params
	var mediaFile = queryVars["ecl_mediaFile"];
	var imageFile = queryVars["ecl_stillImage"];
	var pauseOnLoad = queryVars["ecl_mediaPauseOnLoad"];
	var ecl_flashContainerId = queryVars["ecl_flashContainer"];
	var debug = queryVars["ecl_debug"];
	var mediaTitle = queryVars["mediaTitle"];
	
	if (imageFile == undefined)
	{
		imageFile = ecl_defaultMediaImage;
	}
	
	// Determine if the video should play in a modal window or in page
	if (ecl_flashContainerId == "modalWindow")
	{
		// Display media in modal window
		ecl_launchModalWindow(unescape(mediaTitle), "329px", function()
		  { 
				// Set the width and height of video - by setting the #contentContainer
				$("#contentContainer").width(ecl_videoPlayerWidth);
				$("#contentContainer").height(ecl_videoPlayerHeight);
				
				// Show the media in page
				showVideo(mediaFile, imageFile, pauseOnLoad, "contentContainer", debug);
		  });
	} 
	else 
	{	
		// Show the media in page
		showVideo(mediaFile, imageFile, pauseOnLoad, ecl_flashContainerId, debug);
	}
	
	// Set this as the active item
	$(ecl_mediaPlayer_activeButton).removeClass("active");	// Unset the previous button
	ecl_mediaPlayer_activeButton = node;					// Update object to new button
	$(node).addClass("active");								// Set the new button to active
	
	return false;	
}

function showVideo (mediaFile, imageFile, pauseOnLoad, ecl_flashContainerId, debug) 
{
	//if (!mediaFile) return true;
	//if (!imageFile) return true;
	
	// Get width and height of container element
	var ecl_flashWidth = $("#" + ecl_flashContainerId).width();
	var ecl_flashHeight = $("#" + ecl_flashContainerId).height();	

	$("#" + ecl_flashContainerId).hide();

	var flashvars = {};
	flashvars.displayWidth = ecl_flashWidth;
	flashvars.displayHeight = ecl_flashHeight;
	flashvars.pauseOnLoad = pauseOnLoad;
	flashvars.mediaFile = mediaFile;
	flashvars.imageFile = imageFile;
	flashvars.debug = debug;

	var params = {};
	params.allowFullScreen = "true";
	params.allowScriptAccess = "always";
	params.menu = "false";
	params.salign = "LT";
	params.scale = "noscale";
	params.wmode = "transparent";	
	
	var attributes = {};
	attributes.id = ecl_flashContainerId;
	swfobject.embedSWF(ecl_flashMediaPlayerSWF, ecl_flashContainerId, flashvars.displayWidth, flashvars.displayHeight, ecl_flashVersion, ecl_flashExpressInstallSWF, flashvars, params, attributes);
			
	$("#" + ecl_flashContainerId).show();

	// Prevent click action
	return false;
}




/* ########################################################################### *
/* ##### LOAD EVENTS
/* ########################################################################### */

function ecl_executeLoadEvents()
{
	$(".ecl_clickOnLoad").trigger("click");
}



/* ########################################################################### *
/* ##### JQUERY & OTHER UTILITIES
/* ########################################################################### */

// Supply a query string and parse it into a key value pairs

jQuery.query = function(s) 
{
		var r = {};
		if (s) {
				var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
				q = q.replace(/\&$/, ''); // remove the trailing &
				jQuery.each(q.split('&'), function() {
						var splitted = this.split('=');
						var key = splitted[0];
						var val = splitted[1];
						// convert numbers
						if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
						// convert booleans
						if (val == 'true') val = true;
						if (val == 'false') val = false;
						// ignore empty values
						if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
				});
		}
		return r;
};

/* FIN */
