jQuery.fn.ytplaylist = function(options) {
 
  // default settings
  var options = jQuery.extend( {
    holderId: 'ytvideo',
	playerHeight: '212',
	playerWidth: '320',
	addThumbs: true,
	thumbSize: 'small',
	showInline: false,
	autoPlay: true,
	showRelated: true,
	allowFullScreen: false
  },options);
 
  return this.each(function() {
							
   		var selector = $(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){
		   if(id == 'video' || id == 'clip1' || id == 'clip2' || id == 'clip3' || id == 'clip4' || id == 'clip5' || id == 'clip6'){
			   var html  = '<embed src="http://62.193.234.152/player.swf?file=http://62.193.234.152/'+id+'.flv&image=http://62.193.234.152/'+id+'.jpg&link=http://www.bsmcom.com/&bufferlength=1&autostart=false&controlbar=over&menu=false" allowfullscreen="true" type="application/x-shockwave-flash" width="320" height="212" ></embed>';
			   return html;
		   }
		   else{
			   var html  = '';
		
			   html += '<object height="'+options.playerHeight+'" width="'+options.playerWidth+'">';
			   html += '<param name="movie" value="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
			   html += '<param name="wmode" value="transparent"> </param>';
			   if(options.allowFullScreen){
					html += '<param name="allowfullscreen" value="true"> </param>';
			   }
			   html += '<embed src="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"';
			   if(options.allowFullScreen){
					html += ' allowfullscreen="true" ';
			   }
			   html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+options.playerHeight+'" width="'+options.playerWidth+'"></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;
		};
		
		//load inital video
		var firstVid = selector.children("li:first-child").addClass("currentvideo").children("a").attr("href");
		//alert('1'+firstVid);
		$("#"+options.holderId+"").html(play(youtubeid(firstVid)));
		
		//load video on request
		selector.children("li").children("a").click(function(){
			if(options.showInline) {
				$("li.currentvideo").removeClass("currentvideo");
				//alert ('2'+$(this).attr("href"));
				$(this).parent("li").addClass("currentvideo").html(play(youtubeid($(this).attr("href"))));
			}
			else {
				//alert ('3'+$(this).attr("href"));
				$("#"+options.holderId+"").html(play(youtubeid($(this).attr("href"))));
				$(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo");
			}
			return false;
		});
		
		//do we want thumns with that?
		if(options.addThumbs) {
			selector.children().each(function(i){
				var replacedText = $(this).text();
				//alert ('4'+$(this).children("a").attr("href"));
				if(youtubeid($(this).children("a").attr("href")) == 'video' 
					|| youtubeid($(this).children("a").attr("href")) == 'clip1' 
					|| youtubeid($(this).children("a").attr("href")) == 'clip2'
					|| youtubeid($(this).children("a").attr("href")) == 'clip3'
					|| youtubeid($(this).children("a").attr("href")) == 'clip4'
					|| youtubeid($(this).children("a").attr("href")) == 'clip5'
					|| youtubeid($(this).children("a").attr("href")) == 'clip6'){
					var thumbUrl = "http://62.193.234.152/"+youtubeid($(this).children("a").attr("href"))+".jpg";
				}
				else{
					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+"' />").attr("title", replacedText);
			});	
		}
			
		
   
  });
 
};