/** 
 * Common mBed customization JS functions
 ***/

function updateCustomEmbedSize() {
  var width = 0;
  if ($F('embed_size') == 'Small') {
    width = 410;
    $('customwidth').value = width;
    $('customwidth').disable().addClassName('dis');
  } else if ($F('embed_size') == 'Large') {
    width = 679;
    $('customwidth').value = width;
    $('customwidth').disable().addClassName('dis');
  } else {
    width = parseInt($F('customwidth'));
    $('customwidth').enable().removeClassName('dis');
  }

  width = constrainedEmbedWidth(width);
  var height = Math.floor(width * 9 / 16)  + 28;
  $('customheight').value = height;
}

function constrainedEmbedWidth(width) {
  if (isNaN(width)) width = 410;
  if (width < 410) width = 410; // minimum width is 410
  if (width > 720) width = 720; // maximum width is 720
  return width;
}

function updateEmbedCode(code, width, height, url) {
  code = code.replace(/__WIDTH__/g, width.toString());
  code = code.replace(/__HEIGHT__/g, height.toString());
  code = code.replace(/__URL__/g, url.toString());
    
  $('embedcode').value = code;
}
