//By Andres from Teckpert.com - Thanks! -http://www.teckpert.com/blog/jquery-development-your-own-plugins-part-2/
(function($){
    $.fn.embeddables = function(){
        var supportedSites = {
            'youtube' : '<object width="560" height="340"><param name="movie" value="[URL]&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="[URL]&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>',
            'dailymotion': '<object width="480" height="275"><param name="movie" value="[URL]"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="[URL]" type="application/x-shockwave-flash" width="480" height="275" allowfullscreen="true" allowscriptaccess="always"></object>',
            'megavideo': '<object width="640" height="467"><param name="movie" value="[URL]"></param><param name="allowFullScreen" value="true"></param><embed src="[URL]" type="application/x-shockwave-flash" allowfullscreen="true" width="640" height="467"></embed></object>',
            'vimeo' : '<object width="575" height="323"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="[URL]" /><embed src="[URL]" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="575" height="323"></embed></object>'
        }
        var handle = {
            'youtube'    : ['watch?v=', 'v/'],
            'dailymotion': [/.*dailymotion.*\/video\/(.*?)_.*/, 'http://dailymotion.com/swf/$1'],
            'megavideo'  : ['?v=', 'v/'],
            'vimeo'      : [/.*vimeo.*\/(.*[0-9])/, 'http://vimeo.com/moogaloop.swf?clip_id=$1&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=00adef&fullscreen=1']
        }
        // Load in this element for all a tags
        this.each(function(){
            var href   = this.getAttribute('href');
            var aClass = this.getAttribute('class');
            var key = href.match(/(http:\/\/www\.|http:\/\/|www\.|)(.*?)\.(\.com|\.*)/)[2];
            if( supportedSites[key] )
            {
               if( href.indexOf('http://www.') == -1 )
                        href = 'http://www.' + href.replace('www.', '');
                if( aClass.indexOf('modal') > -1 )
                {
                    $(this).click(function(){
                        $.fn.colorbox({
                            html: supportedSites[key].replace(/\[URL\]/ig, href.replace(handle[key][0], handle[key][1])),
                            open: true
                        });
                                return false;
                     })
                }else{
                    $(this).before(supportedSites[key].replace(/\[URL\]/ig, href.replace(handle[key][0], handle[key][1]))).remove();
                }
            }
        });
    }
})(jQuery)
