//
//  This script was created
//  by Mircho Mirev
//  mo /mo@momche.net/
//
//	:: feel free to use it BUT
//	:: if you want to use this code PLEASE send me a note
//	:: and please keep this disclaimer intact
//

function point( nX, nY )
{
	this.x = nX
	this.y = nY
}

function mover( hLayer )
{
	this.name = 'mover' + (mover.nCount++)
	this.aPath = new Array()
	this.aSpeed = 0
	this.nSteps = 0
	this.nPos = 0
	this.bMoving = false
	this.hTimeout = null
	this.obj = this.name
	eval( this.obj + '=this' )

	this.setLayer( hLayer )
}
mover.nCount = 0

mover.prototype.setLayer = function( hLayer )
{
	if( hLayer != null )
	{
		this.hLayer = hLayer
	}
}


mover.prototype.moveTo = function( x, y )
{
	sPx = ( bw.ns5 || bw.ie ) ? 'px' : ''
	this.hLayer.hElement.style.left = x + sPx
	this.hLayer.hElement.style.top = y + sPx
}

mover.prototype.onStop = function()
{
}

mover.prototype.onStart = function()
{
}

mover.prototype.play = function()
{
	if( !this.bMoving ) return
	this.onStart()
	//this.hLayer.moveTo( this.aPath[this.nPos].x, this.aPath[this.nPos].y )
	this.moveTo( this.aPath[this.nPos].x, this.aPath[this.nPos].y )
	this.nPos++
	if( this.nPos < this.nSteps )
	{
		this.hTimeout = setTimeout( this.obj+'.play()', this.nSpeed )
	}
	else
	{
		this.stop()
	}
}

mover.prototype.stop = function()
{
	clearTimeout( this.hTimeout )
	this.hTimeout = null
	this.bMoving = false
	this.onStop()
}

mover.prototype.slideTo = function( nX, nY, nInc, nSpeed )
{
	if( this.bMoving )
	{
		this.stop()
	}
	var dX = nX - this.hLayer.getLeft()
	var dY = nY - this.hLayer.getTop()
	if( this.initSlide( nX, nY, dX, dY, nInc, nSpeed ) )
	{
		this.bMoving = true
		this.play()
	}
}

mover.prototype.initSlide = function( nX, nY, dX, dY, nInc, nSpeed )
{
	this.nPos = 0
	this.nSpeed = nSpeed
	var nT = Math.sqrt( Math.pow(dX,2) + Math.pow(dY,2) ) / nInc
	this.nSteps = nT
	if( this.nSteps == 0 ) return false
	deltaX = dX/nT
	deltaY = dY/nT
	
	sX = this.hLayer.getLeft()
	sY = this.hLayer.getTop()
	for( var nI=0; nI<this.nSteps-1; nI++ )
	{
		sX += deltaX
		sY += deltaY
		this.aPath[nI] = new point( sX, sY )
	}
	this.aPath[nI] = new point( nX, nY )
	return true
}


mover.prototype.glideTo = function( nX, nY, nInc, nSpeed )
{
	if( this.bMoving )
	{
		this.stop()
	}
	var dX = nX - this.hLayer.getLeft()
	var dY = nY - this.hLayer.getTop()
	if( this.initGlide( nX, nY, dX, dY, nInc, nSpeed ) )
	{
		this.bMoving = true
		this.play()
	}
}

mover.prototype.initGlide = function( nX, nY, dX, dY, nAngleInc, nSpeed )
{
	this.nPos = 0
	this.nSpeed = nSpeed
	this.nSteps = 0
	
	var nW = Math.sqrt( Math.pow(dX,2) + Math.pow(dY,2) )
	if( nW == 0 ) return false

	var nEndAngle = 90

	sX = this.hLayer.getLeft()
	sY = this.hLayer.getTop()
	
	var nI = nAngleInc
	while( nI < nEndAngle )
	{
		this.nSteps++
		nC = nW*Math.sin( nI*Math.PI/180 )
		this.aPath[this.nSteps - 1] = new point( sX+dX*(nC/nW), sY+dY*(nC/nW) )
		nI += nAngleInc
	}
	this.aPath[this.nSteps - 1] = new point( nX, nY )
	return true
}

