//
//  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 cPoint( nX, nY )
{
	this.x = nX
	this.y = nY
}

function cMover( hMoveObject )
{
	this.name = 'mover' + ( cMover.nCount++ )
	this.hMoveObject = hMoveObject
	this.aPath = new Array()
	this.aSpeed = 0
	this.nSteps = 0
	this.nPos = 0
	this.bMoving = false
	this.hTimeout = null
	this.sPx = ( bw.ns5 || bw.ie ) ? 'px' : ''
	this.obj = this.name
	eval( this.obj + '=this' )

	this.setMoveObject( hMoveObject )
}
cMover.nCount = 0

cMover.prototype.setMoveObject = function( hMoveObject )
{
	if( hMoveObject != null )
	{
		this.hMoveObject = hMoveObject
	}
}

cMover.prototype.moveTo = function( x, y )
{
	this.hMoveObject.style.left = x + this.sPx
	this.hMoveObject.style.top = y + this.sPx
}

cMover.prototype.onStop = function()
{
}

cMover.prototype.onStart = function()
{
}

cMover.prototype.play = function()
{
	if( !this.bMoving ) return
	this.onStart()
	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()
	}
}

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

cMover.getMoveObjectLeft = function( hObject )
{
	if( hObject.currentStyle )
	{
		return parseInt( hObject.currentStyle.left )
	}
	else
	{
		return parseInt( hObject.offsetLeft )
	}
}

cMover.getMoveObjectTop = function( hObject )
{
	if( hObject.currentStyle )
	{
		return parseInt( hObject.currentStyle.top )
	}
	else
	{
		return parseInt( hObject.offsetTop )
	}
}

cMover.prototype.slideTo = function( nX, nY, nInc, nSpeed )
{
	if( this.bMoving )
	{
		this.stop()
	}
	var dX = nX - cMover.getMoveObjectLeft( this.hMoveObject )
	var dY = nY - cMover.getMoveObjectTop( this.hMoveObject )
	if( this.initSlide( nX, nY, dX, dY, nInc, nSpeed ) )
	{
		this.bMoving = true
		this.play()
	}
}

cMover.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 = cMover.getMoveObjectLeft( this.hMoveObject )
	sY = cMover.getMoveObjectTop( this.hMoveObject )
	for( var nI=0; nI<this.nSteps-1; nI++ )
	{
		sX += deltaX
		sY += deltaY
		this.aPath[nI] = new cPoint( sX, sY )
	}
	this.aPath[nI] = new cPoint( nX, nY )
	return true
}


cMover.prototype.glideTo = function( nX, nY, nInc, nSpeed )
{
	if( this.bMoving )
	{
		this.stop()
	}
	var dX = nX - cMover.getMoveObjectLeft( this.hMoveObject )
	var dY = nY - cMover.getMoveObjectTop( this.hMoveObject )
	if( this.initGlide( nX, nY, dX, dY, nInc, nSpeed ) )
	{
		this.bMoving = true
		this.play()
	}
}

cMover.prototype.initGlide = function( nX, nY, dX, dY, nAngleInc, nSpeed )
{
	this.nPos = 0
	this.nSpeed = nSpeed
	this.nSteps = 0
	
	var nEndAngle = 90

	sX = cMover.getMoveObjectLeft( this.hMoveObject )
	sY = cMover.getMoveObjectTop( this.hMoveObject )
	
	var nI = nAngleInc
	while( nI < nEndAngle )
	{
		this.nSteps++
		nC = Math.sin( nI*Math.PI/180 )
		this.aPath[this.nSteps - 1] = new cPoint( sX+dX*nC, sY+dY*nC )
		nI += nAngleInc
	}
	this.aPath[this.nSteps - 1] = new cPoint( nX, nY )
	return true
}
