/**
Rendered 3D Environement
**/
var mmp_render = {

	'isIe':null,
	'isOpera':null,
	'isSafari':null,
	'isGecko':null,
	'factory_element':null,
	
	'id_count':0,
	'object_coord':[],
	'global_angle_x':50,
	'global_angle_y':0,
	'global_angle_z':25,
	'type_render':"points",
		
	'init':function(element_target, parameters){
		var navig_agent = navigator.userAgent.toLowerCase();
		
		this.isOpera = (navig_agent.indexOf('opera') > -1);
		this.isSafari = (navig_agent.indexOf('safari') > -1);
		this.isGecko = (!this.isOpera && !this.isSafari && navig_agent.indexOf('gecko') > -1);
		this.isIE = (!this.isOpera && navig_agent.indexOf('msie') > -1); 
		
		if(typeof element_target != 'object'){
			element_target = document.getElementById(element_target);
			if(typeof element_target != 'object'){
				alert("No specified element avaiable");
			}
		}
		
		if(this.isGecko || this.isOpera ){
			var head_element = document.getElementsByTagName("head")[0];
			
			var canvasEl = document.createElement('canvas');	
			canvasEl.id = "mmp_element"+this.id_count;	
			if(parameters.width != null){
				canvasEl.width = parameters.width;		
			}
			if(parameters.height != null){
				canvasEl.height = parameters.height;		
			}	
			
			element_target.appendChild(canvasEl);
			this.factory_element = mmp_canvasrender;			
		}	
		
		if(this.factory_element != null){
			this.factory_element.init("mmp_element"+this.id_count);	
			this.id_count = this.id_count +1;
			if(parameters.type_render != null){
				this.type_render = parameters.type_render;
			}
			if(parameters.anim != null && parameters.anim != false){
				setInterval("mmp_render.anim_scene()", 100)
			}
		}else{
			alert("Your Browser can't support this features");
		}
	}, 
	
	'addCube':function(name, x, y, z, width, height, depth){
		if(this.factory_element != null){
			var temp_coord = [];
			
			temp_coord.push([x, y, z]);
			temp_coord.push([x+width, y, z]);
			temp_coord.push([x+width, y+height, z]);
			temp_coord.push([x, y+height, z]);
			temp_coord.push([x, y, z]);
			
			temp_coord.push([x, y, z+depth]);
			temp_coord.push([x+width, y, z+depth]);
			temp_coord.push([x+width, y+height, z+depth]);
			temp_coord.push([x, y+height, z+depth]);
			temp_coord.push([x, y, z+depth]);
			
			this.object_coord.push({'id':name, 'coords':temp_coord})
			if(this.type_render == "points"){
				this.factory_element.cube(temp_coord);
			}else{
				this.factory_element.cube_faces(temp_coord);
			}
		}else{
			alert("Your Browser can't support this features");		
		}
	},
	'anim_scene':function(){
		this.global_angle_x = this.global_angle_x + 1;
		this.global_angle_y = this.global_angle_y + 2;
		this.global_angle_z = this.global_angle_z + 3;
		this.factory_element.clearScene();
		for(var i=0; i<this.object_coord.length; i++){
			if(this.type_render == "points"){
				this.factory_element.cube(this.object_coord[i].coords);
			}else{
				this.factory_element.cube_faces(this.object_coord[i].coords);
			}
		}
	}

}

var mmp_3dsetup = {
	
	'sin':function(angle){
		return Math.sin(this._calculate(angle));
	},
	'cos':function(angle){
		return Math.cos(this._calculate(angle));
	},
	'_calculate':function(angle){	
		return (angle+90)%360 *Math.PI/180;	
	}
}

