soundManager.onload=function(){mySound=soundManager.createSound({id:"aSound",url:"fuck.mp3",volume:80})};soundManager.onerror=function(){disableSoundInMyApp()};window.addEvent("domready",function(){var a=new Accordion($("content"),"h3.toggler","div.element",{opacity:false,onActive:function(c,b){},onBackground:function(c,b){}});requ=new Request({method:"post",url:"wapi.php",data:{hiscore:1},onRequest:function(){},onComplete:function(b){$("hiscoretable").innerHTML=b}}).send()});function xy(b,a){this.x=b;this.y=a}function initGame(){level=parseInt($("level").value,10);clear();$clear(Game);Game=null;position=new Array();score=0;wormLength=1;apple=new xy(1,1);lastpos=new xy(start,start);position.push(new xy(lastpos.x,lastpos.y));document.addEvent("keydown",function a(b){lastKey=b.key});direction="down";appleTimer=size-1;time=new Date();time=time.getSeconds();Game=loop.periodical(100/level);$("score").innerHTML=score;drawApple()}function clear(){for(var b=0;b<size;b++){for(var a=0;a<size;a++){$("c_"+b+"_"+a).removeClass("worm");$("c_"+b+"_"+a).removeClass("apple")}$("a_"+b).addClass("apple")}}var t=0;function gameLoop(){handleInput();checkCollision();drawWorm();if(t==10){$("a_"+appleTimer+"").removeClass("apple");if(appleTimer>0){appleTimer--}t=0}t++}function handleInput(){if(lastKey=="up"||lastKey=="down"||lastKey=="left"||lastKey=="right"){direction=lastKey}if(direction=="up"){if(lastpos.y==0){lastpos.y=size-1}else{lastpos.y--}position.push(new xy(lastpos.x,lastpos.y))}if(direction=="down"){if(lastpos.y==size-1){lastpos.y=0}else{lastpos.y++}position.push(new xy(lastpos.x,lastpos.y))}if(direction=="left"){if(lastpos.x==0){lastpos.x=size-1}else{lastpos.x--}position.push(new xy(lastpos.x,lastpos.y))}if(direction=="right"){if(lastpos.x==size-1){lastpos.x=0}else{lastpos.x++}position.push(new xy(lastpos.x,lastpos.y))}}function checkCollision(){if(lastpos.x==apple.x&&lastpos.y==apple.y){if(soundeffects){mySound.play()}$("c_"+apple.y+"_"+apple.x).removeClass("apple");score=score+level+appleTimer;wormLength++;var b=parseInt(Math.random()*size,10);var a=parseInt(Math.random()*size,10);while(!isFree(b,a)){b=parseInt(Math.random()*size,10);a=parseInt(Math.random()*size,10)}apple.x=b;apple.y=a;appleTimer=size-1;fillAppleTimer();drawApple()}if(wormLength>1&&collided()){if(soundeffects){mySound.play()}$clear(Game);Game=null;document.removeEvents("keydown");$("finalscore").innerHTML=score;$("submitscore").removeClass("hide");$("submitinfo").innerHTML=""}}function fillAppleTimer(){for(var a=0;a<size;a++){$("a_"+a).addClass("apple")}}function collided(){var a=position[position.length-1].x;var c=position[position.length-1].y;for(var b=0;b<position.length-1;b++){if(position[b].x==a&&position[b].y==c){return true}}return false}function isFree(a,c){for(var b=0;b<position.length;b++){if(position[b].x==a&&position[b].y==c){return false}}return true}function drawWorm(){if(position.length>wormLength){var a=position.shift();$("c_"+a.y+"_"+a.x).removeClass("worm")}$("c_"+lastpos.y+"_"+lastpos.x).addClass("worm")}function drawApple(){$("c_"+apple.y+"_"+apple.x).addClass("apple");$("score").innerHTML=score}function saveScore(){var b=new Date();b=b.getSeconds()-time;var a=new Request({method:"post",url:"wapi.php",data:{score:score,name:$("name").value,time:b,level:level,save:1},onRequest:function(){$("submitinfo").innerHTML="<h2>Submitting.. please wait..</h2>"},onComplete:function(c){$("submitscore").addClass("hide");requ.send()}}).send()};