
document.observe('dom:loaded', setup );

// ----------------------------------------------------------------------------
function setup() {

  $('imageSizeButton').observe('click',   calcImageSize );
  $('memoryLimitButton').observe('click', calcMemoryLimit );
  
  $('megapixel').observe('keyup',    calcFromMegapixel );
  $('dstmegapixel').observe('keyup', calcFromMegapixel );

  $('srcheight').observe('keyup', calcToMegapixel );
  $('srcwidth').observe('keyup',  calcToMegapixel );
  $('dstheight').observe('keyup', calcToMegapixel );
  $('dstwidth').observe('keyup',  calcToMegapixel );

  calcToMegapixel();

}

// ----------------------------------------------------------------------------
function calcImageSize() {

  bytes = Math.round( $('memorylimit').value / $('fudgefactor').value * 1024 * 1024 );
  bpp   = $('bpp3').checked ? 3 : 4;

  if ( $('radiosrc').checked ) {
    bytes = bytes - $('srcwidth').value * $('srcheight').value;
    $('dstwidth').value  = Math.round( Math.sqrt( bytes / bpp ) );
    $('dstheight').value = Math.round( Math.sqrt( bytes / bpp ) );
  }
  else {
    bytes = bytes - $('dstwidth').value * $('dstheight').value;
    $('srcwidth').value  = Math.round( Math.sqrt( bytes / bpp ) );
    $('srcheight').value = Math.round( Math.sqrt( bytes / bpp ) );
  }
  
  srcbytes = $('srcwidth').value * $('srcheight').value * bpp;
  dstbytes = $('dstwidth').value * $('dstheight').value * bpp;
  
  $('megapixel').value    = ( srcbytes / ( 1024 * 1024 ) / bpp ).toFixed( 1 );
  $('dstmegapixel').value = ( dstbytes / ( 1024 * 1024 ) / bpp ).toFixed( 1 );

  return false;

}

// ----------------------------------------------------------------------------
function calcMemoryLimit() {

  bpp = $('bpp3').checked ? 3 : 4;

  $('memorylimitini').innerHTML =
    $('memorylimitcode').innerHTML =
      $('memorylimit').value =
        Math.round(
          (
            $('dstwidth').value * $('dstheight').value * bpp +
            $('srcwidth').value * $('srcheight').value * bpp
          ) *
          $('fudgefactor').value
          / 1024 / 1024
        )
  ;

  return false;

}

// ----------------------------------------------------------------------------
function calcFromMegapixel( e ) {

  element = Event.element( e );

  if ( element.id == 'megapixel' ) {
    $('srcwidth').value  = 
      $('srcheight').value = Math.round( Math.sqrt( element.value * 1024 * 1024 ) );
  }

  if ( element.id == 'dstmegapixel' ) {
    $('dstwidth').value  = 
      $('dstheight').value = Math.round( Math.sqrt( element.value * 1024 * 1024 ) );
  }

}

// ----------------------------------------------------------------------------
function calcToMegapixel( e ) {

  if ( typeof( e ) != 'undefined' )
    element = Event.element( e );

  if ( typeof( element ) == 'undefined' 
       || ( element.id == 'srcheight' ) || ( element.id == 'srcwidth' ) ) {
    $('megapixel').value  = 
      ( $('srcheight').value * $('srcwidth').value / 1024 / 1024 ).toFixed( 1 );
  }

  if ( typeof( element ) == 'undefined'
       || ( element.id == 'dstheight' ) || ( element.id == 'dstwidth' ) ) {
    $('dstmegapixel').value  = 
      ( $('dstheight').value * $('dstwidth').value / 1024 / 1024 ).toFixed( 1 );
  }

}

