
var vidWidth  = 180;
var vidHeight = 160;

$(document).ready(function()
{

    // Inspirado en http://stackoverflow.com/questions/1712847/find-all-youtube-links-with-js-jquery
    // Adaptado por Cancer, implementado por BumbleMac

    /////////////
    // Youtube //
    /////////////

    // Cleanup por si el video está dentro de un [link][/link]
    $('#videos_container a').each(function()
    {
        var href = $(this).attr('href');
        if(href.search(/youtube.com\/watch/gi) >= 0) {
            var acontents = $(this).html();
            // console.log( acontents );
            $(this).data('content', acontents );
            $(this).text(href);
        } // end if
    });
    
    var obj = '<object width="' + vidWidth + '" height="' + vidHeight + '">'
            +     '<param name="movie" value="http://www.youtube.com/v/[vid]&hl=es&fs=1"></param>'
            +      '<param name="allowFullScreen" value="true"></param>'
            +      '<param name="allowscriptaccess" value="always"></param>'
            +      '<embed src="http://www.youtube.com/v/[vid]&hl=es&fs=1" ' +
                          'type="application/x-shockwave-flash" allowscriptaccess="always" ' +
                          'allowfullscreen="true" width="' + vidWidth + '" ' + 'height="' + vidHeight + '">'
            +      '</embed>'
            + '</object>';

    $('#videos_container a:contains("youtube.com/watch")').each(function()
    {
        var that = $(this);
        var vid = that.html().match(/(?:v=)([\w\-]+)/g); // end up with v=oHg5SJYRHA0
        if (vid.length) 
        {
            $.each(vid, function(i)
            {
                that.html( '<span style="display: inline-block; border: 1px solid red;">' + obj.replace(/\[vid\]/g, this.replace('v=','')) + '</span><br/>' +
                           '<a href="http://www.youtube.com/watch?' + vid + '" target="_blank">' + that.data('content') + '</a>' );
                that.removeAttr('href');
            }); // .each
        } // end if
    }); // #videos_container .each youtube

}); // .ready


