

function Matrix(x, y)
{
	this.x = x;
	this.y = y;
	this.data = [];
	// promote the 1st class functions
}

Matrix.prototype.rotate = function(ang) {
	var ret = new Matrix(3,3);
	ret.data = [	Math.cos(ang),	-Math.sin(ang),	0,
			Math.sin(ang),	Math.cos(ang),	0,
			0,		0,		1 ];
	return ret;
}

Matrix.prototype.scale = function(sx, sy) {
	var ret = new Matrix(3,3);
	ret.data = [	sx,		0,		0,
			0,		sy,		0,
			0,		0,		1 ];
	return ret;
}

Matrix.prototype.translate = function(tx, ty)
{
	var ret = new Matrix(3,3);
	ret.data = [	1,		0,		tx,
			0,		1,		ty,
			0,		0,		1 ];
	return ret;
}

// EXSPONGE!!!
Matrix.prototype.getData = function(_x, _y)
{
	return this.data[_x + _y * this.x];
}

Matrix.prototype.setData = function(_x, _y, _f)
{
	this.data[_x + _y * this.x] = _f;
}

// should return a "new" function too.
Matrix.prototype.mul = function(A,B)
{
	if (B.y != A.x)
	{
		return null;
	}
	else
	{
		var matrixOut = new Matrix(B.x, A.x);
//		this.x = B.x;
//		this.y = A.x;

		for (xx = 0; xx < B.x; xx++)
		{
			for (yy = 0; yy < A.y; yy++)
			{
				var sum = 0;
				for (xy = 0; xy < B.y; xy++)
				{
					a = A.getData(xy, yy);
					b = B.getData(xx, xy);

					sum += b * a;
				}

				matrixOut.setData(xx, yy, sum);
			}
		}
		
		return matrixOut;
	}
}

// fairly simple, but oh so handy!
Matrix.prototype.toHtml = function()
{
	var s = '<table border="1" width="100%" style="text-align:right;">';
	for (y = 0; y < this.y; y++)
	{
		s += '<tr>';
		for (x = 0; x < this.x; x++)
		{
			s += '<td><p>' + this.getData(x,y).toFixed(5).toString() + '</p></td>';
		}
		s += '</tr>';
	}
	s += '</table>';
	return s;
}

// this sucks!

Matrix.rotate = Matrix.prototype.rotate;
Matrix.scale = Matrix.prototype.scale;
Matrix.translate = Matrix.prototype.translate;
Matrix.mul = Matrix.prototype.mul;


