/* 
 * quarkleG
 */

var quarkleG=new function(){var d={canvasW:500,canvasH:500,framerate:30,outerRadius:180,inertia:0.9,duration:30,mouseRadius:2,mouseForce:-0.5,numberEls:70};var c;var b;var h;var j;var a;var p=this;var g={};var e;var i=false;var n={current:{x:9999,y:9999},previous:{x:9999,y:9999},velocity:{x:0,y:0}};var l=[{r:23,g:80,b:131,a:0.5},{r:235,g:166,b:18,a:0.5},{r:135,g:153,b:63,a:0.5},{r:50,g:160,b:172,a:0.5}];var k=[{r:193,g:129,b:255,a:0.5},{r:253,g:254,b:0,a:0.5},{r:253,g:2,b:143,a:0.5}];var m=[{r:23,g:80,b:131,a:0.5},{r:23,g:80,b:131,a:0.5},{r:23,g:80,b:131,a:0.5},{r:23,g:80,b:131,a:0.5},{r:23,g:80,b:131,a:0.5},{r:32,g:192,b:255,a:0.5}];this.init=function(){h=document.getElementById("quarkle");c=document.getElementById("quarkle-canvas");j=document.getElementById("quarkle-mask");a=$(".quarkle-label");if(c&&c.getContext){c.width=d.canvasW;c.height=d.canvasH;if(typeof(G_vmlCanvasManager)!="undefined"){G_vmlCanvasManager.initElement(c);}b=c.getContext("2d");b.globalCompositeOperation="source-over";this.windowResizeHandler();j.addEventListener("mousedown",function(q){q.preventDefault();},false);j.addEventListener("mousemove",this.mouseMoveHandler,false);j.addEventListener("mouseleave",this.mouseLeaveHandler,false);window.addEventListener("resize",function(q){p.windowResizeHandler();},false);this.createParticles();this.startClock();}};this.startClock=function(){var q=this;e=window.setInterval(function(){q.update();},1000/d.framerate);};var o={protons:[],electrons:[],quarks:[]};var f=function(q){q=q!=null?q:{};this.x=q.x||d.canvasW/2;this.y=q.y||d.canvasH/2;this.vX=q.vX||0;this.vY=q.vY||0;this.radius=q.radius||10;this.oRadius=q.radius||10;this.mass=q.mass||1+Math.random()*8;this.color=q.color||{r:0,g:0,b:0,a:0.5};this.oColor=util.clone(q.color);this.alpha=q.alpha||0.5;this.type=q.type||"particle";this.electrons=0;this.update=function(w){var r=util.getVector({x:d.canvasW/2,y:d.canvasH/2},this);if(r.distance>=225){this.x-=this.vX*1.5;this.y-=this.vY*1.5;this.vX*=-3;this.vY*=-3;}if(this.type=="electron"){for(var t in o.protons){var s=o.protons[t];if(util.colorMatch(this.color,s.oColor)){v=util.getVector(s,this);if(v.distance<s.radius){this.dissolve(s,v);}var z=0.0003/v.distance*s.radius;this.vX-=v.dX*z*this.mass;this.vY-=v.dY*z*this.mass;}}}if(this.targetColor&&!util.colorMatch(this.color,this.targetColor)){if(Math.abs(this.targetColor.r-this.color.r)/10<1&&Math.abs(this.targetColor.g-this.color.g)/10<1&&Math.abs(this.targetColor.b-this.color.b)/10<1){this.color=util.clone(this.targetColor);this.targetColor=false;}else{this.color.r+=Math.ceil((this.targetColor.r-this.color.r)/10);this.color.g+=Math.ceil((this.targetColor.g-this.color.g)/10);this.color.b+=Math.ceil((this.targetColor.b-this.color.b)/10);}}if((this.type=="quark"||this.type=="strange")&&Math.random()<0.02){this.type="electron";this.targetColor=util.clone(l[Math.floor(Math.random()*l.length)]);}if(this.type!="proton"){var u=Math.random()<0.1;var B=u?-0.25+Math.random()*0.5:0;this.radius+=this.radius+B>0&&this.radius+B<=this.oRadius+10?B:0;}var x=util.getVector(n.current,this);x.dX/=x.distance;x.dY/=x.distance;if(x.distance<this.radius+d.mouseRadius){if(this.type!="proton"){var A=d.mouseForce-(x.distance/(this.radius+d.mouseRadius));this.vX-=x.dX*A*this.mass/this.radius/0.1;this.vY-=x.dY*A*this.mass/this.radius/0.1;}else{this.setActive(w);}}this.vX*=d.inertia;this.vY*=d.inertia;this.x+=this.vX;this.y+=this.vY;if(this.type=="proton"){var y=0.002;this.vX+=(this.targetX-this.x)*y*this.electrons/d.numberEls;this.vY+=(this.targetY-this.y)*y*this.electrons/d.numberEls;this.radius+=(this.radius<this.targetRadius)?(this.targetRadius-this.radius)*y*3:0;if(i){this.targetColor=util.clone(this.oColor);}}};this.render=function(){b.fillStyle="rgba("+this.color.r+","+this.color.g+","+this.color.b+","+this.alpha+")";b.beginPath();b.arc(this.x,this.y,Math.abs(this.radius),0,Math.PI*2,true);b.closePath();b.fill();};this.dissolve=function(s,r){if(this.alpha<=0.05){s.electrons++;this.dissolved=1;}this.alpha-=0.01;};this.burst=function(){for(var r=0;r<d.numberEls;r++){var t=Math.random()*120;var s=Math.random()<=0.07;var u=new f({x:this.x+Math.sin(r)*t,y:this.y+Math.cos(r)*t,vX:Math.random()*50,vY:Math.random()*50,radius:2+Math.random()*13,color:s?util.clone(k[Math.floor(Math.random()*k.length)]):util.clone(this.color),alpha:0.1+Math.random()*0.65,type:s?"strange":"electron"});o.electrons.push(u);}this.needle=0;};this.setActive=function(r){for(var s=0;s<o.protons.length;s++){if(s!=r){o.protons[s].targetColor={r:200,g:200,b:200,a:0.5};o.protons[s].over=false;o.protons[s].hideLabel();}}this.targetColor=util.clone(this.oColor);this.over=true;this.showLabel(r);};this.showLabel=function(t){var s=g.left+o.protons[t].x;var u=g.top+o.protons[t].y;var r=a.eq(t);r.show().css({left:s+r.outerWidth()+50<window.innerWidth?s:s-r.width()+50,top:u-r.height()/2});};this.hideLabel=function(){a.hide();};};this.createParticles=function(){for(var w=0;w<l.length;w++){var x=l[w];var s=new f({x:250+-100+Math.random()*200,y:250+-100+Math.random()*200,radius:15+Math.random()*15,color:util.clone(x),alpha:0.85,type:"proton"});o.protons.push(s);this.setStage(w);for(var u=0;u<d.numberEls;u++){var A=Math.random()*100;var y=Math.random()<=0.07;var z=new f({x:s.x+Math.sin(u)*A,y:s.y+Math.cos(u)*A,radius:2+Math.random()*10,color:y?util.clone(k[Math.floor(Math.random()*k.length)]):util.clone(x),alpha:0.1+Math.random()*0.65,type:y?"strange":"electron"});o.electrons.push(z);}}for(var t=0;t<88;t++){var r=new f({x:250+Math.cos(t)*d.outerRadius,y:250+Math.sin(t)*d.outerRadius,radius:2+Math.random()*12,color:util.clone(m[Math.floor(Math.random()*m.length)]),alpha:0.2+Math.random()*0.6,type:"quark"});r.shift={x:-10+Math.random()*20,y:-10+Math.random()*20};r.x+=r.shift.x;r.y+=r.shift.y;o.quarks.push(r);}};this.setStage=function(q){var r=[{x:150,y:250,targetX:90,targetY:160,targetRadius:115},{x:250,y:150,targetX:360,targetY:150,targetRadius:140},{x:250,y:370,targetX:170,targetY:370,targetRadius:90},{x:350,y:250,targetX:350,targetY:390,targetRadius:80}];o.protons[q].x=r[q].x;o.protons[q].y=r[q].y;o.protons[q].targetX=r[q].targetX;o.protons[q].targetY=r[q].targetY;o.protons[q].targetRadius=r[q].targetRadius;};this.update=function(){b.clearRect(0,0,d.canvasW,d.canvasH);for(var x=0;x<o.protons.length;x++){if(o.protons[x].over){i=true;}o.protons[x].update(x);if(o.protons[x].needle){o.protons[x].burst();}}for(var u=0;u<o.quarks.length;u++){o.quarks[u].update(u);if(o.quarks[u].dissolved){o.quarks.splice(u,1);}}for(var w=0;w<o.electrons.length;w++){o.electrons[w].update(w);if(o.electrons[w].dissolved){o.electrons.splice(w,1);}}var s=[].concat(o.electrons,o.quarks,o.protons);for(var t=0;t<s.length;t++){s[t].render();}};this.windowResizeHandler=function(){g={left:h.offsetLeft,top:h.offsetTop};a.hide();};this.mouseMoveHandler=function(q){n.velocity.x=n.current.x-n.previous.x;n.velocity.y=n.current.y-n.previous.y;n.previous.x=n.current.x;n.previous.y=n.current.y;n.current.x=q.clientX-g.left;n.current.y=q.clientY-g.top+(document.all?document.body.scrollTop:window.pageYOffset);};this.mouseLeaveHandler=function(q){n.current.x=n.previous.x=9999;n.current.y=n.previous.y=9999;n.velocity.x=n.velocity.y=0;};};var util=new function(){this.clone=function(a){function b(d){for(var c in d){this[c]=d[c];}}return new b(a);};this.colorMatch=function(e,d){var g=Math.round;var c=false;if(g(e.r)==g(d.r)&&g(e.g)==g(d.g)&&g(e.b)==g(d.b)){c=true;}return c;};this.getVector=function(f,d){var e=d.x-f.x;var c=d.y-f.y;return{dX:e,dY:c,distance:Math.sqrt(e*e+c*c),angle:Math.atan2(c,e)};};this.log=function(){if(typeof("console")!="undefined"){if(arguments&&arguments[0]==="clear"){console.clear();}else{console.log(arguments);}}};};
