<!--
/* ------------------------------------------------------------------------ */
/*	Videoset by Abrar Khan July 2009

	This is version 2 of the Videoset. 
	
	This version initialises three versions 
	of the videset, Carousel, Thumbnails and List. Clicking on Thumbnails and 
	List will also update the relvant captions. This information for the caption 
	comes from the emp playlist xml and the title and summary is used.

	This version captures playlist information by having a "playlist" query string 
	attached to the url. It also captures the veideoset_associated player as many 
	emp's can be on the page, this is also done by capturing a query string called 
	"emp1". Would like this changed to "videoset_emp".

	The next version will not require this string to be passed through, but would 
	rather have each list item element mapped to a js file containg a json object 
	that will hold information for each li xml playlist.
   */
/* ------------------------------------------------------------------------ */
var glow;
var dom;
var $;
var events;
var anim;
var VideoSet = null;

var playlistFullCleanURL = "";
var playlistRelativeCleanURL = "";
var videoset_player = "";
var Videoset_settings_object = null;
var clicked_object = null;

gloader.load(["glow", "1", "glow.dom", "glow.anim", "glow.net", "glow.events"], {
	async: true,
	onLoad: function(glowLocal)
	{
		glowLocal.ready(function()
		{
			glow = glowLocal;
			dom = glowLocal.dom;
			$ = glowLocal.dom.get;
			events = glowLocal.events;
			anim = glowLocal.anim;
			
//			VideoSet.Initialise();
		});
	}
});

/* ------------------------------------------------------------------------ */
/* --------->>>   G L O B A L   V I D E O S E T   O B J E C T   <<<-------- */
/* ------------------------------------------------------------------------ */

