var Actor = function (img)
{
  //console.log(img);
  this.img        = img;
  this.x          = 0;
  this.y          = 0;
  this.centerX    = 0;
  this.centerY    = 0;
  this.rotation   = 0;
  this.scaleX     = 1;
  this.scaleY     = 1;
  this.width      = 0;
  this.height     = 0;
  this.linkAction = null;
  // init
  this.initLink();
}


Actor.MILSEC_JUMP_DELAY = 410;


Actor.prototype.initLink = function ()
{
  var parent = this.img.parentNode;
  if (parent.tagName.toUpperCase() === 'A')
  {
    this.linkAction = function (ev)
    {
      if ($(parent).triggerHandler('click') === false)
      {
        return ev;
      }
      if ((parent.onclick instanceof Function) && (parent.onclick(ev) === false))
      {
        return ev;
      }
      window.setTimeout(
        function(){Controller.jumpPage(parent.href);}, 
        Actor.MILSEC_JUMP_DELAY
      );
    }
  }
}


Actor.prototype.draw = function (context)
{
  var cx = this.centerX;
  var cy = this.centerY;
  var sx = this.scaleX;
  var sy = this.scaleY;
  //console.log(this.centerX, this.centerY, this.rotation, this.x, this.y);
  context.save();
  // <-- world matrix pushed
  if ((sx !== 1) || (sy !== 1))
  {
    context.scale(sx, sy);
  }
  context.translate(this.x + cx, this.y + cy);
  context.rotate(this.rotation);
  context.translate(-cx, -cy);
  context.drawImage(this.img, 0, 0);
  // world matrix pops -->
  context.restore();
}


Actor.prototype.onHover = function (ev, adjustX, adjustY)
{
  return (this.linkAction && this.checkHit(ev.clientX, ev.clientY, adjustX, adjustY));
}


Actor.prototype.onClick = function (ev, adjustX, adjustY)
{
  var result = this.checkHit(ev.clientX, ev.clientY, adjustX, adjustY);
  if (result)
  {
    this.linkAction();
  }
  return result;
}


Actor.prototype.checkHit = function (x, y, adjustX, adjustY)
{
  //console.log(ev);
  adjustX    = adjustX || 0;
  adjustY    = adjustY || 0;
  var result = false;
  var sx     = this.scaleX;
  var sy     = this.scaleY;
  var left   = adjustX  + (this.x      * sx);
  var right  = left     + (this.width  * sx);
  var top    = adjustY  + (this.y      * sy);
  var bottom = top      + (this.height * sy);
  if ( 
    (left < x) 
    && (x < right)
    && (top < y)
    && (y < bottom)
  )
  {
    result = true;
  }
  return result;
}


