/* yanugs.js
 *  Bouncing Yanugs
 *
 * v2.4 - september 24, 2010, Fix bug with duration, added min
 * v2.3 - June 18, 2007 - Fix some small timing issues
 * v2.2 - June 10, 2007 - Make it work on IE7
 * v2.1 - June 7, 2007 - Fix framerate bug
 * v2.0 - June 6, 2007 - rewrite
 * v1.3 - May 19, 2007 - slowdown framerate over time
 * v1.2 - May 13, 2007 - onmouseover for yanugs
 * v1.1 - March 7, 2007 Hacked by JeanHuguesRobert
 * Loosely based on Boucing Images by Lloyd Hassell
 * lloydhassell.com
 */

YanUgs = {
  imgSrc: [
    'http://virteal.com/yanug_sad64.png',
    'http://virteal.com/yanug_neutral64.png',
    'http://virteal.com/yanug_blink64.png',
    'http://virteal.com/yanug64.png'
  ],
  frameCount: 0,
  startDate: null,
  allStartDates: [],
  dirX: [],
  dirY: [],
  posX: [],
  posY: [],
  speedX: [],
  speedY: [],
  mouseX: 0,
  mouseY: 0,
};

YanUgs.gaussian_rand = function (){
  var a, b, ft;
  ft = 2.0;
  while( ft >= 1.0)  {
    a = 2.0 * Math.random() - 1.0;
    b = 2.0 * Math.random() - 1.0;
    ft = a * a + b * b;
  }
  ft= Math.sqrt( (-2.0 * Math.log( ft)) / ft);
  return a * ft;
}

YanUgs.onmousemove = function ( e ){
  var de = false, bug = function(a){console && console.log( a)}
  var ya = YanUgs;
  var ok = false;
  if( window.event ){ e = window.event; }
  try{
    if( e.clientX || e.clientY ){
      ya.mouseX = e.clientX;
      ya.mouseY = e.clientY;
      ok = true;
    }
    de&&bug( "YU, x: " + ya.mouseX + ", y: " + ya.mouseY)
  }catch( x ){}
  if( !ok && (e.pageX || e.pageY) ){
    ya.mouseX
    = e.pageX + document.body.scrollLeft
    - document.documentElement.scrollLeft;
    ya.mouseY
    = e.pageY + document.body.scrollTop
    - document.documentElement.scrollTop;
    de&&bug( "YUb, x: " + ya.mouseX + ", y: " + ya.mouseY)
  }
}

