/* Function in this file:
BB_LocObject(selObj, CurrentLoc)				returns x,y location of object into an array
BB_StepObject(selObj, mLeft, mTop)				moves object to new location
BB_MoveObject(selObj, destX, destY, BB_CalcPath)		moves object over time
BB_StopMove()							stops a move
*/

// Setup variables for moving
var BB_Steppings = new Array();
var BB_MovingObject = "";
var BB_MaxSteps = 20;
var BB_Steppings1 = new Array();
var BB_MovingObject1 = "";
var BB_MaxSteps1 = 20;
var BB_Steppings2 = new Array();
var BB_MovingObject2 = "";
var BB_MaxSteps2 = 20;

// locate an object on the screen. CurrentLocX,Y are the names of global variables
function BB_LocObject(selObj)
	{
	var strObj, strSet
	var CurrentLoc = new Array();
	strObj = docObj +  '.' + selObj + styleObj;
	strSet = "CurrentLoc[0] = parseInt(" + strObj + ".left)";
	eval(strSet);
	strSet = "CurrentLoc[1] = parseInt(" + strObj + ".top)";
	eval(strSet);
	return CurrentLoc;
	}

// moves an object in a single step
function BB_StepObject(selObj, mLeft, mTop)
	{
	var evalStmt;

	evalStmt = docObj +  '.' + selObj + styleObj;

	domObj = eval(evalStmt);
	domObj.top = mTop;
	domObj.left = mLeft;
	}

/* Move an object over time.  This function requires that the caller have created a CalcPath function and pass its name as a string.
*/
function BB_MoveObject(selObj, destX, destY, BB_CalcPath, TimeDelay)
	{
	var NumSteps;
	var StepFunction;
	var EndFunction;
	var evalStmt;
	
	if (BB_MovingObject != "")
		return;
/* Start by getting the current location of the object.  LocObject will place the current location into the 
first (#0) spot in the array. BB_Steppings[0] = x location, while BB_Steppings[0] = y location
*/
	BB_Steppings[0] = new Array();
	BB_Steppings[0][0] = 0;
	BB_Steppings[0][1] = 0;
	BB_Steppings[0] = BB_LocObject(selObj);
/* CalcPath will create an array of NumSteps steps that is the path for the object.  This must be provided for each 
application or website. The array will be of the form BB_Steppings[stepnumber][0 = xval, 1 = yval].
*/
	evalStmt = "NumSteps = " + BB_CalcPath + "(destX, destY)";
	eval(evalStmt);

	StepFunction = 'BB_StepObject("' + selObj + '", BB_Steppings[BB_Count][0], ' + ' BB_Steppings[BB_Count][1])';
	EndFunction = 'BB_StepObject("' + selObj + '", ' + destX + ', ' + destY + '); BB_MovingObject = "";';
	BB_Count = 1;
	BB_MovingObject = selObj;	//name of moving object
	BB_RepStop = 1;			//allows rep controller to go

	BB_RepController(NumSteps, StepFunction, EndFunction, TimeDelay);
	}

/* Move two objects over time.  This function requires that the caller have created a CalcPath function and pass its name as a string.
*/
function BB_Move2Objects(selObj1, destX1, destY1, selObj2, destX2, destY2, BB_CalcPath, TimeDelay)
	{
	var NumSteps1, NumSteps2;
	var StepFunction;
	var EndFunction;
	var evalStmt;
	
//	if (BB_MovingObject1 != "")
//		return;

/* Start by getting the current location of the object.  LocObject will place the current location into the 
first (#0) spot in the array. BB_Steppings1X[0] = x location, while BB_Steppings1Y[0] = y location
*/
	BB_Steppings1[0] = new Array();
	BB_Steppings1[0][0] = 0;
	BB_Steppings1[0][1] = 0;
	BB_Steppings1[0] = BB_LocObject(selObj1);
	BB_Steppings2[0] = new Array();
	BB_Steppings2[0][0] = 0;
	BB_Steppings2[0][1] = 0;
	BB_Steppings2[0] = BB_LocObject(selObj2);
/* CalcPath will create an array of NumSteps steps that is the path for the object.  This must be provided for each 
application or website. The array will be of the form BB_Steppings1[stepnumber][0 = xval, 1 = yval].
*/
	evalStmt = "NumSteps1 = " + BB_CalcPath + "1(destX1, destY1)";
	eval(evalStmt);
	evalStmt = "NumSteps2 = " + BB_CalcPath + "2(destX2, destY2)";
	eval(evalStmt);

	StepFunction = 'BB_StepObject("' + selObj1 + '", BB_Steppings1[BB_Count1][0], ' + ' BB_Steppings1[BB_Count1][1]);';
	StepFunction += 'BB_StepObject("' + selObj2 + '", BB_Steppings2[BB_Count1][0], ' + ' BB_Steppings2[BB_Count1][1])';
	EndFunction = 'BB_StepObject("' + selObj1 + '", ' + destX1 + ', ' + destY1 + '); BB_MovingObject1 = "";';
	EndFunction += 'BB_StepObject("' + selObj2 + '", ' + destX2 + ', ' + destY2 + '); BB_MovingObject2 = "";';

	BB_Count1 = 1;
	BB_MovingObject1 = selObj1;	//name of moving object
	BB_RepStop1 = 1;			//allows rep controller to go

	BB_RepController1(NumSteps1, StepFunction, EndFunction, TimeDelay);
	}

