// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
jQuery(document).ajaxSend(function(event, request, settings) {
    if (typeof(AUTH_TOKEN) == "undefined") return;
    if (/authenticity_token=/.test(settings.data)) return;
    settings.data = ((settings.data) ? settings.data + "&" : "") +
        "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

function calculate() {
  try {
    war = $('#width').val().match(/(\d+)/);
    har = $('#height').val().match(/(\d+)/);
  } catch(e) {
    return;
  }
  
  if (!war) {
    war = [1];
  }
  if (!har) {
    har = [1];
  }
  w = war[0];
  if (w==0) {
    w = 1;
    $('#width').val(w);
  }
  h = har[0];
  if (h==0) {
    h = 1;
    $('#height').val(h);
  }
  k = h/w;

  www = Math.sqrt(4096*2048/k);
  $('#alamyw').text(Math.ceil(www));
  $('#alamyh').text(Math.ceil(www*k));

  www = Math.sqrt(2000*2000/k);
  $('#mp4w').text(Math.ceil(www));
  $('#mp4h').text(Math.ceil(www*k));

  www = Math.sqrt(2000*2500/k);
  $('#mp5w').text(Math.ceil(www));
  $('#mp5h').text(Math.ceil(www*k));

  www = Math.sqrt(2000*3000/k);
  $('#mp6w').text(Math.ceil(www));
  $('#mp6h').text(Math.ceil(www*k));

  www = Math.sqrt(1200*1600/k);
  $('#ismw').text(Math.ceil(www));
  $('#ismh').text(Math.ceil(www*k));

  www = Math.sqrt(1920*2560/k);
  $('#islw').text(Math.ceil(www));
  $('#islh').text(Math.ceil(www*k));

  www = Math.sqrt(2800*4200/k);
  $('#isxlw').text(Math.ceil(www));
  $('#isxlh').text(Math.ceil(www*k));

  www = Math.sqrt(3300*4900/k);
  $('#isxxlw').text(Math.ceil(www));
  $('#isxxlh').text(Math.ceil(www*k));

  www = Math.sqrt(3700*5600/k);
  $('#isxxxlw').text(Math.ceil(www));
  $('#isxxxlh').text(Math.ceil(www*k));
}

function documentReady() {
  $('#width').keyup(function() {
   calculate();
  });
  $('#height').keyup(function() {
    $('#width').keyup();
  });
  calculate();
  
  $("a.rjs").click(function() {
    $.ajax({
      url: this.href,
      dataType: "script",
      beforeSend: function(xhr) {
        xhr.setRequestHeader("Accept", "text/javascript");
      }
    });
    return false;
  });
}

$(document).ready(documentReady); 
