var M3DPlot=function(){this.alpha=1;this.fcolor={r:'175',g:'153',b:'207'};this.ecolor={r:'1',g:'1',b:'1'};this.fps=25;this.focalLength=700;this.datas=[];this.clock=null;this.screenPoints=[];this.faces=[];this.point_blender=[];this.zoom=0.7;this.translateY=1;this.offset=0.1;this.get3DPoint=function(x,y,z,col){return{x:parseInt(x*this.multi),y:parseInt(y*this.multi*this.zoom),z:parseInt(z*this.multi),color:col};};this.get2DPoint=function(x,y,depth){return{x:x,y:y,depth:depth};};this.prepareDatas=function(){var i=0;var j=0;var offset=this.offset;var translate=(this.datas.length+((this.datas.length-1)*offset))/2;var bw=1;var max=0;for(i=0;i<this.datas.length;i++){max=Math.max(max,this.datas[i].value);}
var transY=max/(this.multi*0.1*this.zoom);var zoom=0;for(i=0;i<this.datas.length;i++){var h=(this.canvas.height*this.datas[i].value/((max)*this.multi))-(transY);var color=this.datas[i].color;this.point_blender.push(this.get3DPoint(0+j-translate,transY,0-zoom,color));this.point_blender.push(this.get3DPoint(1+j-translate,transY,0-zoom,color));this.point_blender.push(this.get3DPoint(1+j-translate,-h,0-zoom,color));this.point_blender.push(this.get3DPoint(0+j-translate,-h,0-zoom,color));this.point_blender.push(this.get3DPoint(0+j-translate,transY,1-zoom,color));this.point_blender.push(this.get3DPoint(1+j-translate,transY,1-zoom,color));this.point_blender.push(this.get3DPoint(1+j-translate,-h,1-zoom,color));this.point_blender.push(this.get3DPoint(0+j-translate,-h,1-zoom,color));j+=1+offset;}};this.init=function(canvas){this.canvas=document.getElementById(canvas);this.multi=this.canvas.width*this.zoom/(this.datas.length);this.screenPoints=[];this.prepareDatas();try{this.theScene=this.canvas.getContext("2d");}catch(e){var ie=navigator.appVersion.match(/MSIE (\d\.\d)/);var opera=(navigator.userAgent.toLowerCase().indexOf('opera')!=-1);if((!ie)||(ie[1]<6)||(opera)){alert('not supported navigator');}}
this.theScene.globalCompositeOperation='source-over';this.cubeAxisRotations=this.get3DPoint(0,0,0);this.modulo=0;this.mousex=0;this.mousey=0;(function(_t){_t.canvas.onmousedown=function(evt){_t.mouse=true;_t.mousex=evt.clientX;_t.mousey=evt.clientY;_t.mousex-=_t.canvas.offsetLeft+_t.canvas.width/2;_t.mousey-=_t.canvas.offsetTop+_t.canvas.height/2;_t.mouseorigx=_t.mousex;_t.mouseorigy=_t.mousey;};_t.canvas.onmouseup=function(){_t.mouse=false};_t.canvas.onmousemove=function(evt){if(!_t.mouse)return;_t.mousex=evt.clientX;_t.mousey=evt.clientY;_t.mousex-=_t.mouseorigx+_t.canvas.offsetLeft+_t.canvas.width/2;_t.mousey-=_t.mouseorigy+_t.canvas.offsetTop+_t.canvas.height/2;_t.rotateObject();_t.mousex=evt.clientX;_t.mousey=evt.clientY;_t.mousex-=_t.canvas.offsetLeft+_t.canvas.width/2;_t.mousey-=_t.canvas.offsetTop+_t.canvas.height/2;_t.mouseorigx=_t.mousex;_t.mouseorigy=_t.mousey;};})(this)
this.rotateObject()};this.refresh=function(){this.clock=setTimeout(this.rotateObject,1/this.fps);};this.get2DFrom3D=function(){var sx=Math.sin(this.cubeAxisRotations.x);var cx=Math.cos(this.cubeAxisRotations.x);var sy=Math.sin(this.cubeAxisRotations.y);var cy=Math.cos(this.cubeAxisRotations.y);var sz=Math.sin(this.cubeAxisRotations.z);var cz=Math.cos(this.cubeAxisRotations.z);var x,y,z,xy,xz,yx,yz,zx,zy,scaleFactor;var i=this.point_blender.length;while(i--){x=this.point_blender[i].x;y=this.point_blender[i].y;z=this.point_blender[i].z;col=this.point_blender[i].color;xy=cx*y-sx*z;xz=sx*y+cx*z;yz=cy*xz-sy*x;yx=sy*xz+cy*x;zx=cz*yx-sz*xy;zy=sz*yx+cz*xy;scaleFactor=this.focalLength/(this.focalLength+yz);x=zx*scaleFactor;y=zy*scaleFactor;z=yz;if(!this.screenPoints[i]){this.screenPoints[i]=this.get2DPoint(x,y,-z,scaleFactor);}
else{this.screenPoints[i].x=Math.round(x,5);this.screenPoints[i].y=Math.round(y,5);this.screenPoints[i].depth=-z;}
this.screenPoints[i].color=col;}};this.rotateObject=function(){if(this.mousex==0||parseInt(this.mousex)==0){this.mousex=1;}
if(this.mousey==0||parseInt(this.mousey)==0){this.mousey=1;}
this.cubeAxisRotations.y-=this.mousex/100;this.cubeAxisRotations.x+=this.mousey/100;this.get2DFrom3D();this.draw3d();};this.drawFaces=function(){var index=0;var offset=0.75;var emitance=0.8;var i=0;var j=0;for(i=0;i<this.faces.length;i++){index=((i*(1-offset)*0.5)/(this.faces.length*(1-emitance)))+offset
this.theScene.fillStyle="rgba("+parseInt(this.faces[i].color.r*index)+","
+parseInt(this.faces[i].color.g*index)+","
+parseInt(this.faces[i].color.b*index)+","
+(this.faces[i].color.a?this.faces[i].color.a:1)
+")";this.theScene.strokeStyle="rgba("+parseInt(this.ecolor.r*index)+","
+parseInt(this.ecolor.g*index)+","
+parseInt(this.ecolor.b*index)+","
+(this.ecolor.a?this.ecolor.a:1)
+")";this.theScene.moveTo(this.faces[i].points[0].x,this.faces[i].points[0].y);this.theScene.beginPath();for(j=0;j<this.faces[i].points.length;j++){this.theScene.lineTo(this.faces[i].points[j].x,this.faces[i].points[j].y);}
this.theScene.closePath();this.theScene.fill();this.theScene.stroke();}};this.draw3d=function(){if(this.faces.length<1){this.faces=[];var i=0;for(i=0;i<this.datas.length;i++){var index=(i)*8;var color=this.datas[i].color;this.faces.push({points:[this.screenPoints[0+index],this.screenPoints[1+index],this.screenPoints[2+index],this.screenPoints[3+index]],color:this.screenPoints[0+index].color});this.faces.push({points:[this.screenPoints[3+index],this.screenPoints[7+index],this.screenPoints[4+index],this.screenPoints[0+index]],color:this.screenPoints[0+index].color});this.faces.push({points:[this.screenPoints[0+index],this.screenPoints[4+index],this.screenPoints[5+index],this.screenPoints[1+index]],color:this.screenPoints[0+index].color});this.faces.push({points:[this.screenPoints[1+index],this.screenPoints[5+index],this.screenPoints[6+index],this.screenPoints[2+index]],color:this.screenPoints[0+index].color});this.faces.push({points:[this.screenPoints[2+index],this.screenPoints[3+index],this.screenPoints[7+index],this.screenPoints[6+index]],color:this.screenPoints[0+index].color});this.faces.push({points:[this.screenPoints[6+index],this.screenPoints[5+index],this.screenPoints[4+index],this.screenPoints[7+index]],color:this.screenPoints[0+index].color});}}
this.faces.sort(this.orderDepth);this.theScene.save();this.theScene.moveTo(0,0);this.theScene.clearRect(0,0,this.canvas.width,this.canvas.height);this.theScene.translate(this.canvas.width/2,this.canvas.height/2);this.drawFaces();this.theScene.restore();};this.orderDepth=function(a,b){var i=0,max1av=0,max2av=0;for(i=0;i<a.points.length;i++){max1av+=a.points[i].depth;}
max1av=max1av/a.points.length;for(i=0;i<b.points.length;i++){max2av+=b.points[i].depth;}
max2av=max2av/b.points.length;return max1av-max2av;}}
