//-------------------------------------------------
//		Based on youtube playlist jquery plugin
//		Created by dan@geckonm.com
//		www.geckonewmedia.com
//		Modified by Chris Reese
//
//		v1.1 - updated to allow fullscreen 
//			 - thanks Ashraf for the request
//-------------------------------------------------

jQuery.fn.ytplaylist = function(options) {
 
  // default settings
  var ytoptions = jQuery.extend( 
  {
    holderId: 'ytvideo',
	playerHeight: '180',
	playerWidth: '275',
	addThumbs: false,
	thumbSize: 'small',
	showInline: false,
	autoPlay: true,
	showRelated: false,
	allowFullScreen: true
  },options);
  
  var player = "vimeo";
  
  
 
  return this.each(function() {
		
				
		var selector = $(this);
		
		var autoPlay = "";
		var showRelated = "&rel=0";
		var fullScreen = "";
		if(ytoptions.autoPlay) autoPlay = "&autoplay=1"; 
		if(ytoptions.showRelated) showRelated = "&rel=1"; 
		if(ytoptions.allowFullScreen) fullScreen = "&fs=1"; 
		
		
		//throw a youtube player in
		function play(id)
		{
			
			if (player == "yt")
			{
			   var html  = '';
		
			   html += '<object height="'+ytoptions.playerHeight+'" width="'+ytoptions.playerWidth+'">';
			   html += '<param name="movie" value="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
			   html += '<param name="wmode" value="transparent"> </param>';
			   if(ytoptions.allowFullScreen) { 
					html += '<param name="allowfullscreen" value="true"> </param>'; 
			   }
			   html += '<embed src="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"';
			   if(ytoptions.allowFullScreen) { 
					html += ' allowfullscreen="true" '; 
				}
			   html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+ytoptions.playerHeight+'" width="'+ytoptions.playerWidth+'"></embed>';
			   html += '</object>';
				
			  
			}
			else
			{
				var html  = '';
				
				html += '<object height="'+ytoptions.playerHeight+'" width="'+ytoptions.playerWidth+'">';
				if(ytoptions.allowFullScreen) 
				{ 
					html += '<param name="allowfullscreen" value="true" />';
				}
				html += '<param name="allowscriptaccess" value="always" />';
				html += '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + id + '&amp;server=vimeo.com&amp;color=00adef';
				if (ytoptions.allowFullScreen)
				{
					html += '&amp;fullscreen=1" />';
				}
				else
				{
					html += '" />';	
				}
				
				html += '<embed src="http://vimeo.com/moogaloop.swf?clip_id=' + id + '&amp;server=vimeo.com&amp;color=00adef"';
				if (ytoptions.allowFullScreen)
				{
					html += '&amp;fullscreen=1"';
				}
				
				html += ' type="application/x-shockwave-flash"';
				if (ytoptions.allowFullScreen)
				{
					html += 'allowfullscreen="true"';
				}
					
				
				html += ' allowscriptaccess="always" width="'+ytoptions.playerWidth+'" height="'+ytoptions.playerHeight+'"></embed>';
				html += '</object>';
				
				
				
			  
			}
			return html;
		   
		};
		
		
		//grab a youtube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
		function youtubeid(url) {
			var ytid = url.match("[\\?&]v=([^&#]*)");
			ytid = ytid[1];
			
			return ytid;
		};
		
		
		function getVimeoId(url)
		{
			var vArray = url.split("/");
			var vid = vArray[vArray.length - 1];
			
			return vid;	
		}
		
		
		function getPlayerType(evtObj)
		{
			list = evtObj.attr('class').split(" ");
			player = list[0];
			
			return player;
		}
		
		//load inital video
		var firstVid = selector.children("li:first-child").addClass("currentvideo").attr("id").split("_")[1];
		
		$("#"+ytoptions.holderId+"").html(play(firstVid));
		
		
		
		//load video on request
		
		selector.children("li").bind('click', function(evt) {
			
			evt.preventDefault();
			// Set the type
			getPlayerType($(this));
			
			var $id = $(this).attr("id").split("_")[1];
			
			
			// Build the player
			$("#"+ytoptions.holderId+"").html(play($id));
			/*
			if(ytoptions.showInline) {
				$("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo").html(play($(this).parent("li").attr("id")));
			}
			else {
				$("#"+ytoptions.holderId+"").html(play($(this).parent("li").attr("id")));
				$(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo");
			}
			*/
			
			
			
			
		});
		
		//do we want thumns with that?
		if(ytoptions.addThumbs) {
			
			selector.children().each(function(i){
											  
				var replacedText = $(this).text();
				
				if(ytoptions.thumbSize == 'small') {
					var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/2.jpg";
				}
				else {
					var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/0.jpg";
				}
				
				
				$(this).children("a").empty().html("<img src='"+thumbUrl+"' alt='"+replacedText+"' />"+replacedText).attr("title", replacedText);
				
			});	
			
		}
			
		
   
  });
 
};
