// depends on common.js: QQ
QQ.Animator = function(){

  return {
  	fade: function (objectID,settings){
			
			if (settings === undefined) settings = {}
			
			// get the object
			var object = obj(objectID);
			
			var startAlpha = settings.startAlpha;
			var endAlpha = settings.endAlpha;
			var interval = settings.interval;
			var duration = settings.duration;
			var callback = settings.callback;
			
			//// handle defaults
			if (startAlpha === undefined) startAlpha = 0;
			if (endAlpha === undefined) endAlpha = 100;	
			if (interval === undefined) interval = 10;
			if (duration === undefined) duration = 100;
	
			// fade in or out
			var fadeType = 1; // fade in
			if (startAlpha > endAlpha) fadeType = -1; // fade out
			
			// how many steps to do
			var steps = duration / interval;
			var currentStep = 1;
			
			// if values are given in percent set them in [0,1] range
			if (startAlpha > 1) startAlpha = startAlpha / 100;
			if (endAlpha > 1) endAlpha = endAlpha / 100;
			var stepAlpha = QQ.Math.round((endAlpha - startAlpha) / steps,4);
			
			object.style.opacity = startAlpha;
			
			// set fade interval
			var fadeInterval = setInterval(fadeObject,interval);
			
			
			
			// fade the object
			function fadeObject(){
				object.style.opacity = startAlpha + currentStep*stepAlpha;				
				if (Math.abs(currentStep) >= steps){
					clearInterval(fadeInterval);
					if (callback !== undefined) callback();
				}
				++currentStep;
			}
		},
		move: function (objectID,settings){

			if (settings === undefined) settings = {}
			
			// get the object
		  var object = obj(objectID);
			
			var startX = settings.startX;
			var startY = settings.startY;
			var endX = settings.endX;					// obligatory
			var endY = settings.endY;					// obligatory
			var interval = settings.interval;	
			var duration = settings.duration;
			var callback = settings.callback;

			//// handle defaults
			// position of object (has to be set at object)
			if (startX === undefined) startX = parseInt(object.style.left);  
			if (startY === undefined) startY = parseInt(object.style.top);
			// interval and duration
			if (interval === undefined) interval = 10;
			if (duration === undefined) duration = 100;
		
			// how many steps to do
			var steps = duration / interval;
			var currentStep = 1;
			
			object.style.position = "absolute";
			object.style.left = startX + "px";
			object.style.top = startY + "px";
			
			// calculate interval
			var stepX = (endX - startX) / steps;
			var stepY = (endY - startY) / steps;

			// set move interval
			var moveInterval = setInterval(moveObject,interval);

			// move the object
			function moveObject(){
				//object.style.left = parseInt(object.style.left) + stepX + "px";
				//object.style.top = parseInt(object.style.top) + stepY + "px";
				object.style.left = (startX + currentStep*stepX) + "px";
				object.style.top = (startY + currentStep*stepY) + "px";				
				if (currentStep >= steps){
					clearInterval(moveInterval);
					if (callback !== undefined) callback();
				}
				++currentStep;
			}
		}
  };
}();
