// Animation paths... paths along which you can move things

// Dependencies - dodgylib/mathutil.js
// Version 29/9/2004
// This is probably dodgy, if there is a proper way to do inheritence in js
// then this certainly needs it!

// Constructors for some different kinds of paths

// Plain path
// top and left can be arrays or numbers, specify neither or both
function path(top, left, wrap) {
	function points() {
		return this.top.length;
	}
	function setWrap(wrap) {
		this.wrap = wrap
	}
	function addIndex(index, value, increment) {
		if (value == undefined) value = 0;
		if (increment == undefined) increment = 1;
		this.index[index] = value;
		this.increment[index] = increment;
	}
	if (top.length != undefined && typeof(top) == 'object')
	{
		this.top = top;
	}
	else 
	{
		if (typeof(top) == "number" && top != undefined)
		{
				var frames = 1;
				if (left.length != undefined)
				{
					frames = left.length;
				}
				this.top = new Array(frames);
				for (i = 0; i < frames; i++)
				{
					this.top[i] = parseInt(top);
				}
		}
	}

	if (left.length != undefined && typeof(left) == 'object')
	{
		this.left = left;
	}
	else 
	{
		if (typeof(left) == "number" && left != undefined)
		{
			var frames = 1;
			if (top.length != undefined)
			{
				frames = top.length;
			}
			this.left = new Array(frames);
			for (i = 0; i < frames; i++)
			{
				this.left[i] = parseInt(left);
			}
		}
	}


	this.addIndex = addIndex;
	this.setWrap = setWrap;
	this.points = points;

	this.wrap = wrap;
	this.index = new Array();
	this.increment = new Array();
	this.addIndex(0);
}

// Use precalcs to generate a circular path for you
function circularPath(topr, leftr, frames, top, left) {
	this.top = new Array();
	this.left = new Array();
	this.index = new Array();
	this.increment = new Array();
	this.top = precalcSin(topr, frames, top);
	this.left = precalcCos(leftr, frames, left);
	this.points = points;
	this.wrap = true;
	function points() {
		return this.top.length;
	}
	function setWrap(wrap) {
		this.wrap = wrap
	}
	function addIndex(index, value, increment) {
		if (value == undefined) value = 0;
		if (increment == undefined) increment = 1;
		this.index[index] = value;
		this.increment[index] = increment;
	}
	this.addIndex = addIndex;
	this.setWrap = setWrap;
	this.addIndex(0);
}

function flatPath(leftr, frames, top, left) {
	this.top = new Array();
	this.left = new Array();
	this.index = new Array();
	this.increment = new Array();
	this.top = new Array(frames);
	for (i = 0; i < frames; i++) {
		this.top[i] = top;
	}
	this.left = precalcCos(leftr, frames, left);
	this.points = points;
	this.wrap = true;
	function points() {
		return this.top.length;
	}
	function setWrap(wrap) {
		this.wrap = wrap
	}
	function addIndex(index, value, increment) {
		if (value == undefined) value = 0;
		if (increment == undefined) increment = 1;
		this.index[index] = value;
		this.increment[index] = increment;
	}
	this.addIndex = addIndex;
	this.setWrap = setWrap;
	this.addIndex(0);
}


// Utility functions
function moveElementAlongPath(path, id, index) {
	if (path == undefined) return;
	if (index == undefined) index = 0;
	d = document.getElementById(id);
	i = path['index'][index];
	i = i + path['increment'][index];
	p = path.points();
	if (i >= p) 
	{
		if (! path.wrap) return;
		i = 0;
	}
	if (i < 0) 
	{
		if (! path.wrap) return;
		i = (p - 1);
	}
	d.style.left = path['left'][i];
	d.style.top = path['top'][i];
	path['index'][index] = i;
}

