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: 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)
		{
		   var html = '';
		   		if (id.match('vimeo')) {
                    embedHeight = 300;
                    tmp = id.split('/');
                    embedMovie = 'http://vimeo.com/moogaloop.swf?clip_id='+tmp[3]+'&server=vimeo.com&show_title=0&show_byline=1&show_portrait=0&color=&fullscreen=1';
                }

                if (id.match('youtube')) {
                    embedHeight = 324;
                    tmp = id.split('v=');
                    embedMovie = 'http://www.youtube.com/v/'+tmp[1]+'&hl=en_US&fs=1&';
                }

                html = '<object width="400" height="'+embedHeight+'">';
                html += '<param name="allowfullscreen" value="true"></param>';
                html += '<param  name="wmode" value="opaque"></param>';
                html += '<param name="movie" value="'+embedMovie+'"></param>';
                html += '<embed src="'+embedMovie+'" type="application/x-shockwave-flash" allowscriptaccess="sameDomain" allowfullscreen="true" width="400" height="'+embedHeight+'" wmode="opaque"></embed>';
                html += '</object>';
		   return html;

		};



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

		//load video on request
		selector.children("li").children("a").click(function() {

			if(options.showInline) {
				$("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo").html(play($(this).attr("href")));
			}
			else {
				$("#"+options.holderId+"").html(play($(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();

				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";
				}
                */
                thumb =$(this).children("a").attr('thumb');
                desc =$(this).children("a").attr('description');
				$(this).children("a").empty().html("<img src='"+thumb+"'/>"+desc);

			});

		}



  });

};
