//-------------------------------------------------
//		youtube playlist jquery plugin
//		Created by dan@geckonm.com
//		www.geckonewmedia.com
//
//		v1.1 - updated to allow fullscreen
//			 - thanks Ashraf for the request
//
//		Big props to Dan for creating this!!!
//		Updated by the Ackernaut to avoid <embed> if desireable for valid XHTML
//		Updated by the Ackernaut to avoid problems using the apostrophe within attributes
//		See: http://www.ackernaut.com/code/tune-in-to-youtube-with-jquery/
//
//		v2.0 - Amended by Jonnya (LHM Media)
//		Now includes controls to fade aditional divs of information
//		Build specifically for WordPress widget integration
//		
//		v2.1 - Amended by Jonnya (LHM Media)
//		Code in 'showinfo' parameter to hide title
//
//-------------------------------------------------

jQuery.fn.ytplaylist = function(options) {

  // default settings
  var options = jQuery.extend( {
    holderId: 'ytvideo',
	playerHeight: '300',
	playerWidth: '450',
	addThumbs: false,
	thumbSize: 'small',
	showInline: false,
	autoPlay: true,
	showRelated: true,
	allowFullScreen: true
  },options);

  return this.each(function() {

   		var selector = jQuery(this);

		var autoPlay = "";
		var showRelated = "&rel=0";
		var fullScreen = "";
		if(options.autoPlay) autoPlay = "&autoplay=1";
		if(options.showRelated) showRelated = "&rel=1";
		if(options.allowFullScreen) fullScreen = "&fs=1";

		//throw a youtube player in
		function play(id)
		{
		   var html  = '';

		   html += '<object type="application/x-shockwave-flash" style="width:'+options.playerWidth+'px; height:'+options.playerHeight+'px;" data="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'">';
		   html += '<param name="movie" value="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'" />';
		   if(options.allowFullScreen) {
		   		html += '<param name="allowfullscreen" value="true" />';
		   }
		   html += '<param name="allowscriptaccess" value="always" />';
		   html += '<param name="wmode" value="opaque" />';
		   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+'&showinfo=0';
		};


		//load inital video
		var firstVid = selector.children("li:first-child").addClass("currentvideo").children("a").attr("href");
		jQuery("#"+options.holderId+"").html(play(youtubeid(firstVid)));

		//Just show first description and hide others until they are needed
		 jQuery('.vid-desc-1').fadeIn();
		 jQuery('.vid-desc-2').hide();
		 jQuery('.vid-desc-3').hide();
		 jQuery('.vid-desc-4').hide();
		 jQuery('.vid-desc-5').hide();
		 jQuery('.vid-desc-6').hide();
		 jQuery('.vid-desc-7').hide();
		 jQuery('.vid-desc-8').hide();
		 jQuery('.vid-desc-9').hide();
		 jQuery('.vid-desc-10').hide();
		//load video on request
		selector.children("li").children("a").click(function() {

			if(options.showInline) {
				jQuery("li.currentvideo").removeClass("currentvideo");
				jQuery(this).parent("li").addClass("currentvideo").html(play(youtubeid($(this).attr("href"))));
			}
			else {
				jQuery("#"+options.holderId+"").html(play(youtubeid($(this).attr("href"))));
				jQuery(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
				jQuery(this).parent("li").addClass("currentvideo");
			}

			return false;
		});



		//Display description relevant to specific video
		selector.children("li").children("a").click(function() {

			// This gets the parent css class
			var this_linkid = $(this).parent().attr("class");

			// This extracts the number - we know its always at position 8 due to class naming
			var this_linkid_num_first = this_linkid.charAt(8);
			// This extracts the number - now we are upto double figures (upto 10) on vids, we need to check on second number too!
			var this_linkid_num_sec = '';
			var this_linkid_num_sec = this_linkid.charAt(9);

			var this_linkid_num = this_linkid_num_first+this_linkid_num_sec;

			var desc_identify = 'youtubedesc-'+this_linkid_num;

			if (this_linkid_num == 1) {
				 jQuery('.vid-desc-1').fadeIn();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}

			if (this_linkid_num == 2) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').fadeIn();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}

			if (this_linkid_num == 3) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').fadeIn();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}

			if (this_linkid_num == 4) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').fadeIn();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}

			if (this_linkid_num == 5) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').fadeIn();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}

			if (this_linkid_num == 6) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').fadeIn();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}


			if (this_linkid_num == 7) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').fadeIn();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}


			if (this_linkid_num == 8) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').fadeIn();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').hide();
			}


			if (this_linkid_num == 9) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').fadeIn();
				 jQuery('.vid-desc-10').hide();
			}


			if (this_linkid_num == 10) {
				 jQuery('.vid-desc-1').hide();
				 jQuery('.vid-desc-2').hide();
				 jQuery('.vid-desc-3').hide();
				 jQuery('.vid-desc-4').hide();
				 jQuery('.vid-desc-5').hide();
				 jQuery('.vid-desc-6').hide();
				 jQuery('.vid-desc-7').hide();
				 jQuery('.vid-desc-8').hide();
				 jQuery('.vid-desc-9').hide();
				 jQuery('.vid-desc-10').fadeIn();
			}

			return false;
		});



		//do we want thumbs with that?
		if(options.addThumbs) {

			selector.children().each(function(i){

				var replacedText = $(this).text();

				if(options.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);

			});

		}

  });

};
