


function MovingObject(object)
{
  this.x = 0;
  this.y = 0;
  this.object = null;
  this.timer = null;
  this.finished = true;
  this.hardless =  1;
  this.acceleration =  0.8;
  this.range =  10;


  var self = this;
  function __construct()
  {
    var type =  typeof object;

    if(type == 'string')
    {
      self.object = document.getElementById(object);
    }
    else if(type  == 'object')
    {
      self.object = object;
    }
  }

  __construct();
}

function _MovingObject_setX(x)
{
  this.object.style.left = x + 'px';
  this.x = x;

  return x;
}

MovingObject.prototype.setX = _MovingObject_setX;

function _MovingObject_setY(y)
{
  this.object.style.top = y + 'px';
  this.y = y;

  return y;
}

MovingObject.prototype.setY = _MovingObject_setY;


function _MovingObject_getX()
{
  return this.x;
}

MovingObject.prototype.getX = _MovingObject_getX;


function _MovingObject_getY()
{
  return this.y;
}

MovingObject.prototype.getY = _MovingObject_getY;


function _MovingObject_go(x, y, callBackFunc)
{
  var self  = this;

  var nowX  = this.setX(self.object.offsetLeft),
      nowY  = this.setY(self.object.offsetTop),
      distance = getDistance(nowX, x, nowY, y);

  clearInterval(self.timer);

  if(distance)
  {
    var sin = (y - nowY) / distance,
        cos = (x - nowX) / distance,
        limit=Math.atan(this.range),
        halfLimit=limit*2,
        run=-this.range,
        oldX=nowX,
        oldY=nowY,
        acceleration=this.acceleration*this.hardless;

    this.timer = setInterval(go, 15);
    this.finished = false;
  }

  function getDistance(x1, x2, y1, y2)
  {
    return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
  }

  function go()
  {
	 kx=((Math.atan(run * self.hardless)+limit)/halfLimit)*distance;
     nowX = oldX + cos * kx;
     nowY = oldY + sin * kx;

     run += acceleration;
     if(run>self.range)
     {
       clearInterval(self.timer);
       nowX = x;
       nowY = y;
       self.finished = true;
     }

     self.setX(nowX);
     self.setY(nowY);

     if(self.finished)
     {
       if(callBackFunc)
       {
         try
         {
           callBackFunc(self);
         }
         catch(e){ alert(e); }
       }
     }
  }

}

MovingObject.prototype.go = _MovingObject_go;