var HasMoved=false,Tile=[],SelBall=null,Colors=["Red","Blue","Green","Yellow","Purple","Orange","Pink"],Nextballs=[],Balls=0,Score=0,ScoreInTurn=0,LinesInTurn=0,LinesInOneMove=0,SSStart,SSMid,SSEnd,ImagesDir="images/alienballs/",BackgroundImage="images/CellBk.gif",RulesDiv=document.getElementById("ov_rules"),ScorDiv=document.getElementById("ov_scor"),ball=function(g,e,b){var f=g,h=e,d=(g-1)*9+e,a=b;return{getRow:function(){return f},getColumn:function(){return h},refresh:function(){document.images[d].src=ImagesDir+a+"Ball.gif"},clear:function(){a=null;document.images[d].src=BackgroundImage},isClear:function(){return a===null},getColor:function(){return a},setColor:function(c){a=c;document.images[d].src=ImagesDir+c+"Ball.gif"},select:function(){soundManager.play(a);document.images[d].src=ImagesDir+a+"Selected.gif"},deselect:function(){document.images[d].src=ImagesDir+a+"Ball.gif"},fade:function(){document.images[d].src=ImagesDir+a+"Fade.gif";document.images[d].src=BackgroundImage;a=null}}};var preload=function(){var c=[],b,a=0;for(b=0;b<Colors.length;b+=1){c[a]=new Image();c[a].src=ImagesDir+Colors[b]+"Ball.gif";a+=1;c[a]=new Image();c[a].src=ImagesDir+Colors[b]+"Selected.gif";a+=1;c[a]=new Image();c[a].src=ImagesDir+Colors[b]+"Fade.gif";a+=1}};var randomFromZero=function(a){return Math.round(a*Math.random())};var resetSS=function(){SSStart=0;SSMid="0";SSEnd=Score};var refreshScores=function(){document.forms.alienballs.score.value=Score;document.getElementById("your-score").innerHTML=Score;var a="Calc: "+(Score+ScoreInTurn*LinesInTurn);if(Score+ScoreInTurn*LinesInTurn!==0){a+=" = "}if(SSStart>1){a+=SSStart+" * "}if(SSMid!=="0"){a+=SSMid}if(SSEnd!==0){a+=" + "+SSEnd}document.getElementById("score-det").innerHTML=a};var direction=function(f,e,d,h){var m,k=f,g=e,l=1,a=0,n=[];while(d+k>0&&d+k<10&&h+g>0&&h+g<10&&Tile[d][h].getColor()===Tile[d+k][h+g].getColor()){n[a]=d+k;n[a+1]=h+g;a+=2;l+=1;k+=f;g+=e}k=-f;g=-e;while(d+k>0&&d+k<10&&h+g>0&&h+g<10&&Tile[d][h].getColor()===Tile[d+k][h+g].getColor()){n[a]=d+k;n[a+1]=h+g;a+=2;l+=1;k-=f;g-=e}if(l>4){if(LinesInTurn===0){resetSS()}ScoreInTurn+=l*l*l;if(SSMid==="0"){SSMid=l+"<sup>3</sup>"}else{SSMid="("+l+"<sup>3</sup> + "+SSMid+")"}LinesInTurn+=1;SSStart+=1;refreshScores();for(m=0;m<n.length;m+=2){Tile[n[m]][n[m+1]].fade();Balls-=1}LinesInOneMove+=1;return true}return false};var row=function(e,h){var g,f=direction(0,1,e,h),d=direction(1,1,e,h),b=direction(1,0,e,h),a=direction(1,-1,e,h);if(LinesInOneMove>1){soundManager.play("multiline");g=Math.pow(10,LinesInOneMove-1);SSMid=g+" * "+SSMid;ScoreInTurn*=g;LinesInTurn-=LinesInOneMove-1;SSStart-=SSStart-1;refreshScores()}LinesInOneMove=0;if(f||d||b||a){if(LinesInTurn>1){soundManager.play("another_line_in_turn")}soundManager.play(Tile[e][h].getColor()+"Line");Tile[e][h].fade();Balls-=1;return true}else{if(LinesInTurn>0){Score+=ScoreInTurn*LinesInTurn;LinesInTurn=0;ScoreInTurn=0;refreshScores()}return false}};var addballs=function(){var b=0,c,a;while(b<3){c=randomFromZero(8)+1;a=randomFromZero(8)+1;if(Tile[a][c].isClear()){Tile[a][c].setColor(Nextballs[b]);Balls+=1;b+=1;row(a,c);if(Balls>80){submitscore(1);break}}}for(b=0;b<3;b+=1){c=randomFromZero(6);Nextballs[b]=Colors[c];document.images[82+b].src=ImagesDir+Colors[c]+"Ball.gif"}};var newgame=function(){SelBall=null;Balls=0;ScoreInTurn=0;Score=0;resetSS();refreshScores();var a,b;for(a=1;a<10;a+=1){for(b=1;b<10;b+=1){Tile[a][b].clear()}}for(a=0;a<3;a+=1){Nextballs[a]=Colors[randomFromZero(6)]}addballs();addballs()};var submitscore=function(b){if(HasMoved){document.forms.alienballs.submit()}else{newgame()}};var moveball=function(c){var a=SelBall.getColor(),b;for(b=0;b<c.length;b+=2){if(b+2!==c.length){Tile[c[b]][c[b+1]].clear()}else{Tile[c[b]][c[b+1]].setColor(a)}}HasMoved=true};var findPath=function(a,j){var d=true,g,l,e=[],f=0,k,b,m=[];for(g=0;g<11;g+=1){e[g]=[];for(l=0;l<11;l+=1){if(g>0&&g<10&&l>0&&l<10&&Tile[g][l].isClear()){e[g][l]=99}else{e[g][l]=-1}}}e[a][j]=0;e[SelBall.getRow()][SelBall.getColumn()]=99;while(d){d=false;for(l=1;l<10;l+=1){for(g=1;g<10;g+=1){if(e[g][l]>0){if(e[g-1][l]>=0){if(e[g][l]>e[g-1][l]+1){e[g][l]=e[g-1][l]+1;d=true}}if(e[g][l-1]>=0){if(e[g][l]>e[g][l-1]+1){e[g][l]=e[g][l-1]+1;d=true}}}}}for(l=9;l>0;l-=1){for(g=9;g>0;g-=1){if(e[g][l]>0){if(e[g+1][l]>=0){if(e[g][l]>e[g+1][l]+1){e[g][l]=(e[g+1][l]+1);d=true}}if(e[g][l+1]>=0){if(e[g][l]>e[g][l+1]+1){e[g][l]=e[g][l+1]+1;d=true}}}}}}if(e[SelBall.getRow()][SelBall.getColumn()]<99){g=SelBall.getRow();l=SelBall.getColumn();while(e[g][l]>0){m[f]=g;m[f+1]=l;f+=2;k=g;b=l;if(e[g+1][l]<e[g][l]&&e[g+1][l]>=0){k=g+1;b=l}if(e[g][l+1]<e[k][b]&&e[g][l+1]>=0){k=g;b=l+1}if(e[g-1][l]<e[k][b]&&e[g-1][l]>=0){k=g-1;b=l}if(e[g][l-1]<e[k][b]&&e[g][l-1]>=0){k=g;b=l-1}g=k;l=b}m[f]=a;m[f+1]=j;return m}return null};var clicked=function(a,e){if(SelBall===null){if(!Tile[a][e].isClear()){Tile[a][e].select();SelBall=Tile[a][e]}}else{if(Tile[a][e].isClear()){var d,b=findPath(a,e);if(b!==null){moveball(b);d=row(a,e);SelBall=null;if(!d){addballs()}}else{soundManager.play("fail")}}else{SelBall.deselect();if(Tile[a][e]===SelBall){SelBall=null}else{Tile[a][e].select();SelBall=Tile[a][e]}}}};var setGraphics=function(a){ImagesDir="images/"+a+"/";preload();var b,d;for(b=1;b<10;b+=1){for(d=1;d<10;d+=1){if(!Tile[b][d].isClear()){Tile[b][d].refresh()}}}for(b=0;b<3;b++){document.images[82+b].src=ImagesDir+Nextballs[b]+"Ball.gif"}};var toggle_sound=function(c){var b=document.getElementById("sound-status-on"),a=document.getElementById("sound-status-off");if(c==="on"){b.style.display="block";a.style.display="none";soundManager.unmute()}else{b.style.display="none";a.style.display="block";soundManager.mute()}document.cookie="sound="+c+";path=/;expires=Fri 24 Mar 2134 23:22:50 GMT"};soundManager.url="/swf/";soundManager.flashVersion=9;soundManager.debugMode=false;soundManager.onload=function(){soundManager.createSound("fail","/sounds/fail.mp3");soundManager.createSound("new_game","/sounds/new_game.mp3");soundManager.createSound("multiline","/sounds/multiline.mp3");soundManager.createSound("another_line_in_turn","/sounds/another_line_in_turn.mp3");for(var a=0;a<Colors.length;a+=1){soundManager.createSound(Colors[a],"/sounds/"+Colors[a]+".mp3");soundManager.createSound(Colors[a]+"Line","/sounds/"+Colors[a]+"Line.mp3")}soundManager.play("new_game","/sounds/new_game.mp3")};var getCookie=function(c){var a=new RegExp("[; ]"+c+"=([^\\s;]*)");var b=(" "+document.cookie).match(a);if(c&&b){return unescape(b[1])}return""};window.onload=function(){if(getCookie("sound")==="off"){soundManager.mute()}var a,b;for(a=1;a<10;a+=1){Tile[a]=[];for(b=1;b<10;b+=1){Tile[a][b]=ball(a,b,null)}}preload();newgame()};
