///////////////////////////////////////////////////////
/////////////////////////////////////////////////////// 
 
function AnimationObject(elemID ,  AnimObName , CompositionFatherObjName)
{
	this.elemID 	  = elemID;
	this.pgElementObj = document.getElementById(elemID);
	
		if( (CompositionFatherObjName) && (CompositionFatherObjName != "") )
		{
			this.qualifiedName = CompositionFatherObjName + "." + AnimObName;
		}
		else
		{
			this.qualifiedName= AnimObName;
		}
	this.getCurrentX  = AnimationObject_getCurrentX;
	this.getCurrentY  = AnimationObject_getCurrentY;
	this.animateTo    = AnimationObject_animateTo;
	this.ConstructStepingScript = AnimationObject_ConstructStepingScript;
	this.interval	  			= null;
	
	
	
}

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////


function AnimationObject_getCurrentX()
{
	var X_Curr = document.getElementById(this.elemID).style.left ;
	X_Curr = X_Curr.replace("px" , "");
	X_Curr = parseInt(X_Curr);
	return parseInt(X_Curr);
}

function AnimationObject_getCurrentY()
{
	var Y_Curr = document.getElementById(this.elemID).style.top ;
	Y_Curr = Y_Curr.replace("px" , "");
	Y_Curr = parseInt(Y_Curr);
	return parseInt(Y_Curr);
}

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

function AnimationObject_animateTo( xTarget, yTarget, speed) 
{	
	if(this.interval)
	{
		clearInterval(this.interval);
	}
	var startX = this.getCurrentX(this.pgElementObj);
	var startY = this.getCurrentY(this.pgElementObj);
 
 	//alert(startX + "   " +startY );
 
    var xDelta = Math.abs(xTarget - startX);
    var yDelta = Math.abs(yTarget - startY);
    
	//alert(xDelta + "  " + yDelta);
	
    var vel = (speed > 0) ? speed : 1;
    
    var  pathLen = Math.sqrt(
							  (Math.pow((startX - xTarget), 2)) + 
							  (Math.pow((startY - yTarget), 2))
							 );
    
	//alert(pathLen);
	
    var xStep = parseInt(((xTarget - startX) / pathLen) * vel);
    var yStep = parseInt(((yTarget - startY) / pathLen) * vel);
   
   	if((startX == xTarget)&&(startY == yTarget))
	{
		return;
	}
		
		var StepingScript = this.ConstructStepingScript ( xStep 
													    , yStep
														, xTarget
													    , yTarget 
													    ) ;
		this.interval = setInterval( StepingScript  , 10 );
}

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

function AnimationObject_ConstructStepingScript( xStep 
											   , yStep 
											   , xTarget 
											   , yTarget
											   )
{

	// By constructing the the code that would be repeated in an owned to object method
	// on the fly, we working around the shortage of passing references to the function 
	// that would be repeated inside setInterval().
	// The "this" operator inside this function always refers to the window object.
	// Therfore I work around this constraint by entering a fully qualified name of the 
	// current object to the constructor. Wich then could be used inside the object owned 
	// function.
	
	var script = new String("");
	
	/// Hanging objects in local variables
	script +="var animObj    =" + this.qualifiedName + ";"
	+"var docElemRef = document.getElementById('"+ this.elemID +"');"
	
	+"var xCurrent = animObj.getCurrentX();"
	+"var yCurrent = animObj.getCurrentY();"
	
	//alert(xTarget);
			//+"alert('" +typeof xTarget  +"');"
			//+"alert(typeof xCurrent);"
	 +"var xReached = ( (Math.abs (" + xTarget + "- xCurrent) ) <=  (Math.abs(" + xStep +")) );"
	 +"var yReached = ( (Math.abs (" + yTarget + "- yCurrent) ) <=  (Math.abs(" + yStep +")) );"
		
		
		//+"alert('xCurrent = ' + xCurrent);"
		//+"alert('xTarget = '  +" + xTarget +");"
		
		//+"alert('xReached = ' + xReached);"
		//+"alert('yReached = ' + yReached);"
		
	+"if(! (xReached && yReached) )"
	+"{"
		+"if(! xReached)"
		+"{"
			+"docElemRef.style.left = (xCurrent +" + xStep +") + 'px';"
		+"}"
		
		+"if(! yReached)"
		+"{"
			+"docElemRef.style.top  = (yCurrent +" + yStep +") + 'px';"
		+"}"
		//+ "alert("+yTarget+");"
		//+ "alert(yCurrent);"
		//+ "alert(xCurrent);"
		//+ "alert(xReached);"
		 
	+"}"
	+"else"
	+"{"
		+ "docElemRef.style.left =" + xTarget + "+ 'px';"
		+ "docElemRef.style.top  =" + yTarget + "+ 'px';"
		+ "clearInterval(animObj.interval);"
	+"}"
	;	
	return script;
}