var editing = 0;

Triangle = function (vertex1,vertex2,vertex3) {
	this.vertex1 = vertex1;
	this.vertex2 = vertex2;
	this.vertex3 = vertex3;
	this.drawx1 = 0;
	this.drawx2 = 0;
	this.drawx3 = 0;
	this.drawy1 = 0;
	this.drawy2 = 0;
	this.drawy3 = 0;
	this.m1 = 0;
	this.m2 = 0;
	this.m3 = 0;
	return this;
}

Triangle.prototype.checkpoint = function (x,y) {
	if (Math.abs(this.drawy1-y) < 4 &&
		Math.abs(this.drawx1-x) < 4)
		vertexindex = this.vertex1;
	if (Math.abs(this.drawy2-y) < 4 &&
		Math.abs(this.drawx2-x) < 4)
		vertexindex = this.vertex2;
	if (Math.abs(this.drawy3-y) < 4 &&
		Math.abs(this.drawx3-x) < 4)
		vertexindex = this.vertex3;
	var b3 = this.drawy1-this.m3*this.drawx1;
	var sign3 = (this.drawy3-this.m3*this.drawx3-b3>0) ? 1 : -1;
	var b2 = this.drawy3-this.m2*this.drawx3;
	var sign2 = (this.drawy2-this.m2*this.drawx2-b2>0) ? 1 : -1;
	var b1 = this.drawy2-this.m1*this.drawx2;
	var sign1 = (this.drawy1-this.m1*this.drawx1-b1>0) ? 1 : -1;
	if ((y-this.m3*x-b3)*sign3 > 0 &&
		(y-this.m2*x-b2)*sign2 > 0 &&
		(y-this.m1*x-b1)*sign1 > 0) return 1;
}

Polygon = function (r,g,b) {
	this.name = '';
	this.trilist = new Array();
	this.vertices = new Array();
	this.red = r;
	this.green = g;
	this.blue = b;
	this.sred = 122;
	this.sgreen = 122;
	this.sblue = 122;
	this.selected = 0;
	this.averagez = 0;
	this.hero = 0;
	this.editable = 0;
	this.onrefresh = '';
	this.onchoose = '';
	this.worldx = 0;
	this.worldy = 0;
	this.active = 0;
}

Polygon.prototype.rotatez = function(theta) {
	for (var k=0;k<this.vertices.length;k++) {
		var newtvertex = vertexMultiply(translatematrix(this.worldx,this.worldy,0),this.vertices[k]);
		var newrvertex = vertexMultiply(rotatezmatrix(theta),newtvertex);
		var newmvertex = vertexMultiply(translatematrix(-this.worldx,-this.worldy,0),newrvertex);
		this.vertices[k] = newmvertex;
	}
}

Polygon.prototype.displayhelp = function() {
	$('hoverbox').innerHTML = 'Welcome!<br><br>Up: Move forward<br>Left/Right: Rotate<br>Left Mouse: Select Object<br>Right Mouse Drag: Camera Rotate<br>Return: Edit Vertex Escape';
}

Polygon.prototype.playmusic = function(filename) {
	$('hoverbox').innerHTML = '<embed src="' + filename + '" width="0" height="0" autostart="false" name="mysound" enablejavascript="true"/>The Jukebox<br><br><a href=javascript:document.mysound.Play()>Play "Cracked and Crazed"</a>';
}


Polygon.prototype.addvertex = function (vertex) {
	for (var k=0;k<this.vertices.length;k++) {
		if (vertex[0] == this.vertices[k][0] &&
			vertex[1] == this.vertices[k][1] &&
			vertex[2] == this.vertices[k][2]) {
			return k;
		}
	}
	this.vertices[this.vertices.length] = Array(vertex[0],vertex[1],vertex[2],vertex[3]);
	return this.vertices.length-1;
}

Polygon.prototype.addface = function (vertex1,vertex2,vertex3) {
	var index1 = this.addvertex(vertex1);
	var index2 = this.addvertex(vertex2);
	var index3 = this.addvertex(vertex3);
	this.trilist[this.trilist.length] = new Triangle(index1,index2,index3);
	return true;
}

