// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// cage_bot.js
//
// copyright (c) 2010 drow <drow@bin.sh>
// all rights reserved.

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// configuration

// $('bot').getWidth() doesn't seem to work properly, and returns a
//   value dependent on $('cage').getWidth()
// so, content begins in $('src'), and gets copied into $('bot') later

  var bot_w = $('src').getWidth();
  var bot_h = $('src').getHeight();
  var cage_w = $('cage').getWidth();
  var cage_h = $('cage').getHeight();

  var off_x = 0;
  var off_y = 0;
  var dx = 1;
  var dy = 1;
  var dt = 30;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// bounce

  function bounce () {
    if ((off_x + dx) >= (bot_w - cage_w)) {
      off_x = bot_w - cage_w;           dx = 0 - dx;
    } else if ((off_x + dx) <= 0) {
      off_x = 0;                        dx = 0 - dx;
    } else {
      off_x += dx;
    }
    if ((off_y + dy) >= (bot_h - cage_h)) {
      off_y = bot_h - cage_h;           dy = 0 - dy;
    } else if ((off_y + dy) <= 0) {
      off_y = 0;                        dy = 0 - dy;
    } else {
      off_y += dy;
    }
    $('bot').setStyle('left: ' + (0 - off_x) + 'px;');
    $('bot').setStyle('top: '  + (0 - off_y) + 'px;');
  }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// showtime

  $('bot').update($('src').innerHTML);
  setInterval('bounce()',dt);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


