// requires:
//   DHTMLAPI.js
//   initDrag.js

function Blimp() {
  this.obj = null;
  this.offset = $V([0,0]);
  this.position = $V([0,0]);
  this.course = $V([0,0]);
  this.waypoint = $V([0,0]);
  this.interval = null;
  this.debug = null;
}
Blimp.prototype = {
  setReference: function(obj) {
    // this.debugWrite("setReference");
    this.obj = obj;
  },
  
  getReference: function() {
    return this.obj;
  },
  
  setPosition: function(x,y) {
    this.position.setElements([x, y]);
    this.offset.setElements([x, y]);
    // this.debugWrite("setPosition");
  },
  
  setWaypoint: function(x,y) {
    this.waypoint = $V([x,y]);
  },
  
  randomizeWaypoint: function() {
    // this.debugWrite("randomizeWaypoint");
    var xMin = - 2*this.obj.offsetWidth;
    var yMin = - 2*this.obj.offsetHeight;
    var xMax = getInsideWindowWidth() - this.obj.offsetWidth;
    var yMax = getInsideWindowHeight() - this.obj.offsetHeight;
    this.waypoint = $V([Math.floor(Math.random()*(xMax-xMin))+xMin,
                        Math.floor(Math.random()*(yMax-yMin))+yMin]);
  },
  
  setCourseToWaypoint: function() {
    // this.debugWrite("setCourseToWaypoint");
    var w = this.waypoint;
    var v = w.subtract(this.position);
    this.course = v.toUnitVector(); 
  },
  
  start: function() {
    // this.debugWrite("start");
    if(!this.interval) {
      this.interval = setInterval("blimp.run()", 50);
    }
  },
  
  stop: function() {
    // this.debugWrite("stop");
    clearInterval(this.interval);
    this.interval = null;
  },
  
  run: function() {
    // this.debugWrite("run");
    // see where we are
    // adjust if item was dragged
    var imagePos = $V([this.obj.offsetLeft, this.obj.offsetTop]),
        displacement = imagePos.subtract(this.offset);
    if(displacement.modulus() > 0) {
      this.position = imagePos;
    }
    // determine movement of item
    displacement = this.position.subtract(this.waypoint);
    if(displacement.modulus() < 20) {
      // the probability blimp creates a new waypoint
      // Requirement now is to do nothing
      // this.randomizeWaypoint();
      this.stop();
    }
    this.setCourseToWaypoint();
    this.position = this.position.add(this.course);
    // move item on screen
    offsetLeft = Math.floor(this.position.e(1));
    offsetTop = Math.floor(this.position.e(2));
    this.offset.setElements([offsetLeft, offsetTop]);
    shiftTo(this.obj, offsetLeft, offsetTop);
  },
  
  debugWrite: function(n) {
    if(this.debug) {
      var s = (n)? "-"+n+"-" : "";
      s =s+" offset:" + this.offset.inspect()
        +  " waypoint:" + this.waypoint.inspect()
//      +  " position:" + this.position.inspect()
        +  " course:" + this.course.inspect()
        +  " interval:" + this.interval;
      this.debug.value = s;
    }
  },
  
  debugTo: function(elemID) {
    var obj = getRawObject(elemID);
    this.debug = obj;
  }
};

Blimp.create = function(elemID) {
  var blimp = new Blimp();
  var obj = getRawObject(elemID);
  blimp.setReference(obj);
  //blimp.setPosition(obj.offsetLeft, obj.offsetTop);
  //don't use setPosition, use the .css instead
  //blimp.setPosition(0, 0);
  //blimp.randomizeWaypoint();
  var xMax = getInsideWindowWidth() - blimp.obj.offsetWidth;
  blimp.setWaypoint(xMax,200);
  return blimp;
};

initDHTMLAPI();
initDrag();

// floating blimp
var blimp = Blimp.create("blimpWrap");
var blimpWrapper = blimp.getReference();

// start blimp
blimpWrapper.style.visibility="visible";
blimp.start();


