(function($){

    var yl2e = {
        onclick: function() {

            href    = 'http://www.youtube.com/v/' + this.href.match(/[a-zA-Z0-9-_]+$/) + '&rel=0&autoplay=1';
            wrapper = $('<div class="youtubeLinksToEmbed" style="display:none;"><img src="/images/loader.gif" alt="" /><div style="display:none;"></div></div>');
            player  = $('div:first', wrapper);


            player.flash({src: href, width: 425, height: 355});
            $(this).parent().html(wrapper);
            wrapper.queue(function(){
                $(this).dequeue();
                $(this).show();
                $(this).dequeue();
            });
    
            setTimeout(function(){
                wrapper.queue(function(){
                    $(this).find('img').hide().end();
                    $(this).dequeue();
                    $(this).animate({height: 375});
                    $(this).dequeue();
                });
                setTimeout(function(){
                    player.show();    
                }, 500);
            }, 1500);

            return false;
        }    
    }

    $.extend($.fn, {
        youtubeLinksToEmbed: function(){
            $(this).find('a[href*=youtube.com/watch?v=]')
                .each(function(){ 
                    $(this).click(yl2e.onclick) });
        }
    });
})(jQuery);

