// replace Vimeo/Youtube embeds

//<!-- Widescreen YouTube Embeds by Matthew Buchanan & Hayden Hunter
//  http://matthewbuchanan.name/451892574
//  http://blog.haydenhunter.me
//
//  Hacked around by Simon Starr to break out of Tumblr's max width of 500px
//  http://www.simonstarr.com
//
//  Original source here: http://pastie.org/871790
//
//  Released under a Creative Commons attribution license:
//  http://creativecommons.org/licenses/by/3.0/nz/ -->

  $(function() {
    // Change this to whatever width you like
    var contentWidth = 630;

    $("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; // /rel=0/g;
		var newOpts = "rel=0&amp;color1=0xFFFFFF&amp;color2=0xFFFFFF&amp;egm=0&amp;showinfo=0&amp;fs=1&amp;hd=1"; //rel=0&amp;color1=0xeeeeee&amp;color2=0xeeeeee;hd=1
        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(contentWidth * 0.75 + 25);
        var youtubeHeightWide = Math.floor(contentWidth * 0.5625 + 25);
        // Test for widescreen aspect ratio
        $.getJSON("http://gdata.youtube.com/feeds/api/videos/" + youtubeID[1] + "?v=2&alt=json-in-script&callback=?", function (data) {
          oldHeight = /height="?([0-9]*)"?/g;
          oldWidth = /width="?([0-9]*)"?/g;

          newWidth = 'width="' + contentWidth + '"';
          if (data.entry.media$group.yt$aspectRatio != null) {
            newHeight = 'height="' + youtubeHeightWide + '"';
          } else {
            newHeight = 'height="' + youtubeHeight + '"';
          }
          youtubeCode = youtubeCode.replace(oldHeight, newHeight);
          youtubeCode = youtubeCode.replace(oldWidth, newWidth);
          if (params != "") {
            params = params.replace(oldWidth, newWidth);
            params = params.replace(oldHeight, newHeight);
            youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
          }
          // Replace YouTube embed with new code
          parent.html(youtubeCode).css("visibility","visible");
        });
      }
    });
  });
  
  // Vimeo
//<!--Better Vimeo Embeds on Tumblr by Matthew Buchanan http://matthewbuchanan.name/141302328
//    Updated by Alex Hallajian http://journal.alexhallajian.com/post/1153909350
//    Tweaked by UnAmazing to remove portrait & title info and fix color http://unamazing.com -->

$(function() {
    var color = "ffffff";
    var opts = "title=0&byline=0&portrait=0";
    $("iframe").each(function() {
        var $obj = $(this);
        var data = $obj.attr("data");
        var vsrc = $obj.attr("src");
        var oldW = $obj.attr("width");
        var newW = 630;
        var oldH = $obj.attr("height");
        var p = oldW/newW;
        var newH = (oldH / p);
        if (vsrc.search("vimeo") > 0) {
            $obj.replaceWith("<iframe src='"+vsrc+"?"+opts+"&color="+color+"' width='"+newW+"' height='"+newH+"' frameborder='0'></iframe>");
        }
    });
})