YanUgs.move = function (){
  var de = true, bug = function(a){console && console.log( a)}
  var ya = YanUgs;
  if( !ya.startDate ){
    ya.startDate = new Date();
   
    ya.winWidth  = ((window.innerWidth && (window.innerWidth - 18))
    || document.documentElement.clientWidth) - 3;
    ya.winHeight = (window.innerHeight
    || document.documentElement.clientHeight) - 3;
    ya.div = [];
    for( var ii = 0 ; ii < 4 ; ii++ ){
      ya.posX[ii] = 10000;
      ya.posY[ii] = 0;
      ya.dirX[ii] = 'right';
      ya.dirY[ii] = 'down';
      ya.speedX[ii] = 0.1;
      ya.speedY[ii] = 0.1
      ya.allStartDates[ii] = ya.startDate;
      var d = ya.div[ii] = document.createElement( "div");
      d.style.position = "absolute";
      d.style.left = "0px";
      d.style.top = "0px";
      d.style.width = "64px";
      d.style.height = "64px";
     
      //d.setAttribute( 'style',
      //'position:absolute;left:0px;top:0px;width:64px;height:64px');
      d.innerHTML = '<img src="' + ya.imgSrc[ii] + '"'
      + ' height="64" width="64" border="0" vspace="0" hspace="0"'
      + '>';
      document.body.appendChild( d);
      d.onmouseover = function(){
        for( var ii = 0 ; ii < 4 ; ii++ ){
          if( ya.div[ii] == this ){
            //ya.posX[ii] = 0;
            //ya.posY[ii] = 0;
            ya.allStartDates[ii] = new Date();
          }
        }
      }
    }
    document.onmousemove = ya.onmousemove;
  }
  ya.frameCount = ya.frameCount + 1;
  var time_now = (new Date()).getTime();
  var start_date = null;
  var duration = 0;
  var min_duration = time_now - ya.startDate.getTime();
  for( var ii = 0 ; ii < 4 ; ii++ ){
   
    start_date = ya.allStartDates[ii] || ya.startDate;
    duration = time_now - start_date.getTime();
    if( duration < min_duration ){
      min_duration = duration
    }
    var style = ii - 2;
    var speed = 1000 / 2;
    if( duration > speed * ( 4 + ii)     ){ style += 1; }
    if( duration > speed * ( 8 + ii * 2) ){ style += 1; }
    if( duration > speed * (12 + ii * 4) ){ style += 1; }
    if( duration > speed * (20 + ii * 6) ){ style += 1; }
    if( duration > speed * (28 + ii * 8) ){ style += 1; }
    if( style < 0 ){ style = 0; }
    if( style > 3 ){ style = 3; }
    /* Don't change direction too fast */
    var amount_x, amount_y;
    if( true ){
      if( style < 2 ){
        amount_x = (ya.gaussian_rand() / ya.gaussian_rand())
        * 0.5 * (Math.random() - 0.5) * 10;
        amount_y = (ya.gaussian_rand() / ya.gaussian_rand())
        * 0.5 * (Math.random() - 0.5) * 10;
      }else{
        amount_x = ya.gaussian_rand() * style * (Math.random() - 0.5) * 10;
        amount_y = ya.gaussian_rand() * style * (Math.random() - 0.5) * 10;
      }
      var accel = (4 - style);
      accel = (accel * accel * accel) / 60;
      ya.speedX[ii] += (amount_x - ya.speedX[ii]) * accel;
      ya.speedY[ii] += (amount_y - ya.speedY[ii]) * accel;
    }
   
    if( ii == 0 ){
   
    }
    if( Math.abs( (ya.posX[ii] + 32) - ya.mouseX) < 64
    &&  Math.abs( (ya.posY[ii] + 32) - ya.mouseY) < 64
    ){
      ya.allStartDates[ii] = new Date();
      if( ya.mouseX < (ya.posX[ii] + 32) ){
        ya.dirX[ii] = 'right';
      }else{
        ya.dirX[ii] = 'left';
      }
      if( ya.mouseY < (ya.posY[ii] + 32) ){
        ya.dirY[ii] = 'down';
      }else{
        ya.dirY[ii] = 'up';
      }
      if( (ya.speedX[ii] + ya.speedY[ii]) < 10 ){
        if( Math.random() >=  0.5 ){
          ya.speedX[ii] = ya.speedX[ii] = 8;
        }else{
          ya.speedY[ii] = ya.speedY[ii] = 8;
        }
      }
    }
    if( ya.speedX[ii] > 8 ){ ya.speedX[ii] = 8; }
    if( ya.speedX[ii] < 0.1 ){ ya.speedX[ii] = 0.1; }
    if( ya.speedY[ii] > 8 ){ ya.speedY[ii] = 8; }
    if( ya.speedY[ii] < 0.1 ){ ya.speedY[ii] = 0.1; }
    if( ya.dirX[ii] == 'left' ){
      if( YanUgs.posX[ii] > YanUgs.speedX[ii] ){
        YanUgs.posX[ii] -= YanUgs.speedX[ii];
      }else{
        ya.dirX[ii] = 'right';
        ya.posX[ii] = 0;
      }
    }else{
      if( (ya.posX[ii] + 64) < (ya.winWidth - ya.speedX[ii]) ){
        ya.posX[ii] += ya.speedX[ii];
      }else{
        ya.dirX[ii] = 'left';
        ya.posX[ii] = ya.winWidth - 64;
      }
    }
    if( ya.dirY[ii] == 'up' ){
      if( ya.posY[ii] > ya.speedY[ii] ){
        ya.posY[ii] -= ya.speedY[ii];
      }else{
        ya.dirY[ii] = 'down';
        ya.posY[ii] = 0;
      }
    }else{
      if( (ya.posY[ii] + 64) < (ya.winHeight - ya.speedY[ii]) ){
        ya.posY[ii] += ya.speedY[ii];
      }else{
        ya.dirY[ii] = 'up';
        ya.posY[ii] = ya.winHeight - 64;
      }
    }
    if( ya.posX[ii] < 0 ) ya.posX[ii] = 0;
    if( ya.posY[ii] < 0 ) ya.posY[ii] = 0;
    if( (ya.posX[ii] + 64) >= ya.winWidth ){  ya.posX[ii] = ya.winWidth - 64; }
    if( (ya.posY[ii] + 64) >= ya.winHeight ){ ya.posY[ii] = ya.winHeight - 64; }
    ya.div[ii].style.left = ""
    + ( ya.posX[ii]
      + (document.documentElement.scrollLeft || document.body.scrollLeft))
    + "px";
    ya.div[ii].style.top = ""
    + ( ya.posY[ii]
      + (document.documentElement.scrollTop || document.body.scrollTop))
    + "px";
    var img = ya.div[ii].getElementsByTagName( 'IMG')[0];
    if( img.src !== ya.imgSrc[style] ){
      img.src = ya.imgSrc[style];
    }
  }
  var interval = (1000 / 30);
  var minutes  = min_duration / (60 * 1000);
  if( minutes > 1 ){ interval = interval * minutes; }
  window.setTimeout( ya.move, interval);
}

