$(document).ready(function() {

    $('.reblog,article footer a').tipTip({delay:250});
    $('img').fullsize({attribute:'data-fullsize'});
    $('object,embed,article iframe').each(function(index) {
        // Skip the audio player
        if ($(this).parent().attr('class') == 'audio_player')
            return;

        var elem = $(this),
            newHeight = Math.round(556 * (elem.attr('height') / elem.attr('width'))),
            parent = elem.parent();

        if (elem.find('embed[src*="vimeo"]').length > 0 || elem.attr('src').match(/player.vimeo.com/)) {
            var attr = elem.attr('data') || elem.attr('src'),
                clipId = (attr.match(/clip_id=(\d+)/) || attr.match(/player\.(([\w\d_\.]+)\.)?vimeo(ws)?\.com\/video\/(\d+)/i)).pop(),
                clipUrl = 'http://vimeo.com/' + clipId;
            $.getJSON('http://vimeo.com/api/oembed.json?callback=?&width=556&color=008acd&universal=1&url=' + clipUrl, function(data) {
                parent.html(data.html);
            });
        }
        else if (elem.find('embed[src*="youtube"]').length > 0) {
            var videoId = elem.find('embed').attr('src').match(/\/v\/([0-9A-Za-z-_]*)/)[1],
                videoUrl = 'http://www.youtube.com/watch?v=' + videoId;
            $.getJSON('http://api.embed.ly/v1/api/oembed?maxwidth=556&format=json&callback=?&url=' + videoUrl, function(data) {
                parent.html(data.html);
            });
        }
        else if ((elem.attr('src') && elem.attr('src').indexOf('theonion') != -1) || elem.find('param[movie*="theonion"]').length > 0) {
            elem.attr({
                width: 480,
                height: 430
            });
        }
        else {
            if (elem.attr('width') != '556') {
                elem.attr({
                    width: 556,
                    height: newHeight
                });
            }
        }
    });
});