function fixVimeo() {
    /*
        Better Vimeo Embeds 2.1 by Matthew Buchanan
        Modelled on the Vimeo Embedinator Script
        http://mattbu.ch/tumblr/vimeo-embeds/

        Released under a Creative Commons attribution license:
        http://creativecommons.org/licenses/by/3.0/nz/
    */
    var color = "caaf22";
    var opts = "title=0&byline=0&portrait=0";
    $("iframe[src^='http://player.vimeo.com']").each(function() {
        var src = $(this).attr("src");
        var w = $(this).attr("width");
        var h = $(this).attr("height");
        if (src.indexOf("?") == -1) {
            $(this).replaceWith(
                "<iframe src='"+src+"?"+opts+"&color="+
                color+"' width='"+w+"' height='"+h+
                "' frameborder='0'></iframe>"
            );
        }
    });
    $("object[data^='http://vimeo.com']").each(function() {
        var $obj = $(this);
        var data = $obj.attr("data");
        var temp = data.split("clip_id=")[1];
        var id = temp.split("&")[0];
        var server = temp.split("&")[1];
        var w = $obj.attr("width");
        var h = $obj.attr("height");
        $obj.replaceWith(
            "<iframe src='http://player.vimeo.com/video/"
            +id+"?"+server+"&"+opts+"&color="+color+
            "' width='"+w+"' height='"+h+
            "' frameborder='0'></iframe>"
        );
    });
}

function fixYouTube() {
	/*
        Widescreen YouTube Embeds by Matthew Buchanan & Hayden Hunter
        http://matthewbuchanan.name/451892574
        http://blog.haydenhunter.me

        Released under a Creative Commons attribution license:
        http://creativecommons.org/licenses/by/3.0/nz/
    */
    $("div.post object").each(function() {
        if ($(this).find("embed[src^='http://www.youtube.com']").length > 0) {
            // Identify and hide embed(s)
            var parent = $(this).parent();
            parent.css("visibility", "hidden");
            var youtubeCode = parent.html();
            var params = "";
            if (youtubeCode.toLowerCase().indexOf("<param") == -1) {
                // IE doesn't return params with html(), soâ€¦
                $("param", this).each(function() {
                    params += $(this).get(0).outerHTML;
                });
            }
            // Set colours in control bar to match page background
            var oldOpts = /rel=0/g;
            var newOpts = "rel=0&amp;color1=0xFFFFFF&amp;color2=0xFFFFFF";
            youtubeCode = youtubeCode.replace(oldOpts, newOpts);
            if (params != "") {
                params = params.replace(oldOpts, newOpts);
                youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
            }
            // Extract YouTube ID and calculate ideal height
            var youtubeIDParam = $(this).find("embed").attr("src");
            var youtubeIDPattern = /\/v\/([0-9A-Za-z-_]*)/;
            var youtubeID = youtubeIDParam.match(youtubeIDPattern);
            var youtubeHeight = Math.floor(parent.find("object").width() * 0.75 + 25 - 3);
            var youtubeHeightWide = Math.floor(parent.find("object").width() * 0.5625 + 25 - 3);
            // Test for widescreen aspect ratio
            $.getJSON("http://gdata.youtube.com/feeds/api/videos/" + youtubeID[1] + "?v=2&alt=json-in-script&callback=?", function(data) {
                oldOpts = /height="?([0-9]*)"?/g;
                if (data.entry.media$group.yt$aspectRatio != null) {
                    newOpts = 'height="' + youtubeHeightWide + '"';
                } else {
                    newOpts = 'height="' + youtubeHeight + '"';
                }
                youtubeCode = youtubeCode.replace(oldOpts, newOpts);
                if (params != "") {
                    params = params.replace(oldOpts, newOpts);
                    youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
                }
                // Replace YouTube embed with new code
                parent.html(youtubeCode).css("visibility", "visible");
            });
        }
    });
}

function errorRedirect() {
	if ($("h1:contains('Not Found')").length >0) { window.location.href='/404'; }
}

function fixQuotes() {
	$(".attribution").each(function(){result=this.innerHTML;this.innerHTML=result=/^\s*<p>.+<\/p>$/.test(result)?result:"<p>"+result+"</p>"});
	$("blockquote").each(function(){result=this.innerHTML;this.innerHTML=result=/^\s*<p>.+<\/p>$/.test(result)?result:"<p>"+result+"</p>"});
	$("p:empty").remove();
}

function fixPres() {
	$("pre").addClass("prettyprint");
}

$(function() {
	errorRedirect();
    fixVimeo();
    fixYouTube();
	fixQuotes();
	fixPres();
	prettyPrint();
});

