// LoadVideo
// CWS - Peter Collinson 2010
;(function ($) {
$.fn.loadvideo = function(options) {
	var defaults = { 'id' : '0', 'display' : 'image' };
	var jsloaded = false;
	var cssloaded = new Array();
	var jsloaded = new Array();
	var jsinstalled = new Array();
	return this.each(function() {
		var localoptions = $.metadata ? $.extend({}, defaults, $.metadata.get(this)) : defaults;
		switch(localoptions.display) {
		case 'image':
		case 'thumb':
		case 'large':
			break;
		default:
			localoptions.display = 'image';
		}
		process(this, localoptions.id, localoptions.display);
	});
	// processing
	function process(elem, id, type) {
		// get the xml information for the video
		$.get("/video.php?action="+type+"xml&amp;id="+id,
			function(data, status) {
				if (status != "success" || data.length == 0) {
					return;
				}
				// if we have no video file - then just display the icon
				var havevideo = ($(data).find("vlfile").text() != "");
				if (havevideo) {
					// otherwise show the fb link
					// step 1 - get the CSS in
					$(data).find("styles").each(function() {
						$(this).find("url").each(function(){
								loadcss($(this).text());
							});
					});
					// step 2 - get the Javascript in
					$(data).find("scripts").each(function() {
						$(this).find("url").each(function(){
								loadjs($(this).text());
							});
					});
					// step 3 insert the html to display the icon
					var classname = "playerinfo" + id;
					var vlwidth = $(data).find("vlwidth").text() - 0;
					var vlheight = $(data).find("vlheight").text() - 0;					
					var filename = $(data).find("filename").text();					
					var width = $(data).find("width").text() - 0;
					var height = $(data).find("height").text() - 0;
					var title =  $(data).find("vltitle").text();					
					var html = '<a href="/video.php?action=display&amp;id='+id+'" class="'+classname+' iframe" title="'+title+'"><img src="'+filename+'" border="0" width="'+width+'" height="'+height+'" alt="'+title+'"></a>';
					$(elem).append(html);
					// now start fancybox
					var sel = "."+classname;
					$(sel).fancybox({
							frameWidth: vlwidth,
							frameHeight: vlheight
							});
				} else {
					var filename = $(data).find("filename").text();					
					var width = $(data).find("width").text();
					var height = $(data).find("height").text();					
					var title =  $(data).find("title").text();					
					var html = '<img src="'+filename+'" border="0" width="'+width+'" height="'+height+'" alt="'+vltitle+'">';
					$(elem).append(html);
				}
		      }
		// end of .get
		, "xml");
	}
	// load css
	function loadcss(url) {
		// only want to do this once
		for (var i = 0; i < cssloaded.length; i++) {
			if (cssloaded[i] == url) {
				return;
			}
		}
		cssloaded[cssloaded.length] = url;
		$("head").append("<link>");
		var css = $("head").children(":last");
		css.attr({
			rel: "stylesheet",
   			type: "text/css",
			href: url
 		});
	}
	// load css
	function loadjs(url) {
		// only want to do this once
		for (var i = 0; i < jsloaded.length; i++) {
			if (jsloaded[i] == url) {
				return;
			}
		}
		jsloaded[jsloaded.length] = url;
		$.ajaxSetup({
		        cache: true, 
			async: false
			});
		$.getScript(url);
	}
}
})(jQuery);

