﻿/// <reference path="jquery-1.2.6-vsdoc.js" />
$(document).ready(function()
{
	if ($('.videoContainer, .audioContainer, .imageGalleryContainer').length > 0)
	{
		//$.getScript('/content/js/swfobject.js', function()
		//{
		function createPlayer(segmentId, playerDivId, playerPath)
		{
			var configData = getMediaConfigurationItem(segmentId);
			var attributes =
				{
					id: 'player_' + segmentId,
					name: 'player_' + segmentId
				};

			swfobject.embedSWF(playerPath, playerDivId, configData.height, configData.width, "9.0.0", false, configData.flashvars, configData.params, attributes);
		}

		$('.videoContainer, .audioContainer').each(function()
		{
			var playerDiv = this;
			var segmentId = playerDiv.id.slice(playerDiv.id.lastIndexOf('_') + 1);
			createPlayer(segmentId, playerDiv.id, '/content/widgets/player.swf');

			//Set up Playlist
			var configData = getMediaConfigurationItem(segmentId);
			if (configData.playListInfo != '' && configData.playListInfo != 'internal')
			{
				$('#playListCnt_' + segmentId).load('/Feeds/SegmentDataRss/viewashtml.castle?SegmentId=' + segmentId + '&playListInfo=' + configData.playListInfo, function()
				{
					function videoClick(i)
					{
						var player = $('#player_' + segmentId)[0];
						player.sendEvent('STOP');
						player.sendEvent("ITEM", i);
						return false;
					};

					$('#playListCnt_' + segmentId + ' a.videoPlaylist').each(function(i)
					{
						$(this).click(function()
						{
							return videoClick(i);
						}); 
					});

					$('#playListCnt_' + segmentId + ' a.videoPlaylistImage').each(function(i)
					{
						$(this).click(function()
						{
							return videoClick(i); 
						}); 
					});
				});
			}
		});

		$('.imageGalleryContainer').each(function()
		{
			var playerDiv = this;
			var segmentId = playerDiv.id.slice(playerDiv.id.lastIndexOf('_') + 1);
			createPlayer(segmentId, playerDiv.id, '/content/widgets/imagerotator.swf');
		});
		//});
	}
});

function playerEventListener(obj)
{
	$('.videoPlayer, .audioPlayer').each(function()
	{
		//Test each to see if "this" is the player that sent the event
		if ($(this).find('#' + obj.id).length == 0)
		{
			//if it is a different player then tell it to stop
			var playerId = this.id.replace('segment', 'player_');
			console.log('stop ' + playerId);
			$('#' + playerId)[0].sendEvent('STOP');
		}
	});
}

function playerReady(obj)
{
	var player = document.getElementById(obj['id']);
	player.addViewListener("PLAY", "playerEventListener");
	player.addViewListener("LOAD", "playerEventListener");
	player.addViewListener("ITEM", "playerEventListener");
}

//Adds the configuration data for a Audio or Video player to the page's configData object. This is called by the segment and will set up the players
function addToConfigurationData(segmentId, autoStart, height, width, playlistStyle, backcolor, frontcolor, lightcolor, screencolor)
{
	setupMediaConfigNS();
	var configData = createConfigurationData(segmentId, autoStart, height, width, playlistStyle, backcolor, frontcolor, lightcolor, screencolor);
	configData.flashvars.repeat = 'always';
	window.webframe.mediaConfig[segmentId + ""] = configData;
}

//Adds the configuration data for an image display player to the page's configData object. This is called by the segment and will set up the players
function addImageDisplayToConfigurationData(segmentId, autoStart, height, width, transition, rotatetime, shuffle, shownav)
{
	setupMediaConfigNS();
	var configData = createConfigurationData(segmentId, autoStart, height, width, null, '', '', '', '');
	configData.flashvars.transition = transition;
	configData.flashvars.rotatetime = rotatetime;
	configData.flashvars.shuffle = shuffle;
	configData.flashvars.shownavigation = shownav;
	configData.flashvars.repeat = 'always';
	window.webframe.mediaConfig[segmentId + ""] = configData;
}

//Sets up namespace for the configData object
function setupMediaConfigNS()
{
	if (window.webframe == null || typeof (window.webframe) != "object") { window.webframe = new Object(); }
	if (window.webframe.mediaConfig == null) { window.webframe.mediaConfig = new Array(); }
}

//retrieves a configData item
function getMediaConfigurationItem(segmentId)
{ return window.webframe.mediaConfig[segmentId + ""]; }

function createConfigurationData(segmentId, autoStart, height, width, playListInfo, backcolor, frontcolor, lightcolor, screencolor)
{
	var flashvars =
	{
		file: '/Feeds/SegmentDataRss/view.castle?SegmentId=' + segmentId,
		autostart: autoStart,
		repeat: 'list'
	};

	if (playListInfo == 'internal')
	{
		flashvars.playlistsize = height;
		flashvars.playlist = 'right';
		flashvars.backcolor = backcolor || '666666';
		flashvars.frontcolor = frontcolor || 'FFFFFF';
		flashvars.lightcolor = lightcolor || '990000';
		flashvars.screencolor = screencolor || 'E5E5E5';
	}

	var params =
	{
		allowfullscreen: 'true',
		allowscriptaccess: 'always',
		wmode: 'transparent'
	};

	var retVar =
	{
		'flashvars': flashvars,
		'params': params,
		'height': height,
		'width': width,
		'playListInfo': playListInfo
	};

	return retVar;
}