function BB_StopMove()
	{
	if (BB_MovingObject != "")	//if MovingObject is empty, then nothing is moving
		{
		BB_RepStop = 0;	//stop RepController
		BB_MovingObject = "";	//no moving object
		}
	}
function BB_StopMove1()
	{
	if (BB_MovingObject != "")	//if MovingObject is empty, then nothing is moving
		{
		BB_RepStop1 = 0;	//stop RepController
		BB_MovingObject1 = "";	//no moving object
		}
	}
	
/* The next few of functions are sample functions for calculating a path. Caller must supply the
variable BB_MaxSteps1 (usually as a global), the number of steps to be taken.
*/
function BB_PathLinear(destX, destY)
	{
	var diffX, diffY;
	var proportionX, proportionY;
	var kk;

	diffX = destX - BB_Steppings[0][0];
	diffY = destY - BB_Steppings[0][1];
	for (kk = 1; kk <= BB_MaxSteps; kk++)
		{
		proportionX = (kk * diffX)/BB_MaxSteps;
		proportionY = (kk * diffY)/BB_MaxSteps;
		BB_Steppings[kk] = new Array();
		BB_Steppings[kk][0] = BB_Steppings[0][0] + Math.round(proportionX);
		BB_Steppings[kk][1] = BB_Steppings[0][1] + Math.round(proportionY);
		}
	return BB_MaxSteps;
	}
function BB_PathLinear1(destX, destY)
	{
	var diffX, diffY;
	var proportionX, proportionY;
	var kk;

	diffX = destX - BB_Steppings1[0][0];
	diffY = destY - BB_Steppings1[0][1];
	for (kk = 1; kk <= BB_MaxSteps1; kk++)
		{
		proportionX = (kk * diffX)/BB_MaxSteps1;
		proportionY = (kk * diffY)/BB_MaxSteps1;
		BB_Steppings1[kk] = new Array();
		BB_Steppings1[kk][0] = BB_Steppings1[0][0] + Math.round(proportionX);
		BB_Steppings1[kk][1] = BB_Steppings1[0][1] + Math.round(proportionY);
		}
	return BB_MaxSteps1;
	}
function BB_PathLinear2(destX, destY)
	{
	var diffX, diffY;
	var proportionX, proportionY;
	var kk;
	
	diffX = destX - BB_Steppings2[0][0];
	diffY = destY - BB_Steppings2[0][1];
	for (kk = 1; kk <= BB_MaxSteps2; kk++)
		{
		proportionX = (kk * diffX)/BB_MaxSteps2;
		proportionY = (kk * diffY)/BB_MaxSteps2;
		BB_Steppings2[kk] = new Array();
		BB_Steppings2[kk][0] = BB_Steppings2[0][0] + Math.round(proportionX);
		BB_Steppings2[kk][1] = BB_Steppings2[0][1] + Math.round(proportionY);
		}
	return BB_MaxSteps2;
	}
	
	