Polygon.prototype.checkpoint = function (x,y,users) {
	var returnvalue = 0;
	for (var j=0;j<this.trilist.length;j++) {
		if(this.trilist[j].checkpoint(x,y)) returnvalue = 1;
	}
	if (returnvalue || this.editable) {
		$('hoverbox').style.top = screenPos.y;
		$('hoverbox').style.left = screenPos.x;
		if (this.hero) $('hoverbox').innerHTML = this.name + "<br><a href='javascript:;' onclick='plantcube()'>Plant Seed</a>";
		else if (this.onchoose != '') eval('this.' + this.onchoose);
		else if (users) $('hoverbox').innerHTML = this.name + "<br><br>Active: " + Math.floor(this.active/10000) + "h, " + (Math.floor(this.active/100) % 100) + "m, " + (this.active % 100) + "s";
		else $('hoverbox').innerHTML = this.name + "<br><a href='javascript:;' onclick='editcube()'>Edit</a>";
		this.selected = 1;
		return true;
	}
	else {
		this.selected = 0;
		return false;
	}
}

	
Polygon.prototype.draw = function () {
	if (this.onrefresh) {
		eval('this.' + this.onrefresh);
	}
	if (this.selected) {
		var r = this.sred;
		var g = this.sgreen;
		var b = this.sblue;
	} else {
		var r = this.red;
		var g = this.green;
		var b = this.blue;
	}
	for (var w=0;w<this.trilist.length;w++) {
		var mainMatrix;
		if (this.hero) mainMatrix = avatarMatrix;
		else mainMatrix = masterMatrix;
		if (this.hero) {
			drawa = vertexMultiply(mainMatrix,vertexMultiply(worldTransformMatrix,this.vertices[this.trilist[w].vertex1]));
			drawb = vertexMultiply(mainMatrix,vertexMultiply(worldTransformMatrix,this.vertices[this.trilist[w].vertex2]));
			drawc = vertexMultiply(mainMatrix,vertexMultiply(worldTransformMatrix,this.vertices[this.trilist[w].vertex3]));

		} else {
			drawa = vertexMultiply(mainMatrix,vertexMultiply(worldtransMatrix,this.vertices[this.trilist[w].vertex1]));
			drawb = vertexMultiply(mainMatrix,vertexMultiply(worldtransMatrix,this.vertices[this.trilist[w].vertex2]));
			drawc = vertexMultiply(mainMatrix,vertexMultiply(worldtransMatrix,this.vertices[this.trilist[w].vertex3]));
		}
		var scalea = 1/drawa[3]*50;
		var scaleb = 1/drawb[3]*50;
		var scalec = 1/drawc[3]*50;
		this.averagez = drawa[2]*drawb[2]*drawc[2]/drawa[3]/drawb[3]/drawc[3];

		if((Math.abs(drawa[2]/drawa[3])<1 && Math.abs(drawb[2]/drawb[3])<1 && Math.abs(drawc[2]/drawc[3])<1) || this.hero)
		{
			this.trilist[w].drawx1 = drawa[0]*scalea+250;
			this.trilist[w].drawx2 = drawb[0]*scaleb+250;
			this.trilist[w].drawx3 = drawc[0]*scalec+250;
			this.trilist[w].drawy1 = drawa[1]*scalea+250;
			this.trilist[w].drawy2 = drawb[1]*scaleb+250;
			this.trilist[w].drawy3 = drawc[1]*scalec+250;
			if (this.trilist[w].drawx1 > 500 ||
				this.trilist[w].drawx2 > 500 ||
				this.trilist[w].drawx3 > 500 ||
				this.trilist[w].drawx1 < 0 ||
				this.trilist[w].drawx2 < 0 ||
				this.trilist[w].drawx3 < 0 ||
				this.trilist[w].drawy1 > 500 ||
				this.trilist[w].drawy2 > 500 ||
				this.trilist[w].drawy3 > 500 ||
				this.trilist[w].drawy1 < 0 ||
				this.trilist[w].drawy2 < 0 ||
				this.trilist[w].drawy3 < 0 )
				break;
				
			this.trilist[w].m1 = (this.trilist[w].drawy2-this.trilist[w].drawy3)/(this.trilist[w].drawx2-this.trilist[w].drawx3);
			this.trilist[w].m2 = (this.trilist[w].drawy3-this.trilist[w].drawy1)/(this.trilist[w].drawx3-this.trilist[w].drawx1);
			this.trilist[w].m3 = (this.trilist[w].drawy1-this.trilist[w].drawy2)/(this.trilist[w].drawx1-this.trilist[w].drawx2);
			var minimum = Math.min(this.trilist[w].drawx1,Math.min(this.trilist[w].drawx2,this.trilist[w].drawx3));
			if ((((minimum == this.trilist[w].drawx1 && this.trilist[w].m3 > this.trilist[w].m2) ||
				(minimum == this.trilist[w].drawx2 && this.trilist[w].m1 > this.trilist[w].m3) ||
				(minimum == this.trilist[w].drawx3 && this.trilist[w].m2 > this.trilist[w].m1)) == true) || this.hero)
			poly(this.trilist[w].drawx1,this.trilist[w].drawy1,this.trilist[w].drawx2,this.trilist[w].drawy2,this.trilist[w].drawx3,this.trilist[w].drawy3,r,g,b,this.editable,this.trilist[w].vertex1,this.trilist[w].vertex2,this.trilist[w].vertex3);
		}
	}
}

