/**
 * playlist.js is a javascript playlist for the admissions site that dynamically positions itself below the mediaspace id
 * 
 * @author Nathan White 
 * @requires SWFObject must have been loaded, and the player ID registered
 * @todo lets get our player id dynamically at load so changes to the admissions_video module do not require changes here.
 */
var player = null;
var player_current_item;
var player_currently_showing;
var player_tray;
var player_show_current_only;
var player_use_custom_scrollbar;

function playerReady(thePlayer)
{
	var js_src = $('script[src*=playlist.js]:first').attr("src");
	var search_string = 'show_current_only=true';
	player_show_current_only = (js_src.indexOf('show_current_only=true') != -1) ? true : false;
	player_use_custom_scrollbar = (js_src.indexOf('use_custom_scrollbar=false') != -1) ? false : true; // default is true
	player = swfobject.getObjectById('ply'); // lets not hard code this. 
	addListeners();
}

function addListeners()
{
	if (player)
	{
		//if (!player_show_current_only) player.addControllerListener("PLAYLIST", "createPlaylist");
		player.addControllerListener("ITEM", "itemMonitor");
		player.addModelListener("STATE", "stateListener");
		if (!player_show_current_only) createPlaylist();
	}
	else 
	{
		setTimeout(addListeners, 100);
	}
}

function stateListener(obj)
{
	if ( obj.newstate = "PLAYING" )
	{
		if (player_show_current_only) showCurrentItem();
		else focusPlaylist();
		return false;
	}
}

function itemMonitor(obj)
{
	player_current_item = obj.index;
}

function focusPlaylist()
{
	if (player_current_item != player_currently_showing) // minimizes flakiness and extra events being PLAYING event overfires
	{
		var playlist = $("ul.playlist", player_tray);
		var current_item = playlist.children().eq(player_current_item);
		current_item.removeClass('onDeck').addClass('currentlyPlaying').siblings(".currentlyPlaying").removeClass('currentlyPlaying').addClass('onDeck');
		if (player_use_custom_scrollbar) player_tray[0].scrollTo("#movie"+player_current_item);
		player_currently_showing = player_current_item;
	}
	return false;
}

function showCurrentItem()
{
	if (player_current_item != player_currently_showing) // minimizes flakiness and extra events being PLAYING event overfires
	{
		
		var my_playlist = $("ul.playlist");
		var my_tray = $("div.tray");
		
		var createTray = function()
		{
			var tray_html = $('<div class="tray_wrap"><div class="tray"></div></div>');
			$("div#mediaspace").after(tray_html);
		}
		
		var createPlaylist = function()
		{	
			var playlist = player.getPlaylist();
			var current_item = playlist[player_current_item];
			var playlist_html = $('<ul class="playlist"></ul>');
			var mylink = '<strong>'+current_item.title+'</strong>';
			var item = $('<li id="movie'+player_current_item+'" class="currentlyPlaying">'+mylink+current_item.description+'</li>');
			$(playlist_html).append(item);
			$("div.tray").append(playlist_html);
			playlist_html.hide().slideDown("slow", setWrapHeight);
			//$(playlist_html).slideDown("slow", setWrapHeight);
			player_currently_showing = player_current_item;
		}
		
		var setWrapHeight = function()
		{
			var tray_height = $("div.tray").height();
			$("div.tray_wrap").height(tray_height);
		}
		
		var removePlaylist = function() { $("ul.playlist").remove(); }
		var removeAndCreatePlaylist = function() { removePlaylist(); createPlaylist(); }
		if (my_tray.length < 1) createTray();
		if (my_playlist.length > 0)
		{
			my_playlist.slideUp("slow", removeAndCreatePlaylist);
		}
		else createPlaylist();
	}
	return false;
}

function createPlaylist(obj)
{
	var playlist = player.getPlaylist();
	if (playlist) {
		var item_html = new Array();
		var div_html = $('<div class="tray"></div>');
		var playlist_html = $('<ul class="playlist"></ul>');
		var odd_even = 'odd';
		var currently_playing = 'onDeck';
		$(playlist).each(function(index) {
			var mylink = '<a href="#" class="title" title="Play"><strong>'+playlist[index].title+'</strong></a>';
			var item = $('<li id="movie'+index+'" class="'+currently_playing+' '+odd_even+'">'+mylink+playlist[index].description+'</li>');
			$("a.title", item).click(function play_item() {
				player.sendEvent("item", index);
				return false;
			});
			$(playlist_html).append(item);
			odd_even = (odd_even == 'odd') ? 'even' : 'odd';
			//currently_playing = 'onDeck';
		});
		var final_html = $(div_html).append(playlist_html);
		$("div#mediaspace").after(final_html);
		if (player_use_custom_scrollbar) setupScrollBars();
		else // we wrap it anyway in the jScrollPaneContainer because the CSS for the playlist looks for the wrapper
		{
			player_tray = $("div.tray").wrap('<div class="jScrollPaneContainer"></div>');
		}
	}
	else
	{
		setTimeout(createPlaylist, 100);
	}
}

function setupScrollBars()
{
	player_tray = $("div.tray");
	player_tray.css({ 'max-height': "none", "height": "7em" });
	$("ul.playlist", player_tray).children("li:last").css({ 'margin-bottom': "2em" });
	player_tray.jScrollPane({ scrollbarMargin: 0, animateTo: true });
}