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");function Ball(b,d,a){this.r=b;this.c=d;this.num=(b-1)*9+d;this.color=a}Ball.prototype.refresh=function(){document.images[this.num].src=ImagesDir+this.color+"Ball.gif"};Ball.prototype.clear=function(){this.color=null;document.images[this.num].src=BackgroundImage};Ball.prototype.isClear=function(){return this.color===null};Ball.prototype.setColor=function(a){this.color=a;document.images[this.num].src=ImagesDir+a+"Ball.gif"};Ball.prototype.select=function(){soundManager.play(this.color);document.images[this.num].src=ImagesDir+this.color+"Selected.gif"};Ball.prototype.deselect=function(){document.images[this.num].src=ImagesDir+this.color+"Ball.gif"};Ball.prototype.fade=function(){document.images[this.num].src=ImagesDir+this.color+"Fade.gif";this.color=null};Ball.prototype.fadeColor=function(a){document.images[this.num].src=ImagesDir+a+"Fade.gif";this.color=null};function preload(){var c=[],b,a=0;for(b=0;b<Colors.length;b++){c[a]=new Image();c[a].src=ImagesDir+Colors[b]+"Ball.gif";++a;c[a]=new Image();c[a].src=ImagesDir+Colors[b]+"Selected.gif";++a;c[a]=new Image();c[a].src=ImagesDir+Colors[b]+"Fade.gif";++a}}function randomFromZero(a){return Math.round(a*Math.random())}function resetSS(){SSStart=0;SSMid="0";SSEnd=Score}function refreshScores(){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}function direction(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].color==Tile[d+k][h+g].color){n[a]=d+k;n[a+1]=h+g;a+=2;l++;k+=f;g+=e}k=-f;g=-e;while(d+k>0&&d+k<10&&h+g>0&&h+g<10&&Tile[d][h].color==Tile[d+k][h+g].color){n[a]=d+k;n[a+1]=h+g;a+=2;l++;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;++SSStart;refreshScores();for(m=0;m<n.length;m+=2){Tile[n[m]][n[m+1]].fade();Balls--}LinesInOneMove++;return true}return false}function row(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].color+"Line");Tile[e][h].fade();Balls--;return true}else{if(LinesInTurn>0){Score+=ScoreInTurn*LinesInTurn;LinesInTurn=0;ScoreInTurn=0;refreshScores()}return false}}function addballs(){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;++b;row(a,c);if(Balls>80){submitscore(1);break}}}for(b=0;b<3;b++){c=randomFromZero(6);Nextballs[b]=Colors[c];document.images[82+b].src=ImagesDir+Colors[c]+"Ball.gif"}}function newgame(){SelBall=null;Balls=0;ScoreInTurn=0;Score=0;resetSS();refreshScores();var a,b;for(a=1;a<10;a++){for(b=1;b<10;b++){Tile[a][b].clear()}}for(a=0;a<3;a++){Nextballs[a]=Colors[randomFromZero(6)]}addballs();addballs()}function submitscore(b){if(HasMoved){document.forms.alienballs.submit()}else{newgame()}}function getCookie(a){var b=document.cookie;if(b.indexOf(a)!=-1){var d=b.indexOf(a)+a.length+1;var c=b.indexOf(";",d)-1;if(c==-2){c=b.length}return unescape(b.substring(d,c))}else{return false}}function onloadscript(){if(getCookie("sound")=="off"){soundManager.mute()}var a,b;for(a=1;a<10;a++){Tile[a]=[];for(b=1;b<10;b++){Tile[a][b]=new Ball(a,b,null)}}preload();newgame()}function moveball(c){var a=SelBall.color,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}function findPath(a,j){var d=true,g,l,e=[],f=0,k,b,m=[];for(g=0;g<11;g++){e[g]=[];for(l=0;l<11;l++){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.r][SelBall.c]=99;while(d){d=false;for(l=1;l<10;l++){for(g=1;g<10;g++){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--){for(g=9;g>0;g--){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.r][SelBall.c]<99){g=SelBall.r;l=SelBall.c;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}function clicked(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 b=findPath(a,e);if(b!==null){moveball(b);var 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]}}}}function setGraphics(a){ImagesDir="images/"+a+"/";preload();var b,d;for(b=1;b<10;b++){for(d=1;d<10;d++){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"}}function toggle_sound(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++){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")};window.onload=onloadscript;