function AniObject( htmlContext, layerName, imageName, doLoop )
{
 this.objLayer = findLayer( layerName );
 this.imageName = imageName;
 this.jump = 1;
 this.way = new Array();
 this.htmlContext = htmlContext;
 this.timer = 0;
 this.currentWay = 0;
 this.lastXPos = 0;
 this.lastYPos = 0;
 this.isInit = false;
 this.doLoop = doLoop;
 this.speed=20;
 
 this.addWay = XaddWay;
 this.init = Xinit;
 this.stopAniObject = XstopAniObject;
 this.runAniObject = XrunAniObject;
 this.setJump = XsetJump;
 this.setLoopable = XsetLoopable;
}

function XsetJump( jump )
{
 this.jump = jump;
 return;
}

function XsetLoopable( doLoop )
{
 if ( doLoop == false )
 {
  this.doLoop = false;
 }
 else
 {
  this.doLoop = true;
 }
 return;
}

function FromTo( fromX, fromY, toX, toY, image, speed)
{
 this.fromX = fromX;
 this.fromY = fromY;
 this.toX = toX;
 this.toY = toY;
 this.image = preloadImage( image );
 
 this.speed = (!speed || speed == null ) ? 20 : speed;
}

function Xinit( doLoop )
{ 
 if ( !this.objLayer || this.objLayer == null)
 {
  window.status = "Keinen animierbaren Layer angegeben!";
  return;
 }
 this.setLoopable(doLoop);
 rollover( this.imageName, this.way[this.currentWay].image );
 this.speed = this.way[this.currentWay].speed;
 setLayerPosition( this.objLayer, this.way[this.currentWay].fromX, this.way[this.currentWay].fromY);
 setLayerVisible( this.objLayer, true ); 
 this.isInit = true;
 return;
}

function XaddWay( fromX, toX, fromY, toY, image, speed )
{
 if ( isNaN( fromX ) == true )
 {
  fromX = 0;
 }
 if ( isNaN( toX ) == true )
 {
  toX = 0;
 }
 if ( isNaN( fromY ) == true )
 {
  fromY = 0;
 }
 if ( isNaN( toY ) == true )
 {
  toY = 0;
 }  
 this.way[ this.way.length ] = new FromTo( fromX, toX, fromY, toY, image, speed);
 return;
}

function XrunAniObject( doLoop )
{
 if ( !this.htmlContext || this.htmlContext == null || this.way.length < 1 )
 {
  return; 
 }
 if ( this.isInit == false )
 {
  	this.init( doLoop );
  	if ( this.isInit == false )
 	{
		this.stopAniObject();
 	 	return;
 	} 
 }  
 var newX = 0, newY = 0;
 var changeX = false, changeY = false; 
 
 var currentObj = null;
 if ( this.way[this.currentWay].fromX > this.way[this.currentWay].toX ) 
 {
  currentObj = this.aniObjectBack;
  this.lastXPos = this.lastXPos - this.jump;
  newX = this.way[this.currentWay].fromX + this.lastXPos;
  if ( newX <= this.way[this.currentWay].toX )
  {
   changeX = true;
   newX = this.way[this.currentWay].toX;
  }
 }
 else
 {
  currentObj = this.aniObjectForward;
  this.lastXPos = this.lastXPos + this.jump;
  newX = this.way[this.currentWay].fromX + this.lastXPos;
  if ( newX >= this.way[this.currentWay].toX )
  {
   changeX = true;
   newX = this.way[this.currentWay].toX;
  }
 }
 
 if ( this.way[this.currentWay].fromY > this.way[this.currentWay].toY) 
 {
  currentObj = this.aniObjectDown;
  this.lastYPos = this.lastYPos - this.jump;
  newY = this.way[this.currentWay].fromY + this.lastYPos;
  if ( newY <= this.way[this.currentWay].toY )
  {
   changeY = true;
   newY = this.way[this.currentWay].toY;
  }
 }
 else
 {
  currentObj = this.aniObjectUp;
  this.lastYPos = this.lastYPos + this.jump;
  newY = this.way[this.currentWay].fromY + this.lastYPos;
  if ( newY >= this.way[this.currentWay].toY )
  {
   changeY = true;
   newY = this.way[this.currentWay].toY;
  }
 }  
 if ( changeX == true && changeY == true )
 {
  this.currentWay++;
  if ( this.currentWay >= this.way.length )
  {
   if (this.doLoop == false)
   {
    this.currentWay = 0;
	this.lastXPos = 0;
    this.lastYPos = 0; 
    this.stopAniObject();
	setLayerPosition( this.objLayer, newX, newY);
	return;
   }
   this.currentWay = 0;
  }
  this.lastXPos = 0;
  this.lastYPos = 0; 
  
  rollover( this.imageName, this.way[this.currentWay].image );
 }
 setLayerPosition( this.objLayer, newX, newY); 
 timer = setTimeout( this.htmlContext + ".runAniObject( )" , this.speed );
 return;
}

function XstopAniObject()
{  
 if ( this.timer ) 
 { 
  clearTimeout( this.timer );
 }
}