function plantcube() {
	polygonList[0].selected = 0;
	var newindex = polygonList.length;
	polygonList[newindex] = new Polygon(55,255,55);
	polygonList[newindex].name = 'Seed';
	polygonList[newindex].addface(Array(-globalx+.5,-globaly+.5,-.5,1),Array(-globalx-.5,-globaly-.5,-.5,1),Array(-globalx-.5,-globaly+.5,-.5,1));
	polygonList[newindex].addface(Array(-globalx+.5,-globaly-.5,-.5,1),Array(-globalx-.5,-globaly-.5,-.5,1),Array(-globalx+.5,-globaly+.5,-.5,1));
	polygonList[newindex].addface(Array(-globalx-.5,-globaly-.5,-.5,1),Array(-globalx+.5,-globaly-.5,-.5,1),Array(-globalx+.5,-globaly-.5,.5,1));
	polygonList[newindex].addface(Array(-globalx-.5,-globaly-.5,.5,1),Array(-globalx-.5,-globaly-.5,-.5,1),Array(-globalx+.5,-globaly-.5,.5,1));
	polygonList[newindex].addface(Array(-globalx-.5,-globaly+.5,.5,1),Array(-globalx-.5,-globaly-.5,-.5,1),Array(-globalx-.5,-globaly-.5,.5,1));
	polygonList[newindex].addface(Array(-globalx-.5,-globaly+.5,-.5,1),Array(-globalx-.5,-globaly-.5,-.5,1),Array(-globalx-.5,-globaly+.5,.5,1));
	polygonList[newindex].addface(Array(-globalx+.5,-globaly-.5,.5,1),Array(-globalx+.5,-globaly+.5,-.5,1),Array(-globalx+.5,-globaly+.5,.5,1));
	polygonList[newindex].addface(Array(-globalx+.5,-globaly-.5,.5,1),Array(-globalx+.5,-globaly-.5,-.5,1),Array(-globalx+.5,-globaly+.5,-.5,1));
	polygonList[newindex].addface(Array(-globalx+.5,-globaly-.5,.5,1),Array(-globalx+.5,-globaly+.5,.5,1),Array(-globalx-.5,-globaly+.5,.5,1));
	polygonList[newindex].addface(Array(-globalx-.5,-globaly-.5,.5,1),Array(-globalx+.5,-globaly-.5,.5,1),Array(-globalx-.5,-globaly+.5,.5,1));
	polygonList[newindex].addface(Array(-globalx+.5,-globaly+.5,-.5,1),Array(-globalx-.5,-globaly+.5,.5,1),Array(-globalx+.5,-globaly+.5,.5,1));
	polygonList[newindex].addface(Array(-globalx-.5,-globaly+.5,-.5,1),Array(-globalx-.5,-globaly+.5,.5,1),Array(-globalx+.5,-globaly+.5,-.5,1));
	$('hoverbox').style.display = 'none';
	
	pars = 'obj=';
	for (var e=0;e<polygonList[newindex].vertices.length;e++) {
		pars += 'v%20' + (polygonList[newindex].vertices[e][0]+globalx)
			+'%20' + (polygonList[newindex].vertices[e][1]+globaly)
			+'%20' + polygonList[newindex].vertices[e][2]
			+'%20\n';
	}
	for (var f=0;f<polygonList[newindex].trilist.length;f++) {
		pars += 'f%20' + polygonList[newindex].trilist[f].vertex1
		        +'%20' + polygonList[newindex].trilist[f].vertex2
			+'%20' + polygonList[newindex].trilist[f].vertex3
			+'%20\n';
	}
	pars += '&worldx=' + globalx;
	pars += '&worldy=' + globaly;
	pars += '&red=55&green=255&blue=55&send=1&objname=';
	pars += Math.floor(Math.random()*10+1);
	pars += Math.floor(Math.random()*10+1);
	pars += Math.floor(Math.random()*10+1);
	pars += Math.floor(Math.random()*10+1);
	pars += Math.floor(Math.random()*10+1);
	var seedAjax = new Ajax.Request('importobj.php',{method:'post',parameters:pars});	
}

function editcube() {
	for (var w=0;w<polygonList.length;w++) {
		if (polygonList[w].selected) {
			polygonList[w].editable = 1;
			editing = w;
		}
	}
	$('hoverbox').style.display = 'none';
}