var wave1Object = null;
var theSmoothTimer = null;

schedule("waves", mouseMoveInit);




function animationSpeedTest()
{
	var date = new Date();
	var self = this;
	
	this.iteration = 0;
	this.startTime = date.getTime();
	
	setTimeout(function(){self.iterate(); return true;}, 25);
	
	return true;
}




animationSpeedTest.prototype.iterate = function()
{
	var self = this;
	
	if (this.iteration < 100)
	{
		this.iteration++;
		setTimeout(function(){self.iterate(); return true;}, 25);
	}
	else
	{
		var date = new Date();
		
		if (date.getTime() > (this.startTime + 4000))
		{
			animationEnabled = false;
		}
	}
	
	return true;
}




function mouseMoveInit()
{
	new animationSpeedTest();

	wave1Object = new scrollingBackground("wave1", -12, 135, 0);
	wave2Object = new scrollingBackground("wave2", 0, 150, 25);
	wave3Object = new scrollingBackground("wave3", 0, 167, 50);

	document.getElementsByTagName("body")[0].onmousemove = scrollCoords;

	return true;
}




function scrollingBackground(objectID, backgroundX, backgroundY, multiplier)
{
	this.associatedNode = document.getElementById(objectID);
	this.backgroundXOffset = backgroundX;
	this.currPosX = null;
	this.currPosY = backgroundY;
	this.currVelocity = 0;
	this.goalX = 0;
	this.highlightNode1 = document.getElementById(objectID + "B");
	this.highlightNode2 = document.getElementById(objectID + "C");
	this.movementMultiplier = 1;
	this.moving = false;
	this.name = objectID + "Object";
	this.timer = null;
	
	if (multiplier != null)
	{
		this.movementMultiplier = multiplier;
	}
	
	return true;
}




scrollingBackground.prototype.approachPositionX = function(mouseX)
{
	this.goalX =  mouseX + this.movementMultiplier;
	
	if (!this.moving)
	{
		this.move();
	}

	return true;
}




scrollingBackground.prototype.move = function ()
{
	var incrementX = 0;
	
	this.moving = true;
	
	if (this.currPosX == null)
	{
		this.currPosX = this.highlightNode1.offsetLeft + 125;
	}
	
	incrementX = (this.goalX - this.currPosX) / 20;
	
	/* Decelerate on reversed velocity */
	if (incrementX * this.currVelocity < -1)
	{
		incrementX = this.currVelocity / 2;
	}
	
	this.currVelocity = incrementX;
	
	if (Math.abs(incrementX) > 0.005)
	{
		this.highlightNode1.style.backgroundPosition = -parseInt((this.currPosX + incrementX - 125)) + this.backgroundXOffset + "px " + this.currPosY + "px";
		this.highlightNode1.style.left = parseInt(this.currPosX + incrementX - 125) + "px ";
		this.highlightNode2.style.backgroundPosition = -parseInt(this.currPosX + incrementX - 75) + this.backgroundXOffset + "px " + this.currPosY + "px";
		this.highlightNode2.style.left = parseInt(this.currPosX + incrementX - 75) + "px ";
		
		this.currPosX = this.currPosX + incrementX;
	
		this.timer = setTimeout(this.name + ".move()", 25);
	}
	else
	{
		this.moving = false;
	}

	return true;	
}




function scrollBackgrounds(mouseX)
{
	wave1Object.approachPositionX(mouseX);
	wave2Object.approachPositionX(mouseX);
	wave3Object.approachPositionX(mouseX);
	
	return true;
}





function scrollCoords(e)
{
	if (animationEnabled)
	{
		var mouseX = 0;

		if (e)
		{
			mouseX = e.clientX;
		}
		else
		{
			mouseX = event.clientX;
		}

		scrollBackgrounds(mouseX);
	}
	
	return true;
}
