// Combustion Video Embed
(function(){
  if(typeof(window.cvideo_last_id_num) == 'undefined') window.cvideo_last_id_num = -1;
  window.cvideo_last_id_num += 1;

  // shortcuts
  var d = document;
  var urlEncode = function(x){return encodeURIComponent(x)};
  var scripts = d.getElementsByTagName("script");
  var current_node = scripts[scripts.length - 1];
  var addElement = function(x){current_node.parentNode.insertBefore(x, current_node.nextSibling)}; // nextSibling might not be available
  var trimSpaces = function(x){return (x || "").replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, "")}

  // get options
  var options_attr = current_node.getAttribute('content');
  if(options_attr){
    var options_lines = trimSpaces(options_attr || "").split(",");
    var options = {};
    for(var i in options_lines) {
      // ignore all non-string values
      // prototype.js adds function each
      if(typeof(options_lines[i]) != 'string') continue;

      name_value = trimSpaces(options_lines[i]).split("=");
      if(name_value.length == 2) { // only if name value pair
        options[name_value[0]] = name_value[1];
      }
    }
  }

  // translated and modified from sizing.rb
  var scaleVideo = function(w, h, fitw, fith) {
    multiple = 1; // not really needed as an argument

    // make sure they're all float so we don't lose decimals
    w = parseFloat(w);
    h = parseFloat(h);
    fitw = parseFloat(fitw);
    fith = parseFloat(fith);

    // do the scaling
    if(w > fitw) {
      h = (fitw/w) * h;
      w = fitw;
    }
    if(h > fith) {
      w = (fith/h) * w;
      h = fith;
    }

    // round them to integers
    w = parseInt(w);
    h = parseInt(h);

    // snip off a couple of pixels to match the multiple
    if((w % multiple) > 0) {
      w -= (w % multiple);
    }
    if((h % multiple) > 0) {
      h -= (h % multiple);
    }

    return [w, h]
  };


  // start creating the video element
  var video_iframe  = d.createElement('iframe');

  video_iframe.id           = 'cvideo_' + cvideo_last_id_num;
  video_iframe.className    = 'cvideo';

  // video iframe url
  video_iframe.src = "http://video.combustionlabs.com/v/robreynar/don-t-count-on-a-walkthrough.iframe";

  if(options) { // build the query if there are options
    var video_query = [];
    for(var option in options) { // make the name-value pairs
      video_query.push(urlEncode(option) + "=" + urlEncode(options[option]))
    }
    video_query      = video_query.join('&');
    video_iframe.src = video_iframe.src + '?' + video_query;
  }

  // default size
  video_iframe.width  = 640;
  video_iframe.height = 480;

  if(options) {
    // scaled size
    if(options.size) {
      var size = options.size.split('x');
      wh = scaleVideo(video_iframe.width, video_iframe.height, size[0], size[1]);
      video_iframe.width  = wh[0];
      video_iframe.height = wh[1];
    } else if(options.width && options.height) {
      wh = scaleVideo(video_iframe.width, video_iframe.height, options.width, options.height);
      video_iframe.width  = wh[0];
      video_iframe.height = wh[1];
    } else if(options.width) {
      wh = scaleVideo(video_iframe.width, video_iframe.height, options.width, 9999);
      video_iframe.width  = wh[0];
      video_iframe.height = wh[1];
    } else if(options.height) {
      wh = scaleVideo(video_iframe.width, video_iframe.height, 9999, options.height);
      video_iframe.width  = wh[0];
      video_iframe.height = wh[1];
    }
  }

  video_iframe.height = parseInt(video_iframe.height) + 24; // needed adjustment for the control bar

  // common attributes
  video_iframe.scrolling    = 'no';
  video_iframe.frameBorder  = 0;
  video_iframe.marginHeight = 0;
  video_iframe.marginWidth  = 0;

  addElement(video_iframe);

})();