/* This one covers half the distance to the destination each time.  To slow it down, change the 
fract variable to 3 (for one-third) or higher.
*/
function BB_PathGeometric(destX, destY)
	{
	var BB_MaxSteps1 = 20;
	var FractSteps = 2;
	var diffX, diffY;
	var proportionX, proportionY;
	var startX, startY;
	var kk;
	
	startX = BB_Steppings1[0][0];
	startY = BB_Steppings1[0][1];
	
	for (kk = 1; kk <= BB_MaxSteps1; kk++)
		{
		proportionX = (destX - startX)/FractSteps;
		proportionY = (destY - startY)/FractSteps;
		BB_Steppings1[kk] = new Array();
		BB_Steppings1[kk][0] = startX + Math.round(proportionX);
		BB_Steppings1[kk][1] = startY + Math.round(proportionY);
		startX = BB_Steppings1[kk][0];
		startY = BB_Steppings1[kk][1];
		}
	
	return BB_MaxSteps1;
	}
	
/* This one does stutter steps. You must set the threshold value between 0 and 1 to determine the frequency of 
stutter steps.  A high number, close to 1, is recommended.
*/
function BB_PathStutter(destX, destY)
	{
	var diffX, diffY;
	var proportionX, proportionY;
	var kk;
	var testValue;
	var threshold = 0.7;
	
	diffX = BB_Steppings1[0][0] - destX;
	diffY = BB_Steppings1[0][1] - destY;
	
	for (kk = 1; kk <= BB_MaxSteps1; kk++)
		{
		proportionX = (kk * diffX)/BB_MaxSteps1;
		proportionY = (kk * diffY)/BB_MaxSteps1;
		BB_Steppings1[kk] = new Array();
		BB_Steppings1[kk][0] = BB_Steppings1[0][0] + Math.round(proportionX);
		BB_Steppings1[kk][1] = BB_Steppings1[0][1] + Math.round(proportionY);
		}
	for (kk = 3; kk < BB_MaxSteps1; kk++)
		{
		testValue = Math.random();
		if (testValue > threshold)
			{
			BB_Steppings1[kk + 1][0] = BB_Steppings1[kk][0];
			BB_Steppings1[kk + 1][1] = BB_Steppings1[kk][1];
			BB_Steppings1[kk][0] = BB_Steppings1[kk - 2][0];
			BB_Steppings1[kk][1] = BB_Steppings1[kk - 2][1];
			kk++;
			}
		}
	
	return BB_MaxSteps1;
	}
	
/* This one works like PathGeometric, but it starts out slow, then speeds up, then slows down again.
As before, it covers half the distance to the destination each time.  To slow it down, change the 
fract variable to 3 (for one-third) or higher.
*/
function BB_PathBell(destX, destY)
	{
	var FractSteps = 2;
	var diffX, diffY;
	var proportionX, proportionY;
	var startX, startY;
	var midX, midY;
	var kk, midkk;
	
	startX = BB_Steppings1[0][0];
	startY = BB_Steppings1[0][1];
	midX = (destX + startX) / 2;
	midY = (destY + startY) / 2;
	midkk = Math.round(BB_MaxSteps1/2);
	
	for (kk = midkk - 1; kk > 0; kk--)
		{
		proportionX = (midX - startX)/FractSteps;
		proportionY = (midY - startY)/FractSteps;
		BB_Steppings1[kk] = new Array();
		BB_Steppings1[kk][0] = midX + Math.round(proportionX);
		BB_Steppings1[kk][1] = midY + Math.round(proportionY);
		midX = BB_Steppings1[kk][0];
		midY = BB_Steppings1[kk][1];
		}

	midX = (destX + startX) / 2;
	midY = (destY + startY) / 2;
	BB_Steppings1[midkk] = new Array();
	BB_Steppings1[midkk][0] = midX;
	BB_Steppings1[midkk][1] = midY;

	for (kk = midkk + 1; kk <= BB_MaxSteps1; kk++)
		{
		proportionX = (destX - midX)/FractSteps;
		proportionY = (destY - midY)/FractSteps;
		BB_Steppings1[kk] = new Array();
		BB_Steppings1[kk][0] = midX + Math.round(proportionX);
		BB_Steppings1[kk][1] = midY + Math.round(proportionY);
		midX = BB_Steppings1[kk][0];
		midY = BB_Steppings1[kk][1];
		}
	
	return BB_MaxSteps1;
	}
	
