//	
//**	Layer Animation Script
//			Functions for animating positionable content
//**	OIL, Incorporated
//**	May 15, 2002
//**	Version 0.2
//	
//	Requires LayerUtilityFunctions script
//
//	Revisions:
//	0.1		March 30, 2001		Original Version
//	0.2		May 15, 2002		Added the notifyWhenDone parameter to allow a function to be passed
//								that will be called when the moving of an object is complete. Fixed speed
//								calculation to properly use the speedFactor parameter.

var ANIMATE_SPEED = 60;	//	base animation speed in pixels an interval
var ANIMATE_INTERVAL = 50;	//	animation interval between movements, in milliseconds
var animateObjs = new Object();
var animateIntevalObj = null; var animateLayerCntr = 0;

//	Handles moving layers in straight lines
function animateLayerInALine(layerObj, endPos, speedFactor, notifyWhenDone){
	//	Function: Animates a layers movement. Moves the passed layer from 
	//	it's current position to the passed endPos, uses the speedFactor 
	//	to speed up (>1) or slow down (<1) movement.  The speedFactor parameter, 
	//	which is optional, must be greater than zero. speedFactor default is 1.  
	//	The endPos parameter must be a two item array in the form (x,y). The
	//	notifyWhenDone parameter is optional, if set to a function, it is called, passing 
	//	the name of the object being moved as it's only parameter when the movement is finished.
	if((!layerObj) || (typeof endPos != "object") || (!endPos.length) || (endPos.length!=2)){return;}
	//	If we are passed a speedFactor, and it is a number, multiply the ANIMATE_SPEED constant to 
	//	get the speed to be used, if not just use the ANIMATE_SPEED constant.
	var speed = (speedFactor && !isNaN(speedFactor)) ? (ANIMATE_SPEED * speedFactor):ANIMATE_SPEED;
	
	//	Convert ending screen position to coordinates relative to start pos
	curPos = [getLeft(layerObj),getTop(layerObj)]; // get relative current pos
	if((typeof curPos != "object") || (!curPos.length) || (curPos.length!=2)){return;}
	var relX = endPos[0]-curPos[0]; var relY = endPos[1]-curPos[1];
	
	//	Convert the animation speed to separate animation speeds for x and y
	ySpeed = (Math.round((speed * Math.cos(Math.atan2(relX,relY))) * 10) / 10);
	xSpeed = (Math.round((speed * Math.sin(Math.atan2(relX,relY))) * 10) / 10);
	if((ySpeed==0)&&(xSpeed==0)){  return;}
	layerid = (layerObj.id) ? layerObj.id : "layer" + animateLayerCntr;
	//	Add a sub-object to the animateObjs collection for this new object to be animated,
	//	if an existing object of the same name is being animated, this will cancel that animation
	animateObjs[layerid] = {layer:layerObj, xSpeed:xSpeed, ySpeed:ySpeed, x:curPos[0], y:curPos[1], endX:endPos[0], endY:endPos[1], cntr:0};
	//	If a function was passed for notifyWhenDone parameter, add it to the animateObjs sub-object
	if(typeof notifyWhenDone=='function')animateObjs[layerid].notifyFunction=notifyWhenDone;
	//	If there is no animation interval happening, set an interval object to recursively 
	//	call animateLayerInALine_Move() function at the set interval to move the specified object.
	if(animateIntevalObj == null) animateIntevalObj = setInterval("animateLayerInALine_Move()", ANIMATE_INTERVAL);
	//	Return the layer id of this animation object, will be passed to 
	//	notifyWhenDone function when the animation of this object stops
	return layerid;
	}
var timeCntr = 0;
//	Function that does each individual movement for 
//	the layer (called through a setInterval command)
function animateLayerInALine_Move(){
	if(!animateObjs){alert("no animate objects");clearInterval(animateIntevalObj);return;}
	var ao=null; var cntr=0; var layer; var setToEnd;var stillMoving=false;
	//	Step through animateObjs collection and move each object 
	for(aniObj in animateObjs){ao = animateObjs[aniObj];if(!ao){continue;}
		var newX = null; var newY = null; setToEnd=false;
		if((typeof ao!="object")||(!ao.layer)||(ao.cntr>3000)||ao.finished) continue;
		stillMoving=true;
		ao.cntr++; layer=ao.layer; cntr++; 
		
		//	Check if new X is past ending point, if so set setToEnd flag
		if(ao.xSpeed>0) {if(ao.x + ao.xSpeed >= ao.endX) setToEnd=true;}
		else {if((ao.x + ao.xSpeed <= ao.endX)&&(ao.xSpeed!=0)) setToEnd=true;}
		//	Check if new Y is past ending point
		if(ao.ySpeed>0) {if(ao.y + ao.ySpeed >= ao.endY) setToEnd=true;}
		else {if((ao.y + ao.ySpeed <= ao.endY)&&(ao.ySpeed!=0)) setToEnd=true;}
		
		//	If reached ending point, set to end and clear object, else increment position
		if(setToEnd) {newY = ao.endY; newX = ao.endX; animateObjs[aniObj]=null;}
		else {newX = ao.x += ao.xSpeed; newY = ao.y += ao.ySpeed;}
		
		//	Move Object
		setPosition(layer, newX, newY);
		
		//	If complete, remove animate object from collection
		if(setToEnd){
			//	If complete, call notifyFunction if function was set and pass 
			//	layer object id returned when animateLayerInALine was called
			if(ao.notifyFunction){ao.notifyFunction(aniObj);}
			ao.finished=true;
			}
		//alert(layer.id+" new x=" + newX+ "moved to " + getAbsLeft(layer));
		}
	if(!stillMoving){
		clearInterval(animateIntevalObj);
		animateObjs={};
		}
	//
	timeCntr++;
	//	If not moving anything, clear interval
	if(cntr==0){clearInterval(animateIntevalObj);animateIntevalObj = null;}
	}

//	Stops animation if interval object set
function stopAllAnimation(){
	clearInterval(animateIntevalObj);
	animateIntevalObj = null;
	animateObjs={};
	};