/*

 * Copyright Blue Billywig 2009

 * 

 * ALL RIGHTS RESERVED

 * 

 * Author(s): Janroel Koppen <J.Koppen@bluebillywig.com>

 *

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE

 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION

 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION

 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */



// requires jQuery (tested with version 1.3.2)

// You should make sure to include it *before* this file



// You should substitute your own publication name for 'demo' directly below

var bb_host = 'cobouw.bbvms.com';



// You do not need to change anything below this line

// --------------------------------------------------



function bb_mediaclip_playClip(xmlUrl, player_id) {	

	$('#'+player_id).each(function() {

		this.playClip(xmlUrl);

	});

}



function bb_mediaclip_draw(bbid) {

	var host = (typeof bb_host != 'undefined' && bb_host.length) ? bb_host : 'vms.bluebillywig.com'; // default



	// try to identify our player via its embed code (script tag)

	var player_id = null;

	var parent = $('#'+bbid).parent().get(0);

	$(parent).find('script').each(function() {

		var re = /\/p\/[^\/]*\/[clq]\/[^\/]*.js/; // Blue Billywig playout url convention

		var match = re.exec(this.src);

		if (match != null) {

			match = match.toString();

			match = match.replace('/p/', 'p-');

			match = match.replace('/c/', '-');

			match = match.replace('/l/', '-');

			match = match.replace('/q/', '-');

			match = match.replace('.js', '');

			player_id = 'bb-' + match; 

			return false; // break from each

		}

	});

	

	var id = bbid.replace('bb', '');

	var url = 'http://' + host + '/mediaclip/'+encodeURIComponent(id)+'.json?callback=?';

	try {

		$.getJSON(url, function(data){

			if (data.status == 'Error') { $('<cite>Error: ' + data.message + '</cite>').attr('class', 'bb bb_mediaclip').appendTo('#'+bbid); } else {

				var $list = $('<ul/>').attr('class', 'bb bb_mediaclip');

				var clip = data.payload;

				if (clip.__name == 'media-clip') {

					var description = null;

					var author = null;

					var copyright = null;

					var deeplink = null;

					var categories = null;

					var thumbnail = null;

					for (var j in clip.__content) {

						var child = clip.__content[j];

						// find text node fields

						if (child.__name == 'description') {

							description = (typeof child.__content != 'undefined') ? child.__content[0] : '';

						} else if (child.__name == 'author') {

							author = (typeof child.__content != 'undefined') ? child.__content[0] : '';

						} else if (child.__name == 'copyright') {

							copyright = (typeof child.__content != 'undefined') ? child.__content[0] : '';

						} else 

						// find deeplink

						if(child.__name == 'deeplink') {

							deeplink = child.url;

						}

						// find categories

						if(child.__name == 'categorization') {

							var cat_array = new Array();

							for (var k in child.__content) {

								if(child.__content[k].__name == 'category-tree') {

									for (var m in child.__content[k].__content) {

										if(child.__content[k].__content[m].__name == 'category') {

											cat_array[cat_array.length] = child.__content[k].__content[m].name; // NB not __name!

										}

									}

								}

							}

							categories = cat_array.join(', ');

						}

						// find thumbnail

						if(child.__name == 'thumbnails') {

							for (var k in child.__content) {

								if (child.__content[k].main == 'true') thumbnail = child.__content[k];

							}

							if (thumbnail == null) thumbnail = child.__content[0];

						}

					}



					// display

					$('<h3>'+clip.title+'</h3>').attr('class', 'bb bb_mediaclip').appendTo('#'+bbid);

					$('<li>description: '+description+'</li>').attr('class', 'bb bb_mediaclip').appendTo($list);

					$('<li>author: '+author+'</li>').attr('class', 'bb bb_mediaclip').appendTo($list);

					$('<li>copyright: '+copyright+'</li>').attr('class', 'bb bb_mediaclip').appendTo($list);

					$('<li>categories: '+categories+'</li>').attr('class', 'bb bb_mediaclip').appendTo($list);

					var onclick = (player_id != null) ? 'bb_mediaclip_playClip(\''+clip.url+'\', \''+player_id+'\')' : 'location.href=\''+deeplink+'\''; 

					if (thumbnail != null) {

						$('<img onclick="'+onclick+'">').attr('src', 'http://' + host + '/image/128/72' + thumbnail.src).attr('alt', clip.title).attr('class', 'bb bb_mediacliplist').appendTo('#'+bbid);

					}

				}

				$list.appendTo('#'+bbid);

			}

		});



	} catch(err) { $('<cite>Error: json request failed.</cite>').attr('class', 'bb bb_mediaclip').appendTo('#'+bbid); }	

}





$(document).ready(function(){

	$('div.bb_mediaclip').each(function() {

		bb_mediaclip_draw(this.id);

	});

});




