jQuery.fn.ytplaylist = function(options) {
 
    // default settings
    var options = jQuery.extend( { 
        holderId: 'ytholder',
        playerHeight: '370',
        playerWidth: '550',
        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 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");
        jQuery("#"+options.holderId+"").html(play(youtubeid(firstVid)));
		
        //load video on request
        selector.children("li").children("a").click(function() {
            //returnPlayer();
			
			
            if(options.showInline) {
                jQuery("li.currentvideo").removeClass("currentvideo");
                jQuery(this).parent("li").addClass("currentvideo").html(play(youtubeid(jQuery(this).attr("href"))));
                                 
                               
            }
            else {
			    
                //jQuery("#"+options.holderId+"").html('testeee you');
                //play(youtubeid(jQuery(this).attr("href")))
                jQuery(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
                jQuery(this).parent("li").addClass("currentvideo");
                abreyou((play(youtubeid(jQuery(this).attr("href")))));
                jQuery("#"+options.holderId+"").dialog("open");
			
            }
															 
			
			
            return false;
        });
		
        //do we want thumns with that?
        if(options.addThumbs) {
			
            selector.children().each(function(i){
											  
                var replacedText = jQuery(this).text();
				
                if(options.thumbSize == 'small') {
                    var thumbUrl = "http://img.youtube.com/vi/"+youtubeid(jQuery(this).children("a").attr("href"))+"/2.jpg";
                }
                else {
                    var thumbUrl = "http://img.youtube.com/vi/"+youtubeid(jQuery(this).children("a").attr("href"))+"/0.jpg";
                }
				
				
                jQuery(this).children("a").empty().html("<img class='you_thumb' src='"+thumbUrl+"' alt='"+replacedText+"' />"+replacedText).attr("title", replacedText);
                jQuery(this).attr("id",youtubeid(jQuery(this).children("a").attr("href")));
				
            });
			
        }
			
		
   
    });
 
};
