/*
 * Licensed through Creative Commons - Share and Share Alike
 * Copyright: Virtual Synthesis
 */

var REFRESH_RATE = 10;
var MIN_X_ANIM_RANGE = -1000;
var SUPER_LOW_X_ANIM_RANGE = -700;
var LOW_X_ANIM_RANGE = -400;
var MED_X_ANIM_RANGE = -200;
var HIGH_X_ANIM_RANGE = 0;
var SUPER_HIGH_X_ANIM_RANGE = 300;
var MAX_X_ANIM_RANGE = 8000;

function getCoordsDebugStr() {
	var debugStr =
		"Coords name= " + this.coordsName + ": " +
		"\n x= " + this.x +
		"\n y= " + this.y +
		"\n z= " + this.z;
	//alert(debugStr);
	return debugStr;
}

function dumpCoords() {
	//alert("In dumpCoords()");
	this.debugDivEl.innerHTML = this.debugDivEl.innerHTML + this.getCoordsDebugStr();
}

function coords3d( argDebugDivName, argThisName, argX, argY, argZ ) {
	this.debugDivName = argDebugDivName;
	this.debugDivEl = document.getElementById(this.debugDivName);
	this.coordsName = argThisName;
	this.x = argX;
	this.y = argY;
	this.z = argZ;
	this.dumpCoords = dumpCoords;
	this.getCoordsDebugStr = getCoordsDebugStr;
}

// needs work
function getDivLoc( argDebugDivName, argDivName ) {
	var divLoc = null;
	this.divEl = document.getElementById(argDivName);
	if (divEl) {
		divLoc = new coords3d(argDebugDivName,
			argDivName,
			divEl.style.left,
			divEl.style.top,
			divEl.style.z-index
			);
	}
	return divLoc;
}

function getTrajectoryDebugStr() {
	var debugStr =
		"<ul>Trajectory name= " + this.trajectoryName + ": " +
		"\n<li> speed = " + this.speed.getCoordsDebugStr() + "</li>" +
		"\n<li> velocity = " + this.velocity.getCoordsDebugStr() + "</li>" +
		"\n<li> curLoc = " + this.curLoc.getCoordsDebugStr(); + "</li>" +
		"</ul>";
	//alert(debugStr);
	return debugStr;
}

function dumpTrajectory() {
	this.debugDivEl.innerHTML = this.debugDivEl.innerHTML + "<br/>" + this.getTrajectoryDebugStr();
}

function trajectory( argDebugDivName, argThisName, argSpeed, argVelocity, argCurLoc ) {
//	this.debugStr = "";	// "<br/>trajectory: " + argThisName;
	this.debugDivName = argDebugDivName;
	this.debugDivEl = document.getElementById(this.debugDivName);
	this.trajectoryName = argThisName;
	this.trajectoryNumber = 0;
	this.speed = argSpeed;
	this.velocity = argVelocity;
	this.curLoc = argCurLoc;
	this.traject = traject;
	this.tetherX = tetherX;
	this.dumpTrajectory = dumpTrajectory;
	this.getTrajectoryDebugStr = getTrajectoryDebugStr;
}

function traject() {
	//alert ("In traject()");
	this.trajectoryNumber++;
	var newLoc = new coords3d(this.debugDivName, "T" + this.trajectoryNumber, this.curLoc.x, this.curLoc.y, this.curLoc.z);
	this.tetherX();
	newLoc.x = this.curLoc.x + (this.speed.x * this.velocity.x);
	newLoc.y = this.curLoc.y + (this.speed.y * this.velocity.y);
	newLoc.z = this.curLoc.z + (this.speed.z * this.velocity.z);
	this.curLoc = newLoc;
}

function tetherX() {
	if (this.curLoc.x > MAX_X_ANIM_RANGE) {
		this.speed.x = 10;
	} else if (this.curLoc.x > SUPER_HIGH_X_ANIM_RANGE) {
		this.speed.x = 8;
	} else if (this.curLoc.x > HIGH_X_ANIM_RANGE) {
		this.speed.x = 5;
	} else if (this.curLoc.x > MED_X_ANIM_RANGE) {
		this.speed.x = 1;
	} else if (this.curLoc.x > LOW_X_ANIM_RANGE) {
		this.speed.x = 0.8;
	} else if (this.curLoc.x > SUPER_LOW_X_ANIM_RANGE) {
		this.speed.x = 2;
	} else if (this.curLoc.x > MIN_X_ANIM_RANGE) {
		this.speed.x = 5;
	} else {
		this.speed.x = 10;
	}
}

function getAnimageDebugStr() {
	var debugStr =
		"<ul>Animage name= " + this.animageName + ": " +
		"\n<li> imageName= " + this.imageName + "</li>" +
		"\n<li> imageDivName= " + this.imageDivName + "</li>" +
		"\n<li> animations= " + this.animations + "</li>" +
		"\n<li> trajectory= " + this.trajectory.getTrajectoryDebugStr() + "</li>" +
		"</ul>";
	//alert(debugStr);
	return debugStr;
}

function dumpAnimage() {
	if (this.debugDivEl) {
		var newinnerHTML = this.debugDivEl.innerHTML + this.getAnimageDebugStr();
		this.debugDivEl.innerHTML = newinnerHTML;
	}
}

function animate() {
	//alert("In animage.animate()");
	this.animations++;
	//alert("In animage.animate()\nthis.animations= " + this.animations);
	//this.dumpAnimage();
	if (this.trajectory) {
		this.trajectory.traject();
		this.imageDivEl.style.left = this.trajectory.curLoc.x  + 'px';
		this.imageDivEl.style.top = this.trajectory.curLoc.y + 'px';
//		this.imageDivEl.style.z-index = this.trajectory.curLoc.z;	// can't seem to set z-index???
	}
	return false;
}

function animage( argMainDivName, argDebugDivName, argAnimageName, argImageName, argImageDivName, argTrajectory ) {
//alert("In animage constructor...");
	this.mainDivName = argMainDivName;
	this.mainDivEl = document.getElementById(this.mainDivName);
	this.debugDivName = argDebugDivName;
	this.debugDivEl = document.getElementById(this.debugDivName);
	this.animageName = argAnimageName;
	this.imageName = argImageName;
	this.imageEl = document.getElementById(this.imageName);
	this.imageDivName = argImageDivName;
	this.imageDivEl = document.getElementById(this.imageDivName);
	this.trajectory = argTrajectory;
	this.animate = animate;
	this.animations = 0;
	this.active = true;
	this.timerId = null;
	this.dumpAnimage = dumpAnimage;
	this.getAnimageDebugStr = getAnimageDebugStr;
}

