/*

	Retrieves info from learning zone clips, then shows video in a lightbox.

	Relies on CSS.

	Author: <a href="mailto:Michael.Taylor1@bbc.co.uk">Mike Taylor</a> 2008-11-13
	Bug (no display on second video click) fixed 2008-11-24 MET.

*/

function VideoLightbox(args) {

	var ns_links = [];
	
	this.panel = null; // store the panel
	this.clips_url_base = 'http://www.bbc.co.uk' + '/learningzone/clips';
	this.data_url_base = '/apps/ifl/schools/primaryhistory/showrecord?Id=';
	this.data_url_params = ';OutputFormat=JSON'
// to get output in UTF-8, you can try one of the following:
//			+ ';ContentType=charset%3Dutf-8' // see Tom Broughton!
//			+ ';ContentType=text; charset=utf-8' // see Tom Broughton!
			+ ';ContentType=application/json; charset=utf-8' // see Tom Broughton!
//			+ ';ContentType=application%2Fjson%3B+charset%3Dutf-8' // see Tom Broughton!
//			+ ';SuppressCaching=1'  // only for test version
			;
		
	this.width_add_px = 48; // add this to avoid scrollbars in IE6
	this.panel = null; // Glow panel - only need one of these

	this.dbg = true;
	this.dbg = false;

	this.title_text = args['title'] || '';

	// create arrays to store specs and panel bodies
	ns_links = glow.dom.get(args['video-links']);
	this.n_items = ns_links.length; // total items in lightbox list
	this.a_spec = new Array(this.n_items); // store spec for each lightbox
	this.a_ns_body = new Array(this.n_items); // store each panel body

	if (this.dbg)
		alert("Found " + this.n_items + " videos");

	var that = this;

	this.init(ns_links);
}
//-----

VideoLightbox.prototype.init = function(ns_links) {

// MAINTAINED VARIABLES:

	var that = this;

//	alert('init');
//	get_and_show('2301');

	ns_links.each(function(i) {

		var ns_link = glow.dom.get(this);
		glow.events.addListener(ns_link, 'click', function() { show_this_clip(ns_link); return false; });

	});
	
// INNER FUNCTIONS:

	function show_this_clip(ns_link) {

		var re_clip_id = /^clip-(\d+)$/
			, a_matches = null
			, n_bailout = 5
			, ns_parent = ns_link.parent()
			, clip_id = ''
			, item_id = '' // id on the container containing 'clip-nnn' where 'nnn' is clip id.
			;

		if (that.dbg) alert('About to show clip');

		// dig out the clip id from container
		while (n_bailout-- > 0) {
			if (ns_parent.is('li.promo')) {
				item_id = ns_parent.attr('id');
				break;
			}
			else {
				ns_parent = ns_parent.parent();
			}
		}

		if (!item_id) // couldn't find a clip id amongst containers
			return;

		if (that.dbg) alert('Item id: ' + item_id);

		if (a_matches = item_id.match(re_clip_id))	{

			var clip_id = a_matches[1];

			if (that.dbg) alert('about to show '+ clip_id);

			get_and_show(clip_id);
		}

	}
	//-----

	function get_and_show(clip_id) {

		var data_url = that.data_url_base + clip_id + that.data_url_params;

		if (that.dbg) alert('about to get:\n' + data_url);

		glow.net.get(data_url, {

			onLoad: function(response) {

				if (that.dbg) alert('received response');

				var res_json = response.json();
				var a_file_id_part = res_json.Items[0].item.FILE_IDENTIFIER.split('_');
				var clip_url = that.clips_url_base + '/clips/'
					+ [a_file_id_part[0], a_file_id_part[1]].join('_')
					+ '/bb/' + res_json.Items[0].item.FILE_IDENTIFIER + '_bb.xml'
					;

				if (that.dbg) alert('Clip URL: ' + clip_url);

				var title = that.title_text || ''
/*
					+  res_json.Items[0].id
					+ ' - '
					+ res_json.Items[0].item.SCHOOL_LEVEL_NAME
					+ ' - '
*/
					+ res_json.Items[0].item.SUBJECT_NAME
					+ ' - '
					+ res_json.Items[0].item.TOPIC
//					+ ' (' +  res_json.Items[0].id + ')'
					;

				var subtitle = res_json.Items[0].item.CLIP_NAME;
				var desc = res_json.Items[0].item.CLIP_DESCRIPTION;
				
				show_video(title, subtitle, desc, clip_url, clip_id);

			},
			onError: function(response) {
				alert("Error getting data from: " + data_url + "\n\n" + response.statusText());
			}
		});
}
//-----

	function show_video(title, subtitle, desc, clip_url, clip_id) {

		var ns_panel = [] // content for panel
			, ns_close = [] // text added to close button
			, emp = null  // BBC Emp
			;


//			alert('Title: '+title);

		if (!that.panel) {
			ns_panel = glow.dom.create(
				  '<div id="video-panel">'
					+ '<h2 class="hd">' + title + '</h2>'
					+ '<div id="embed-video"></div>'
					+ '<h3>' + subtitle + '</h3>'
					+ '<p>' + desc + '</p>'
				  + '</div>'
				);

			that.panel = new glow.widgets.Panel(ns_panel, {
				"closeOnMaskClick": false
				, width: (512 + 50) + "px"
			});

			that.panel.container.addClass("ph-panel-container");
			that.panel.content.addClass("ph-panel-content");
			that.panel.body.addClass("ph-panel-body");
			that.panel.header.addClass("ph-panel-header");

			ns_close = glow.dom.create('<span class="close-text">Close</span>');//close light box 
			that.panel.content.get("a.panel-close").addClass("close-btn");
			that.panel.content.get("a.panel-close").after(ns_close);

			glow.dom.get("#embed-video").css("display", "block");
		}

		that.panel.header.get('h2').text(title);
		that.panel.body.get('h3').text(subtitle);
		that.panel.body.get('p').text(desc);
		that.panel.show();

		if (that.dbg) alert('Embedding video: ' + clip_url);

		// link to non-flash version for users without Flash
		that.panel.body.get("#embed-video").html('<p>You are trying to view Flash content, but you have no Flash plugin installed. To find out how to install a Flash plugin, go to the <a href="http://www.bbc.co.uk/webwise/askbruce/articles/download/howdoidownloadflashplayer_1.shtml">WebWise Flash install guide</a>.</p><p>Alternatively, watch a <a href="/apps/ifl/learningzone/clips/showrecord?Id='+ clip_id +'">non-flash version</a> of the video clip on Learning Zone Broadband.</p>');

		// MK: unload the EMP after Panel is closed
		glow.events.addListener(that.panel, "afterHide", 
			function(){
				glow.dom.get("#embed-video").empty();
			}
		);

		emp = new bbc.Emp();
		emp.setDomId("embed-video");
		emp.setWidth("512");
		emp.setHeight("323");
		emp.setPlaylist(clip_url);
		emp.set("config_settings_skin", "silver");
		emp.write();  
		that.panel.setPosition();
		
	}

}
//-----

// end of script