/*
JQuery Video Library by gabssnake

Ispired and based in:
- jQuery youtube playlist plugin
	@ http://www.geckonewmedia.com/blog/2009/8/14/jquery-youtube-playlist-plugin---youtubeplaylist
- Ceebox (Videobox)
	@ http://catcubed.com/2008/12/23/ceebox-a-thickboxvideobox-mashup/
*/


jQuery.fn.videolibrary = function(options) {
 
  // default settings
  var options = jQuery.extend( {holderId:'video-holder', videoWidth:'520', videoHeight:'350'},options);
 
  return this.each(function() {

		//given a url, returns the html for the specific video, if not supported redirects to url
		function compileVideoObject(videoAnchorUrl,videoTitle,videoDate,videoDescription) {
		
			// support for video sites
			var videoSite = {
				youtube: {
					type : "youtube",
					siteRgx : /youtube\.com\/watch/i,
					idRgx : /(?:v=)([a-zA-Z0-9_\-]+)/i,
					srcUrl : "http://www.youtube.com/v/",
					srcUrl2 : "&fs=1"
				},
				google: {
					type : "google",
					siteRgx : /google\.com\/videoplay/i,
					idRgx : /(?:id=)([a-zA-Z0-9_\-]+)/i,
					srcUrl : "http://video.google.com/googleplayer.swf?docId=",
					srcUrl2 : "&fs=true"
				},
				vimeo: {
					type : "vimeo",
					siteRgx : /vimeo\.com\/[0-9]+/i,
					idRgx : /(?:\.com\/)([a-zA-Z0-9_]+)/i,
					srcUrl : "http://www.vimeo.com/moogaloop.swf?clip_id=",
					srcUrl2 : "&amp;fullscreen=1"
				}
			}
		
			//some global vars
			var compiledUrl = "";
			var supportedVideo = false;
			
			//identify video
			$.each(videoSite,function(i){
				if(videoAnchorUrl.match(videoSite[i].siteRgx,"")) {
					videoId = videoAnchorUrl.match(videoSite[i].idRgx,"");
					compiledUrl = videoSite[i].srcUrl + videoId[1] + videoSite[i].srcUrl2;
					supportedVideo = true;
				}
			});
			
			//embed video or redirect if not supported
			if(supportedVideo==false) {
				//unable to load video, redirect user
				document.location=videoAnchorUrl;
			} else {
				//mix compiledUrl,videoTitle,videoDate,videoDescription
				//return html
				var html = '<h2>'+videoTitle+'</h2> ';
				html += '<p class="date">'+videoDate+'</p> ';
				html += '<object type="application/x-shockwave-flash" data="'+compiledUrl+'" ';
				html += 'width="'+options.videoWidth+'" height="'+options.videoHeight+'" id="VideoPlayback"> ';
				html += '<param name="movie" value="'+compiledUrl+'" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /></object> ';
				html += '<p class="description">'+videoDescription+'</p> ';
				return html;
			}
		}

		//given an anchor, extract objects to update: video, title, description, etc
		function changeVideo(targetAnchor) {
			var videoURL = targetAnchor.attr("href");
			var videoTXT = targetAnchor.text();
			var videoDAT = targetAnchor.parent("dt").parent("li").find("dd.date").html();
			var videoDES = targetAnchor.parent("dt").parent("li").find("dd.description").text();
			
			//render appropiate video
			$("#"+options.holderId+"").html(compileVideoObject(videoURL,videoTXT,videoDAT,videoDES));
			
			//adjust classes, and toggle details
			targetAnchor.parent("dt").parent("li").parent("ul").find("li.currentvideo").children("dd").toggle();
			targetAnchor.parent("dt").parent("li").parent("ul").find("li.currentvideo").removeClass("currentvideo");
			targetAnchor.parent("dt").parent("li").addClass("currentvideo");
			targetAnchor.parent("dt").parent("li").children("dd").toggle();
		}

		//initiate
		
		//get all videos into array
		var videosArray = new Array();
		$(this).children("li").each(function() { videosArray.push($(this)); });
		//if no hash, load first
		var initVideo = 1;
		if (!document.location.hash.length == "0") {
			var crudeString = document.location.hash.substring(1,document.location.hash.length);
			initVideo = Number(crudeString);
			if(initVideo > videosArray.length || initVideo==0) {
				alert("Not a valid video ID");
				initVideo = 1;
			}
		}
		//do the actual loading
		changeVideo(videosArray[initVideo-1].children("dt").children("a"));

		//click behaviour
		$(this).children("li").children("dt").children("a").click(function() {
			changeVideo($(this));
			return false;
		});

  });
};





// initiate videolibrary
$(function() {
	$("ul.videolist-1").videolibrary();
	$('ul.videolist-1 li dd').toggle();
});

