/*

 * 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_mediacliplist_playClip(xmlUrl, player_id) {	

	$('#'+player_id).each(function() {

		this.playClip(xmlUrl);

	});



	return false;

}



function bb_mediacliplist_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);

	$('script').each(function() { // was: $(parent).find

		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 + '/mediacliplist/'+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_mediacliplist');

				var payload = data.payload;

				for (var i in payload.__content) {

					var clip = payload.__content[i];

					if (clip.__name == 'media-clip') {

						var thumbnail = null;

						var deeplink = null;

						for (var j in clip.__content) {

							var child = clip.__content[j];

							// find thumbnail

							if(child.__name == 'thumbnails') {

								for (var k in child.__content) {

									var thumb = child.__content[k];

									if (thumb.main == 'true') thumbnail = thumb;

								}

								if (thumbnail == null) thumbnail = child.__content[0];

							}

							// find deeplink

							if(child.__name == 'deeplink') {

								deeplink = child.url;

							}

						}

						var onclick = (player_id != null) ? 'bb_mediacliplist_playClip(\''+clip.url+'\', \''+player_id+'\')' : 'location.href=\''+deeplink+'\''; 

						var $item = $('<li>').attr('class', 'bb bb_mediacliplist');

						var $anchor = $('<a href="'+deeplink+'" onclick="return '+onclick+'">')

						if (thumbnail != null) {

							var src = 'http://' + host + '/image/128/72' + thumbnail.src;

							$('<img/>').attr('src', src).attr('alt', clip.title).attr('class', 'bb bb_mediacliplist').appendTo($anchor);

						} else {

							$('<p>' + clip.title + '</p>').attr('class', 'bb bb_mediacliplist').appendTo($anchor);							

						}

						$anchor.appendTo($item);

						$item.appendTo($list);

					}

				}

				$list.appendTo('#'+bbid);

                                $list.attr('class', 'jcarousel-skin-tango').jcarousel();

			}

		});



	} catch(err) { $('<cite>Error: json request failed.</cite>').attr('class', 'bb bb_mediaclip').appendTo('#'+bbid); }	

}





$(document).ready(function(){

//$.readyList.unshift(function() {

	$('div.bb_mediacliplist').each(function() {

		bb_mediacliplist_draw(this.id);

	});

});




