var vector3D = new Class({
	x: 0,
	y: 0,
	z: 0,
	initialize: function(x, y, z) {
		this.x = x;
		this.y = y;
		this.z = z;
	},
	add: function(v) {
		this.x += v.x;
		this.y += v.y;
		this.z += v.z;
	},
	substract: function(v) {
		this.x -= v.x;
		this.y -= v.y;
		this.z -= v.z;
	},
	kreuz: function(v) {
		this.x = this.y*v.z-this.z*v.y;
		this.y = this.z*v.x-this.x*v.z;
		this.z = this.x*v.y-this.y*v.x;
	},
	manipMatrix: function(m) {
		var me = new Array(this.x, this.y, this.z, 1);
		var tmp = new Array(0, 0, 0, 0);
		for(var i=0;i<4;i++) {
			for(var k=0;k<4;k++)
				tmp[i] += me[k]*m.val[k][i];
		}
		this.x = tmp[0];
		this.y = tmp[1];
		this.z = tmp[2];
	},
	draw: function(dist) {
		if(dist+this.z == 0) return false;

		return {x: (dist*this.x/(dist+this.z)), y: (dist*this.y/(dist+this.z))};
	}
});
var matrix4X = new Class({
	val: [],
	initialize: function() {
		this.val = new Array(
			new Array(1, 0, 0, 0),
			new Array(0, 1, 0, 0),
			new Array(0, 0, 1, 0),
			new Array(0, 0, 0, 1)
			);
	},
	kreuz: function(oldval, newval) {
		this.val = new Array(
			new Array(0, 0, 0, 0),
			new Array(0, 0, 0, 0),
			new Array(0, 0, 0, 0),
			new Array(0, 0, 0, 0)
			);
		for(var i=0;i<4;i++)
			for(var j=0;j<4;j++)
				for(var k=0;k<4;k++)
					this.val[i][j] += oldval[i][k]*newval[k][j];
	},
	move: function(v) {
		var ov = this.val;
		var nv = new Array(
			new Array(1, 0, 0, 0),
			new Array(0, 1, 0, 0),
			new Array(0, 0, 1, 0),
			new Array(v.x, v.y, v.z, 1)
			);
		this.kreuz(ov, nv);
	},
	scale: function(x, y, z) {
		var ov = this.val;
		var nv = new Array(
			new Array(x, 0, 0, 0),
			new Array(0, y, 0, 0),
			new Array(0, 0, z, 0),
			new Array(0, 0, 0, 1)
			);
		this.kreuz(ov, nv);
	},
	rotX: function(w) {
		var c = Math.cos(w);
		var s = Math.sin(w);
		var ov = this.val;
		var nv = new Array(
			new Array(1, 0, 0, 0),
			new Array(0, c, s, 0),
			new Array(0, -s, c, 0),
			new Array(0, 0, 0, 1)
			);
		this.kreuz(ov, nv);
	},
	rotY: function(w) {
		var c = Math.cos(w);
		var s = Math.sin(w);
		var ov = this.val;
		var nv = new Array(
			new Array(c, 0, s, 0),
			new Array(0, 1, 0, 0),
			new Array(-s, 0, c, 0),
			new Array(0, 0, 0, 1)
			);
		this.kreuz(ov, nv);
	},
	rotZ: function(w) {
		var c = Math.cos(w);
		var s = Math.sin(w);
		var ov = this.val;
		var nv = new Array(
			new Array(c, s, 0, 0),
			new Array(-s, c, 0, 0),
			new Array(0, 0, 1, 0),
			new Array(0, 0, 0, 1)
			);
		this.kreuz(ov, nv);
	},
	show: function() {
		this.showArr(this.val);
	},
	showArr: function(v) {
		var t = "";
		for(var i=0;i<4;i++) {
			for(var j=0;j<4;j++)
				t += v[i][j]+"\t\t";
			t += "\n";
		}
		alert(t);		
	}
});