var mmp_canvasrender = {
	
	mmp_ctx:null,

	'init':function(element_target_obj){	
		this.mmp_ctx = document.getElementById(element_target_obj).getContext("2d");
	},
	'clearScene':function(){
		this.mmp_ctx.clearRect(0, 0, 1000, 1000);
	},
	'cube':function(array_coords){

		
		this.mmp_ctx.rotate(mmp_3dsetup.cos(mmp_render.global_angle_x)/200,mmp_3dsetup.sin(mmp_render.global_angle_y)/200);
		this.mmp_ctx.scale(1-mmp_3dsetup.cos(mmp_render.global_angle_x)/200, 1-mmp_3dsetup.cos(mmp_render.global_angle_x)/200)
		this.mmp_ctx.translate(mmp_3dsetup.cos(mmp_render.global_angle_x)/200, mmp_3dsetup.sin(mmp_render.global_angle_y)/200)
		this.mmp_ctx.save();
		
		for(var i=0; i< array_coords.length; i++){
		
			this.mmp_ctx.fillStyle = "rgb(200,0,0)";
			
			var _z = array_coords[i][2]*mmp_3dsetup.cos(mmp_render.global_angle_z);
			var _x = array_coords[i][0]+(_z*mmp_3dsetup.cos(mmp_render.global_angle_x));
			var _y = array_coords[i][1]+(_z*mmp_3dsetup.sin(mmp_render.global_angle_y));
			this.mmp_ctx.moveTo(_x, _y);			
			
			if(i<5){
				var _ztmp = array_coords[i+5][2]*mmp_3dsetup.cos(mmp_render.global_angle_z);
				var _xtmp = array_coords[i+5][0]+(_ztmp*mmp_3dsetup.cos(mmp_render.global_angle_x));
				var _ytmp = array_coords[i+5][1]+(_ztmp*mmp_3dsetup.sin(mmp_render.global_angle_y));
				this.mmp_ctx.lineTo(_xtmp, _ytmp);
				
			}
			
			this.mmp_ctx.moveTo(_x, _y);			
			if(i!=0){			
				var _z = array_coords[i-1][2]*mmp_3dsetup.cos(mmp_render.global_angle_z);
				var _x = array_coords[i-1][0]+(_z*mmp_3dsetup.cos(mmp_render.global_angle_x));
				var _y = array_coords[i-1][1]+(_z*mmp_3dsetup.sin(mmp_render.global_angle_y));
			}
			this.mmp_ctx.lineTo(_x, _y);
			this.mmp_ctx.fill();
			//document.getElementById("trace").innerHTML =document.getElementById("trace").innerHTML + "X: "+_x+" - Y: "+_y+" - Z:"+_z+"<br />";
			 			
		
			this.mmp_ctx.closePath();
		}
		this.mmp_ctx.strokeStyle = "white";;
		this.mmp_ctx.stroke();
		this.mmp_ctx.restore();
		
	},
	'faces':function(point1, point2, point3){
			this.mmp_ctx.beginPath();
			this.mmp_ctx.fillStyle = "red";
			this.mmp_ctx.moveTo(point1[0], point1[1]);		
			this.mmp_ctx.lineTo(point2[0], point2[1]);			
			this.mmp_ctx.lineTo(point3[0], point3[1]);	
			this.mmp_ctx.lineTo(point1[0], point1[1]);	
			this.mmp_ctx.fill();
			this.mmp_ctx.stroke();
			this.mmp_ctx.closePath();
		},	
		'cube_faces':function(array_coords){		
			var array_coordss = array_coords
			this.mmp_ctx.rotate(mmp_3dsetup.cos(mmp_render.global_angle_x)/200,mmp_3dsetup.sin(mmp_render.global_angle_y)/200);
			this.mmp_ctx.scale(1-mmp_3dsetup.cos(mmp_render.global_angle_x)/200, 1-mmp_3dsetup.cos(mmp_render.global_angle_x)/200)
			this.mmp_ctx.translate(mmp_3dsetup.cos(mmp_render.global_angle_x)/200, mmp_3dsetup.sin(mmp_render.global_angle_y)/200)
			this.mmp_ctx.save();
			
			for(var i=0; i< array_coords.length; i++){
				array_coordss[i][2] = array_coords[i][2]*mmp_3dsetup.cos(mmp_render.global_angle_z);
				array_coordss[i][0] = array_coords[i][0]+(array_coordss[i][2]*mmp_3dsetup.sin(mmp_render.global_angle_x));
				array_coordss[i][1] = array_coords[i][1]+(array_coordss[i][2]*mmp_3dsetup.cos(mmp_render.global_angle_y));
			}
			
			this.faces(array_coordss[8],array_coordss[7],array_coordss[9]);
			this.faces(array_coordss[6],array_coordss[7],array_coordss[9]);
			
			this.faces(array_coordss[3],array_coordss[2],array_coordss[8]);
			this.faces(array_coordss[7],array_coordss[2],array_coordss[8]);
			
			this.faces(array_coordss[5],array_coordss[4],array_coordss[8]);
			this.faces(array_coordss[3],array_coordss[4],array_coordss[8]);
			
			this.faces(array_coordss[1],array_coordss[6],array_coordss[2]);
			this.faces(array_coordss[7],array_coordss[6],array_coordss[2]);
			
			this.faces(array_coordss[0],array_coordss[1],array_coordss[3]);
			this.faces(array_coordss[2],array_coordss[1],array_coordss[3]);
			
			this.faces(array_coordss[5],array_coordss[0],array_coordss[6]);
			this.faces(array_coordss[1],array_coordss[0],array_coordss[6]);	
	
			this.mmp_ctx.restore();
			
		}
}