VideoSet =
{
/* ------------------------------------------------------------------------ */
/* ------------->>>  I N I T I A L I S E  V I D E O S E T  <<<------------- */
/* ------------------------------------------------------------------------ */
	Initialise: function()
	{
		VideoSet.CarouselVideoset.Init();
		VideoSet.ThumbnailsVideoset.Init();
		VideoSet.ListVideoset.Init();

		VideoSet.LoadLinksInfoObject();

//		VideoSet.ReloadEmp("http://www.bbc.co.uk/tinthepark/2009/media/ting_tings08.xml", "2", "false");
	},

/* ------------------------------------------------------------------------ */
/* ----------------->>>  C A R O U S E L   V I D E O S E T   <<<----------- */
/* ------------------------------------------------------------------------ */
	CarouselVideoset: 
	{
/* ------------------------------------------------------------------------ */
/* Function:VideoSet.CarouselVideoset.Init(); */
/* ------------------------------------------------------------------------ */
		Init: function()
		{
			carousel_videoset_found = $("#carousel_videoset").length;
			debug(carousel_videoset_found)
			if(carousel_videoset_found != 0)
			{
				events.addListener($("#carousel_videoset .details"),"click",function(e)
				{
					e.preventDefault();
					$("#carousel_videoset li").removeClass("selected");
					clicked_thumb = this.parentNode;
					$(clicked_thumb).addClass("selected");

					//now that we have the settings object we need to determine which li item has been clicked.
					var clicked_item_number = parseInt(clicked_thumb.id)-1;
					var xml_playlist = Videoset_settings_object.Thumbnails[clicked_item_number].playlist;
					var videoset_player = Videoset_settings_object.Thumbnails[clicked_item_number].player;
					
					VideoSet.ReloadEmp(xml_playlist, videoset_player);
				});
			}
		}
	},
/* ------------------------------------------------------------------------ */
/* -------------->>>  T H U M B N A I L S   V I D E O S E T   <<<---------- */
/* ------------------------------------------------------------------------ */
	ThumbnailsVideoset: 
	{
/* ------------------------------------------------------------------------ */
/* Function:VideoSet.ThumbnailsVideoset.Init(); */
/* ------------------------------------------------------------------------ */
		Init: function()
		{
			thumbnails_videoset_found = $("#thumbnails_videoset").length;
			debug(thumbnails_videoset_found)

			if(thumbnails_videoset_found != 0)
			{

				glow.events.addListener($("#thumbnails_videoset .thumbnails li"),"mouseover",function()
				{
					$(this).addClass("show");
					return false;
				});
				glow.events.addListener($("#thumbnails_videoset .thumbnails li"),"mouseout",function()
				{
					$(this).removeClass("show");
					return false;
				});

				events.addListener($("#thumbnails_videoset .details"),"click",function(e)
				{
					e.preventDefault();

					$("#thumbnails_videoset li").removeClass("selected");
					clicked_thumb = this.parentNode;
					$(clicked_thumb).addClass("selected");

					//now that we have the settings object we need to determine which li item has been clicked.
					var clicked_item_number = parseInt(clicked_thumb.id)-1;
					var xml_playlist = Videoset_settings_object.Thumbnails[clicked_item_number].playlist;
					var videoset_player = Videoset_settings_object.Thumbnails[clicked_item_number].player;
					
					VideoSet.ReloadEmp(xml_playlist, videoset_player);
					VideoSet.UpdateCaption(xml_playlist, "thumbnails");
					
				});
			}
		}
	},
/* ------------------------------------------------------------------------ */
/* -------------------->>>  L I S T   V I D E O S E T  <<<----------------- */
/* ------------------------------------------------------------------------ */
	ListVideoset: 
	{
/* ------------------------------------------------------------------------ */
/* Function:VideoSet.ListVideoset.Init(); */
/* ------------------------------------------------------------------------ */
		Init: function()
		{
			list_videoset_found = $("#list_videoset").length;
			debug(list_videoset_found)
			if(carousel_videoset_found != 0)
			{
				glow.events.addListener('#list_videoset li a', 'click', function(e) 
				{ 
					e.preventDefault();	
					
					$("#list_videoset li a").removeClass("selected");
					$(this).addClass("selected");

					//now that we have the settings object we need to determine which li item has been clicked.
					var clicked_item_number = parseInt(this.parentNode.id)-1;
					var xml_playlist = Videoset_settings_object.Thumbnails[clicked_item_number].playlist;
					var videoset_player = Videoset_settings_object.Thumbnails[clicked_item_number].player;

					VideoSet.ReloadEmp(xml_playlist, videoset_player);
					VideoSet.UpdateCaption(xml_playlist, "list");
				});
			}
		}
	},
/* ------------------------------------------------------------------------ */
/* Function:VideoSet.LoadLinksInfoObject(); */
/* ------------------------------------------------------------------------ */
	LoadLinksInfoObject: function()
	{
		var this_url = window.location.href.toString();

		var last_slash_pos = this_url.lastIndexOf("/");
		var settings_directory = this_url.slice(0, last_slash_pos+1);


		if(typeof(local_settings_directory) != "undefined")
		{
			settings_directory = local_settings_directory;
		}

		//Load the videoset xml info
		var videoset_info = settings_directory+"videoset_settings.js";

		//Need to pass in the relevant js file this should live in the same directory as the index page.
		glow.net.loadScript(videoset_info, 
		{	onLoad:
			function(response)  
			{ 
				alert("done");
			}
		});
	},
/* ------------------------------------------------------------------------ */
/* Function:VideoSet.InitLinksInfoObject() */
/* ------------------------------------------------------------------------ */
	InitLinksInfoObject:function(videoset_settings)
	{	
		//ok we will take the settings from the js file if no settings have been defined in the html ehader.
		if(typeof(local_videoset_settings) != "undefined")
		{
			//settings have been defined in the html so lets set them to be the global settings.
			Videoset_settings_object = local_videoset_settings;
		}
		else
		{
			//lets assign this object to the global one so we can access this information from wherever required
			Videoset_settings_object = videoset_settings;
		}
	},
/* ------------------------------------------------------------------------ */
/* Function:VideoSet.UpdateCaption(); */
/* ------------------------------------------------------------------------ */
	UpdateCaption: function(xml_playlist, item_type)
	{
		var playlist_relative_url = xml_playlist;

		var empURLStartsWith = playlist_relative_url.slice(0, 4);
		if (empURLStartsWith == "http")
		{
			var tempServerNameFinishPos = PlaylistServer.length;
			playlist_relative_url = xml_playlist.slice(tempServerNameFinishPos);
		}

		glow.net.get(playlist_relative_url, 
		{	onLoad: 
			function(response)  
			{   				
				var response_text = response.text();	
				var title		= glow.dom.get(response.xml()).get("title").text();
				var summary		= glow.dom.get(response.xml()).get("summary").text();

				var tempVideoset_type = "";

				switch(item_type)
				{
					case 'thumbnails':
						tempVideoset_type = "thumbnails_videoset";
						break;
					
					case 'list':
						tempVideoset_type = "list_videoset";
						break;
					default:
						break;
				}

				$("."+tempVideoset_type+" ."+item_type+"_heading").empty();
				$("."+tempVideoset_type+" p.summary").empty();

				$("."+tempVideoset_type+" ."+item_type+"_heading").append(title);
				$("."+tempVideoset_type+" p.summary").append(summary);
			},  
			onError: 
			function() 
			{  
				alert("failed");
			}
		});
	},
/* ------------------------------------------------------------------------ */
/* Function:VideoSet.ReloadEmp() */
/* ------------------------------------------------------------------------ */
	ReloadEmp:function (xml_playlist, player_id, auto_start)
	{
		if(auto_start == undefined)
		{
			auto_start = "true";
		}

		var emp = new embeddedMedia.Player();
		emp.setWidth("512");
		emp.setHeight("323");
		emp.setDomId(player_id);
		emp.setPlaylist(xml_playlist);
		emp.set("config_settings_autoPlay", auto_start);
		emp.set("config_settings_skin", "black");
		emp.write(); 
	}
}

function debug(myMessage)
{
//	console.log(myMessage);
}
//-->

