// Atom class for Verlet physics engine
// by Andreas Blixt <andreasblixt@msn.com> 2007
// Free for use. Please include this header.

Atom = function (group, x, y, radius, friction, density) {
	// Make sure a group was passed.
	if (!(group instanceof Group)) throw "First argument must be an instance of Group.";

	// Store a reference to the specified group which this atom should belong to.
	this.Group = group;

	// Validate horizontal position.
	x = parseFloat(x);
	if (isNaN(x)) throw "Second argument must be a number.";

	// Validate vertical position.
	y = parseFloat(y);
	if (isNaN(y)) throw "Third argument must be a number.";

	// Position this atom at the specified coordinates.
	this.PreviousX = this.CurrentX = x;
	this.PreviousY = this.CurrentY = y;

	// Get a unique ID for this atom.
	var idCounter = 1;
	while (group.World.GetByID(this.ID = "atom" + idCounter++));

	// Set physical properties if valid values were given.
	if (radius > 0.0) this.Radius = radius;
	//if (density > 0.0) this.Density = density;
	//if (friction !== NaN && friction >= 0.0 && friction <= 1.0) this.Friction = friction;

	// Add this atom to the specified group.
	group.Atoms.push(this);

	// Add this atom to the global list of objects.
	group.World.All.push(this);
};

Atom.prototype = {
	// A unique string identifying this particular atom.
	ID: "",

	// Current coordinates of this atom.
	CurrentX: NaN, CurrentY: NaN,

	// A reference to the group which this atom belongs to.
	Group: null,

	// Previous coordinates of this atom.
	PreviousX: NaN, PreviousY: NaN,

	// Values which define the physical properties of this atom.
	//Density: 1.0,
	//Friction: 0.98,
	Radius: 5.0,
	Static: false, // Whether this atom will be unaffected by forces.

	// Removes this atom.
	Remove: function () {
		for (var i in this.Group.Atoms)
			if (this.Group.Atoms[i] === this) this.Group.Atoms.splice(i, 1);
		for (var i in this.Group.World.All)
			if (this.Group.World.All[i] === this) this.Group.World.All.splice(i, 1);
	}
};