Animation = function(imageUtil) {
	this.imageUtil = imageUtil;

	this.currentAngle = 0.0;
	this.t = null;
	this.step = null;
	this.timeSliceSize = 50;
	this.clockWise = true;
	this.remainingStepCount = 0;
  

  this.turnTo = function(toAngle, duration) {
    if (this.currentAngle < toAngle) {
      this.rotateTo(toAngle, duration, true);
    } else {
      this.rotateTo(toAngle, duration, false);
    }
  }
  
	this.rotateTo = function(toAngle, duration, clockWise) {
		Animation.logger.debug("Rotating to angle: " + toAngle + ", duration: "	+ duration + ", clockwise: " + clockWise);
		this.clockWise = clockWise;
		var degsToIterate = 0.0;

		if (this.clockWise) {
			if (toAngle >= this.currentAngle) {
				degsToIterate = toAngle - this.currentAngle;
			} else {
				degsToIterate = 360 - this.currentAngle + toAngle;
			}
		} else {
			if (toAngle <= this.currentAngle) {
				degsToIterate = this.currentAngle - toAngle;
			} else {
				degsToIterate = this.currentAngle + 360 - toAngle;
			}
		}

		Animation.logger.debug("There are " + degsToIterate
				+ " degrees to iterate over");
		this.step = degsToIterate * this.timeSliceSize / duration;
		Animation.logger.debug("The step size will be: " + this.step);
		this.remainingStepCount = duration / this.timeSliceSize;
		Animation.logger.debug("Animation total framecount: "
				+ this.remainingStepCount);
		var oThis = this;
		this.t = setTimeout( function() {
			oThis.timerCallback()
		}, oThis.timeSliceSize);
	}

	this.calculateNextAngle = function() {
		if (this.clockWise) {
			this.currentAngle += this.step;
		} else {
			this.currentAngle -= this.step;
		}
		this.remainingStepCount--;
	}

	this.timerCallback = function() {
		this.calculateNextAngle();
		imageUtil.rotate(this.currentAngle);

		if (this.remainingStepCount <= 0) {
			clearTimeout(this.t);
		} else {
			var oThis = this;
			this.t = setTimeout( function() {
				oThis.timerCallback();
			}, oThis.timeSliceSize);
		}
	}
};
Animation.logger = Logger.getLogger("Animation");
