var themes=[["#2167a4","#f8c524","#cb2a28","#169045","#ffffff"]],body=document.body,FPS=20,contentPadding=20,ballsFront=10,ballsBehind=8,ballDensity=0.5,ballFriction=0.2,ballRestitution=0.3,ballRotation=0.05,canvas,delta=[0,0],stage=[window.screenX,window.screenY,window.innerWidth,window.innerHeight];getBrowserDimensions();var source=document.getElementById("bod"),sourceParent=source.parentNode,items=source.getElementsByTagName("div"),theme=themes[Math.floor(Math.random()*themes.length)],worldAABB,world,iterations=1,timeStep=1/FPS,walls=[],wall_thickness=200,wallsSetted=false,bodies,elements,content,createMode=false,destroyMode=false,isMouseDown=false,mouseJoint,mouseX=0,mouseY=0,PI2=Math.PI*2,timeOfLastTouch=0,dir=1,support=null;if(canvasTextSupport()){source.style.visibility="hidden";body.style.overflow="hidden";init();play()}else{source.style.visibility="visible";support=document.createElement("div");support.innerHTML='<div class="support">Your browser does not support the features on this site :(  Please download a newer, safer, and free browser like <a href="http://www.google.com/chrome/intl/en/landing_chrome.html?hl=en">chrome</a></div>';document.body.insertBefore(support,source)}function canvasSupport(){return !!document.createElement("canvas").getContext}function canvasTextSupport(){if(!canvasSupport()){return false}var b=document.createElement("canvas"),a=b.getContext("2d");return typeof a.fillText=="function"}function init(){var a=document;canvas=a.getElementById("canvas");a.onmousedown=onMouseDown;a.onmouseup=onMouseUp;a.onmousemove=onMouseMove;a.onkeydown=onKeyEvent;a.onkeypress=onKeyEvent;a.addEventListener("touchstart",onTouchStart,false);a.addEventListener("touchmove",onTouchMove,false);a.addEventListener("touchend",onTouchEnd,false);worldAABB=new b2AABB();worldAABB.minVertex.Set(-200,-200);worldAABB.maxVertex.Set(screen.width+200,screen.height+200);world=new b2World(worldAABB,new b2Vec2(0,0),true);setWalls();reset()}function getstyle(b,a){if(b.currentStyle){this.getstyle=function(d,c){return d.currentStyle[c]}}else{this.getstyle=function(d,c){return window.getComputedStyle(d,null)[c]}}return getstyle(b,a)}function convertContent(){var f,d,b,e,a;for(var c=items.length-1;c>-1;c--){items[c].style.padding=0;items[c].style.clear="none";f=items[c].getAttribute("class");d=items[c].clientWidth;b=getstyle(items[c],"backgroundColor");e=items[c].innerHTML;addContentCrc(f,d,b,e)}}function play(){setInterval(loop,1000/40)}function reset(){var b=0,a;if(bodies){for(b=0;b<bodies.length;b++){a=bodies[b];canvas.removeChild(a.GetUserData().element);world.DestroyBody(a);a=null}}bodies=[];elements=[];for(b=0;b<ballsBehind;b++){createCrc()}convertContent();for(b=0;b<ballsFront;b++){createCrc()}}function addContentCrc(g,j,c,e){var d=document.createElement("div"),a=document.createElement("canvas"),h=a.getContext("2d"),b=new b2BodyDef(),f=new b2CircleDef();d.className=g;d.width=d.height=j;d.style.position="absolute";d.style.left=-j+"px";d.style.top=-j+"px";d.style.cursor="default";canvas.appendChild(d);elements.push(d),val="";a.width=j;a.height=j;h.fillStyle=c;h.beginPath();h.arc(j*0.5,j*0.5,j*0.5,0,PI2,true);h.closePath();h.fill();d.appendChild(a);content=document.createElement("div");content.className="content";content.onSelectStart=null;content.innerHTML=e;d.appendChild(content);content.style.width=(j-contentPadding*2)+"px";content.style.left=(((j-content.clientWidth)/2))+"px";content.style.top=((j-content.clientHeight)/2)+"px";if(g==="reset"){d.addEventListener("click",reset,false)}else{if(g==="dir"){val=content.lastChild.nodeValue;if(val.indexOf("up")>-1){d.addEventListener("click",function(){dir=-1},false)}else{if(val.indexOf("down")>-1){d.addEventListener("click",function(){dir=1},false)}}}}f.radius=j/2;f.density=ballDensity;f.friction=ballFriction;f.restitution=ballRestitution;b.AddShape(f);b.userData={element:d};b.position.Set(Math.random()*stage[2],Math.random()*(stage[3]-j)+j/2);b.linearVelocity.Set(Math.random()*200,Math.random()*200);bodies.push(world.CreateBody(b))}function createCrc(a,g){var a=a||Math.random()*stage[2],g=g||Math.random()*500,d=(Math.random()*100>>0)+20,c=document.createElement("canvas"),b=c.getContext("2d"),e=new b2BodyDef(),f=new b2CircleDef();c.width=d;c.height=d;c.style.position="absolute";c.style.left=-200+"px";c.style.top=-200+"px";b.fillStyle=theme[Math.floor(Math.random()*theme.length)];b.beginPath();b.arc(d*0.5,d*0.5,d*0.5,0,PI2,true);b.closePath();b.fill();canvas.appendChild(c);elements.push(c);f.radius=d>>1;f.density=ballDensity;f.friction=ballFriction;f.restitution=ballRestitution;e.AddShape(f);e.userData={element:c};e.position.Set(a,g);e.linearVelocity.Set(Math.random()*400-200,Math.random()*400-200);bodies.push(world.CreateBody(e))}function loop(){delta[0]+=(0-delta[0])*0.5;delta[1]+=(0-delta[1])*0.5;world.m_gravity.x=0;world.m_gravity.y=dir*(100-delta[1]);mouseDrag();world.Step(timeStep,iterations);var a,b,c="";for(i=0;i<bodies.length;i++){a=bodies[i];b=elements[i];b.style.left=(a.m_position0.x-(b.width>>1))+"px";b.style.top=(a.m_position0.y-(b.height>>1))+"px";if(ballRotation&&b.tagName=="DIV"){c="rotate("+(a.m_rotation0*57.2957795)+"deg)";b.style.WebkitTransform=c;b.style.MozTransform=c;b.style.OTransform=c}}}function createBox(g,b,h,e,a,f){if(typeof(f)=="undefined"){f=true}var d=new b2BoxDef(),c=new b2BodyDef();if(!f){d.density=1}d.extents.Set(e,a);c.AddShape(d);c.position.Set(b,h);return g.CreateBody(c)}function onKeyEvent(a){switch(a.which){case 40:dir=1;break;case 38:dir=-1;break}}function onMouseDown(){isMouseDown=true;return false}function onMouseUp(){isMouseDown=false;return false}function onMouseMove(a){mouseX=a.clientX;mouseY=a.clientY}function onTouchStart(a){if(a.touches.length==1){a.preventDefault();mouseX=a.touches[0].pageX;mouseY=a.touches[0].pageY;isMouseDown=true}}function onTouchMove(a){if(a.touches.length==1){a.preventDefault();mouseX=a.touches[0].pageX;mouseY=a.touches[0].pageY}}function onTouchEnd(a){if(a.touches.length==0){a.preventDefault();isMouseDown=false}}function mouseDrag(){if(createMode){createCrc(mouseX,mouseY)}else{if(isMouseDown&&!mouseJoint){var a=getBodyAtMouse();if(a){var b=new b2MouseJointDef();b.body1=world.m_groundBody;b.body2=a;b.target.Set(mouseX,mouseY);b.maxForce=30000*a.m_mass;b.timeStep=timeStep;mouseJoint=world.CreateJoint(b);a.WakeUp()}else{createMode=true}}}if(!isMouseDown){createMode=false;destroyMode=false;if(mouseJoint){world.DestroyJoint(mouseJoint);mouseJoint=null}}if(mouseJoint){var c=new b2Vec2(mouseX,mouseY);mouseJoint.SetTarget(c)}}function getBodyAtMouse(){var f=new b2Vec2(),c=new b2AABB();f.Set(mouseX,mouseY);c.minVertex.Set(mouseX-1,mouseY-1);c.maxVertex.Set(mouseX+1,mouseY+1);var g=10,b=[],e=world.Query(c,b,g),a=null,d=0;for(d=0;d<e;++d){if(b[d].m_body.IsStatic()==false){if(b[d].TestPoint(f)){a=b[d].m_body;break}}}return a}function setWalls(){if(wallsSetted){world.DestroyBody(walls[0]);world.DestroyBody(walls[1]);world.DestroyBody(walls[2]);world.DestroyBody(walls[3]);walls[0]=null;walls[1]=null;walls[2]=null;walls[3]=null}walls[0]=createBox(world,stage[2]/2,-wall_thickness,stage[2],wall_thickness);walls[1]=createBox(world,stage[2]/2,stage[3]+wall_thickness,stage[2],wall_thickness);walls[2]=createBox(world,-wall_thickness,stage[3]/2,wall_thickness,stage[3]);walls[3]=createBox(world,stage[2]+wall_thickness,stage[3]/2,wall_thickness,stage[3]);wallsSetted=true}function getBrowserDimensions(){var a=false;if(stage[0]!=window.screenX){delta[0]=(window.screenX-stage[0])*50;stage[0]=window.screenX;a=true}if(stage[1]!=window.screenY){delta[1]=(window.screenY-stage[1])*50;stage[1]=window.screenY;a=true}if(stage[2]!=window.innerWidth){stage[2]=window.innerWidth;a=true}if(stage[3]!=window.innerHeight){stage[3]=window.innerHeight;a=true}return a};
