var a,FIELDWIDTH=20,FIELDHEIGHT=20,BLOCK_EMPTY=0,BLOCK_USER=1,BLOCK_TARGET=2,BLOCK_CRATE=4,BLOCK_WALL=8,BLOCK_NOSHOW=9,DIR_NULL=-1,DIR_UP=0,DIR_DOWN=1,DIR_LEFT=2,DIR_RIGHT=3,DIR_PUSH=4;function USER(){this.y=this.x=0}
function SOKOBAN(){this.field=new FIELD;this.user=new USER;this.key_left=CookieRead("MooverKey_LEFT")==null?37:parseInt(CookieRead("MooverKey_LEFT"));this.key_up=CookieRead("MooverKey_UP")==null?38:parseInt(CookieRead("MooverKey_UP"));this.key_right=CookieRead("MooverKey_RIGHT")==null?39:parseInt(CookieRead("MooverKey_RIGHT"));this.key_down=CookieRead("MooverKey_DOWN")==null?40:parseInt(CookieRead("MooverKey_DOWN"));this.key_undo=CookieRead("MooverKey_UNDO")==null?85:parseInt(CookieRead("MooverKey_UNDO"));
this.key_redo=CookieRead("MooverKey_REDO")==null?79:parseInt(CookieRead("MooverKey_REDO"));this.key_reset=CookieRead("MooverKey_RESET")==null?27:parseInt(CookieRead("MooverKey_RESET"));this.undolist=this.movelist="";this.cowblinktimer=this.displaypushes=this.displaymoves=this.pushes=this.moves=0;this.cowpicture=[];this.bestpushdatespan=this.bestpushnamespan=this.bestpushspanpush=this.bestpushspanmove=this.bestmovedatespan=this.bestmovenamespan=this.bestmovespanpush=this.bestmovespanmove=this.pushspan=
this.movespan=undefined}SOKOBAN.prototype.fillinside=function fillinside(){for(var b=0;b<this.field.data.length;b++)if(this.field.data[b]==0)this.field.data[b]=BLOCK_NOSHOW;for(b=0;b<this.field.view.length;b++)game.field.view[b]=-2;this.recursivefill(this.user.x,this.user.y)};
SOKOBAN.prototype.recursivefill=function recursivefill(b,c){if(this.field.data[b+c*FIELDHEIGHT]==BLOCK_NOSHOW)this.field.data[b+c*FIELDHEIGHT]=0;this.field.view[b+c*FIELDHEIGHT]=-1;b>0&&this.field.data[b-1+c*FIELDHEIGHT]!=8&&this.field.view[b-1+c*FIELDHEIGHT]==-2&&this.recursivefill(b-1,c);c>0&&this.field.data[b+(c-1)*FIELDHEIGHT]!=8&&this.field.view[b+(c-1)*FIELDHEIGHT]==-2&&this.recursivefill(b,c-1);b+1<FIELDWIDTH&&this.field.data[b+1+c*FIELDHEIGHT]!=8&&this.field.view[b+1+c*FIELDHEIGHT]==-2&&this.recursivefill(b+
1,c);c+1<FIELDWIDTH&&this.field.data[b+(c+1)*FIELDHEIGHT]!=8&&this.field.view[b+(c+1)*FIELDHEIGHT]==-2&&this.recursivefill(b,c+1)};FIELD.prototype.putsquare=function putsquare(b,c,d){this.image[b+c*this.width].src=IMAGES[d].src};a=SOKOBAN.prototype;
a.moveuser=function moveuser(b){var c,d,e,g,i,f,h=DIR_NULL;b&=~DIR_PUSH;i=(c=this.user.x)+(d=this.user.y)*FIELDHEIGHT;b==DIR_UP&&d>0&&d--;b==DIR_DOWN&&d<FIELDHEIGHT&&d++;b==DIR_LEFT&&c>0&&c--;b==DIR_RIGHT&&c<FIELDWIDTH&&c++;f=(e=c)+(g=d)*FIELDHEIGHT;if(this.field.data[f]==BLOCK_EMPTY||this.field.data[f]==BLOCK_TARGET){this.field.data[i]^=BLOCK_USER;this.field.data[f]|=BLOCK_USER;h=b}else if(this.field.data[f]&BLOCK_CRATE){b==DIR_UP&&g>0&&g--;b==DIR_DOWN&&g<FIELDHEIGHT&&g++;b==DIR_LEFT&&e>0&&e--;b==
DIR_RIGHT&&e<FIELDWIDTH&&e++;e=e+g*FIELDHEIGHT;if(this.field.data[e]==BLOCK_EMPTY||this.field.data[e]==BLOCK_TARGET){h=b+DIR_PUSH;this.field.data[i]^=BLOCK_USER;this.field.data[f]|=BLOCK_USER;this.field.data[f]^=BLOCK_CRATE;this.field.data[e]|=BLOCK_CRATE}}if(h!=DIR_NULL){this.user.x=c;this.user.y=d}return h};
a.undomove=function undomove(){if(!this.movelist.length)return 0;var b=parseInt(right(this.movelist,1)),c,d,e;if(b&DIR_PUSH){e=(c=this.user.x)+(d=this.user.y)*FIELDHEIGHT;if(b==(DIR_PUSH|DIR_UP)){d--;this.moveuser(DIR_DOWN)}if(b==(DIR_PUSH|DIR_DOWN)){d++;this.moveuser(DIR_UP)}if(b==(DIR_PUSH|DIR_LEFT)){c--;this.moveuser(DIR_RIGHT)}if(b==(DIR_PUSH|DIR_RIGHT)){c++;this.moveuser(DIR_LEFT)}c=c+d*FIELDHEIGHT;this.field.data[c]^=BLOCK_CRATE;this.field.data[e]|=BLOCK_CRATE}else{b==DIR_UP&&this.moveuser(DIR_DOWN);
b==DIR_DOWN&&this.moveuser(DIR_UP);b==DIR_LEFT&&this.moveuser(DIR_RIGHT);b==DIR_RIGHT&&this.moveuser(DIR_LEFT)}this.undolist+=b;b&DIR_PUSH&&game.pushes--;--game.moves;this.movelist=left(this.movelist,this.movelist.length-1);return 1};a.redomove=function redomove(){if(!this.undolist.length)return 0;var b=parseInt(right(this.undolist,1));m=game.moveuser(b);if(m!=DIR_NULL)game.movelist+=m.toString();m&DIR_PUSH&&game.pushes++;++game.moves;this.undolist=left(this.undolist,this.undolist.length-1)};
a.fillinfobox=function fillinfobox(){var b="";b+="<div style='border:1px solid black;background:#FFFFFF;'><a href='http://www.bitbar.org/moover/'><img id=cow1 src='./images/happycow1.jpg' style='display:none;' border=0><img id=cow2 src='./images/happycow2.jpg' style='display:inline;' border=0><img src='./images/moover.jpg' border=0></a></div>";b+="<div style='border:1px solid black;height:4;font:3px Tahoma;'></div>";b+="<div style='border:1px solid black;'>Level Number: <b><span id=levelnumber>"+
LEVEL_NUMBER+"</span></b></div>";if(LEVEL_PASSWORD!="")b+="<div style='border:1px solid black;'>Level Password: <span id=levelpassword>"+LEVEL_PASSWORD+"</span></div>";b+="<div style='border:1px solid black;'>Current Moove Count: <span id=movespan>0</span></div>";b+="<div style='border:1px solid black;'>Current Push Count: <span id=pushspan>0</span></div>";b+="<div style='border:1px solid black;height:6;font:3px Tahoma;'></div>";b+="<div style='border:1px solid black;'>Least Mooves Record: <span id=bestmovespanmove>none</span></div>";
b+="<div style='border:1px solid black;'>Least Pushes Record: <span id=bestpushspanpush>none</span></div>";b+="<div style='border:1px solid black;height:6;font:3px Tahoma'></div>";b+="<div style='border:1px solid black;'><input type=text id=moveoutput style='width:100%;border:1px solid #663333' onfocus=\"this.style.border='1px solid #CC6666';this.style.color='#EEEEEE';\" onblur=\"this.style.border='1px solid #663333';this.style.color='#888888';\"></div>";b+="<div style='border:1px solid black;'><input type=text id=moveinput  style='width:100%;border:1px solid #333366' onfocus=\"this.style.border='1px solid #6666CC';this.style.color='#EEEEEE';\" onblur=\"this.style.border='1px solid #333366';this.style.color='#888888';\"></div>";
this.field.messageframe.innerHTML=b;this.cowpicture[0]=document.getElementById("cow1");this.cowpicture[1]=document.getElementById("cow2");this.movespan=document.getElementById("movespan");this.pushspan=document.getElementById("pushspan");this.bestmovespanmove=document.getElementById("bestmovespanmove");this.bestpushspanpush=document.getElementById("bestpushspanpush");this.moveinput=document.getElementById("moveinput");this.moveoutput=document.getElementById("moveoutput");this.bestmovespanmove.innerHTML=
RECORD_MOVE_MOVE>0?RECORD_MOVE_MOVE:"none";this.bestpushspanpush.innerHTML=RECORD_PUSH_PUSH>0?RECORD_PUSH_PUSH:"none"};a.gamecomplete=function gamecomplete(){var b;for(b=0;b<this.field.height*this.field.width;b++)if(this.field.data[b]==BLOCK_TARGET||this.field.data[b]==BLOCK_CRATE)return 0;return 1};
a.completemessage=function completemessage(){var b="",c=0,d=0,e="";if(!RECORD_MOVE_MOVE||RECORD_MOVE_MOVE>game.moves)c=1;if(!RECORD_PUSH_PUSH||RECORD_PUSH_PUSH>game.pushes)d=1;switch(c+d*2){case 0:e="Congratulations! You have completed the level. Click 'OK' to proceed to the next level.";break;case 1:e="Congratulations! You have set a new record for this level by completing it with the fewest moves. Please enter your name in the box below to submit your score and proceed to the next level.";break;
case 2:e="Congratulations! You have set a new record for this level by completing it with the fewest pushes. Please enter your name in the box below to submit your score and proceed to the next level.";break;case 3:e="Congratulations! You have set a new record for this level by completing it with the fewest moves and fewest pushes. Please enter your name in the box below to submit your score and proceed to the next level.";break}c+d?this.field.createinputbox(250,130):this.field.createinputbox(250,
85);b+="<form method=post action=garbage.php>";b+="<table width=100% height=100% border=0 cellspacing=0 cellpadding=10>";b+="<tr>";b+="<td align=justify valign=middle colspan=2>";b+=e;b+="</td>";b+="</tr>";b+="<tr>";if(c+d)b+="<td align=center valign=middle>";if(c+d)b+="<input type=text name=playername value='Anonymous' size=25 onfocus=\"this.style.border='1px solid #DDDDDD';this.style.color='#EEEEEE';\" onblur=\"this.style.border='1px solid #888888';this.style.color='#888888';\">";if(c+d)b+="</td>";
b+=c+d?"<td align=center valign=middle>":"<td align=center valign=middle colspan=2>";b+="<input type=hidden name=movedata value='"+game.movelist+"'>";b+="<input type=hidden name=levelnumber value='"+LEVEL_NUMBER+"'>";b+="<input type=submit value='  OK  ' onfocus=\"this.style.border='1px solid #DDDDDD';this.style.color='#EEEEEE';\" onblur=\"this.style.border='1px solid #888888';this.style.color='#888888';\" onmouseover=\"this.style.border='1px solid #DDDDDD';this.style.color='#EEEEEE';\" onmouseout=\"this.style.border='1px solid #888888';this.style.color='#888888';\">";
b+="</td>";b+="</tr>";b+="</table>";b+="</form>";this.field.inputboxinnerframe.innerHTML=b};
a.intromessage=function intromessage(){var b="",c="Congratulations! You have reached level "+LEVEL_NUMBER+".<br>The password for this level is '"+LEVEL_PASSWORD+"'.";this.field.createinputbox(250,85);b+="<table width=100% height=100% border=0 cellspacing=0 cellpadding=10>";b+="<tr>";b+="<td align=justify valign=middle colspan=2>";b+=c;b+="</td>";b+="</tr>";b+="<tr>";b+="<td align=center>";b+="<input type=button value='  OK  ' onclick=\"PlaceHolder.removeChild(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode);\" onfocus=\"this.style.border='1px solid #DDDDDD';this.style.color='#EEEEEE';\" onblur=\"this.style.border='1px solid #888888';this.style.color='#888888';\" onmouseover=\"this.style.border='1px solid #DDDDDD';this.style.color='#EEEEEE';\" onmouseout=\"this.style.border='1px solid #888888';this.style.color='#888888';\">";b+=
"</td>";b+="</tr>";b+="</table>";this.field.inputboxinnerframe.innerHTML=b};a.cowblink=function cowblink(){if(this.cowblinktimer){this.cowblinktimer--;if(!this.cowblinktimer){this.cowpicture[0].style.display="none";this.cowpicture[1].style.display="inline"}}else if(!random(80)){this.cowblinktimer=random(7)+7;this.cowpicture[1].style.display="none";this.cowpicture[0].style.display="inline"}};
