// Ants move along paths (include paths before including ants)
var _ants_antid = 0;
function ant(path, increment)
{
	this.antid = _ants_antid;
	_ants_antid++;
	this.setPath = ant_setPath;
	this.getPath = ant_getPath;
	this.addPathMarker = ant_addPathMarker;
	this.setStopAtPathStart = ant_setStopAtPathStart;
	this.setStopAtPathEnd = ant_setStopAtPathEnd;
	this.setStopAt = ant_setStopAt;
	this.setBounceAtPathStart = ant_setBounceAtPathStart;
	this.setBounceAtPathEnd = ant_setBounceAtPathEnd;
	this.setWrapAtPathStart = ant_setWrapAtPathStart;
	this.setWrapAtPathEnd = ant_setWrapAtPathEnd;
	this.gotoIndex = ant_gotoIndex;
	this.getPathPosition = ant_getPathPosition;
	this.getExtraData = ant_getExtraData;
	this.setExtraData = ant_setExtraData;

	this.step = ant_step;
	this.stop = ant_stop;
	this.bounce = ant_bounce;
	this.wrap = ant_wrap;

	this.markers = new Array();
	this.extradata = new Array();

	this.index = 0;
	this.onstep = nop;
	if (path != undefined)
	{
		this.path = path;
	}
	if (increment != undefined)
	{
		this.increment = increment;
	}
}

function nop(path, index)
{
	return;
}

function ant_setPath(path, index)
{
	this.path = path;
}

function ant_getPath()
{
	return this.path;
}

function ant_addPathMarker(index, func)
{
	this.markers[index] = func;
}

function ant_step()
{
	var doinc = true;
	this.onstep(this, this.path);
	if (this.markers[this.index] != undefined)
	{
		doinc = this.markers[this.index](this, this.path);
	}
	if (doinc == true)
	{
		this.index += this.increment;
	}
}

function ant_stop()
{
	this.increment = 0;
}

function ant_gotoIndex(index)
{
	this.index = index;
}

function ant_getPathPosition()
{
	return this.path.getPosition(this.index);
}

function ant_bounce()
{
	this.increment = - this.increment;
}

function ant_wrap(ant, path)
{
	var p = ant.path;
	var pl = p.positions.length-1;
	if (ant.index >= pl)
	{
		ant.index = 0;
	}
	else
	{
		if (ant.index <= 0)
		{
			ant.index = pl;
		}
	}
	return false;
}

function ant_setStopAt(index)
{
	this.addPathMarker(index, this.stop);
}

function ant_setStopAtPathStart()
{
	this.addPathMarker(0, this.stop)
}

function ant_setStopAtPathEnd()
{
	this.addPathMarker(this.path.length-1, this.stop)
}

function ant_setBounceAtPathStart()
{
	this.addPathMarker(0, this.bounce)
}

function ant_setBounceAtPathEnd()
{
	this.addPathMarker(this.path.length-1, this.bounce)
}

function ant_setPathBounce()
{
	this.setBounceAtPathStart();
	this.setBounceAtPathEnd();
}

function ant_setWrapAtPathStart()
{
	this.addPathMarker(0, this.wrap)
}

function ant_setWrapAtPathEnd()
{
	this.addPathMarker(this.path.positions.length-1, this.wrap)
}

function ant_setPathWrap()
{
	this.setWrapAtPathStart();
	this.setWrapAtPathEnd();
}

function ant_getExtraData(index)
{
	if (index != undefined)
	{
		return this.extradata[index];
	}
	return this.extradata;
}

function ant_setExtraData(index, data)
{
	this.extradata[index] = data;
}
