function fs_WordGame(){this.directions=new Array(1,1,1,-1,-1,1,-1,-1,1,0,-1,0,0,1,0,-1);this.first_number=-1;this.second_number=-1;}
fs_WordGame.prototype.sinit=function(){this.cnt=0;this.ar=new Array(this.size*this.size);for(var j=0;j<this.size*this.size;j++)this.ar[j]=null;}
fs_WordGame.prototype.init=function(size,t,words,parent){this.time=t;this.handler=document.createElement("DIV");document.getElementById(parent).appendChild(this.handler);this.size=size;this.words=words;this.sinit();var repeat=0;for(var i=0;i<words.length;i++){if(this.insert_word(words[i])==false){repeat++;if(repeat==20){this.size++;repeat=0;}
this.sinit();i=-1;}
this.cnt=(this.cnt+1)%8;}
for(var k=0;k<this.size*this.size;k++)
if(this.ar[k]==null)
this.ar[k]=String.fromCharCode(65+Math.round(Math.random()*25));var ret="<table border='1' class='horizontal_table ht_odd round shadow' style=';width:"+this.size*30+"px' align='center' valign='middle'>";for(var y=0;y<this.size;y++){ret+="<tr>";for(var x=0;x<this.size;x++){ret+=("<td align='center'><span id='"+(x+'_'+y)+"' onclick='fs_word_game.select(this)' style='width:20px;height:20px;;display:block;cursor:pointer;margin:2px'>"+(this.ar[y*this.size+x].toUpperCase())+"</span></td>");}
ret+="</tr>";}
ret+="<tr><td id='hints' style='border:0px;padding:5px;' colspan='"+this.size+"'>";ret+=this.words.join(', ');ret+="</td></tr></table>";this.handler.innerHTML=ret;this.start_time=new Date().getTime();}
fs_WordGame.prototype.set_square=function(obj){obj.style.backgroundColor='green';this.first_number=obj.id;}
fs_WordGame.prototype.reset_square=function(){document.getElementById(this.first_number).style.backgroundColor="";this.first_number=-1;}
fs_WordGame.prototype.found_word=function(obj){var dirs=new String(this.first_number).split('_');var x1=dirs[0];var y1=dirs[1];dirs=obj.id.split('_');var x2=dirs[0];var y2=dirs[1];var dx=x2-x1;var dy=y2-y1;if(dx!=0&&dy!=0&&Math.abs(dx)!=Math.abs(dy)){this.reset_square();this.set_square(obj);return false;}
var sx=0;var sy=0;if(dx>0)sx=1;else sx=(dx==0?0:-1);if(dy>0)sy=1;else sy=(dy==0?0:-1);var ret="";var m=dx;for(var i=0,x=x1,y=y1;i<=Math.max(Math.abs(dx),Math.abs(dy));i++){ret+=this.ar[parseInt(y*this.size)+parseInt(x)];x=parseInt(sx)+parseInt(x);y=parseInt(sy)+parseInt(y);}
for(var k=0;k<this.words.length;k++)
if(this.words[k].toUpperCase()==ret.toUpperCase()){for(var i=0,x=x1,y=y1;i<=Math.max(Math.abs(dx),Math.abs(dy));i++){var o=document.getElementById(x+'_'+y);o.style.backgroundColor='red';o.onclick=new Function("return false");x=parseInt(sx)+parseInt(x);y=parseInt(sy)+parseInt(y);}
this.words.splice(k,1);document.getElementById('hints').innerHTML="<b>Words left</b>: "+this.words.join(', ');if(this.words.length==0){var t=(new Date().getTime()-this.start_time)/1000;new Ajax.Request('/games/word_end_game?t='+this.time+'&d='+t,{asynchronous:true,evalScripts:true,onComplete:function(request){eval(request.responseText);}});if(confirm("Congratulations you've finished in "+t+" seconds; do you want to play again?"))
new Ajax.Updater('display_words','/games/list_words',{asynchronous:true,evalScripts:true,onComplete:function(request){}});}
return true;}
this.reset_square();this.set_square(obj);return false;}
fs_WordGame.prototype.select=function(obj){if(obj.id==this.first_number){obj.style.backgroundColor="";this.first_number=-1;}else{if(this.first_number!=-1){if(this.found_word(obj))
this.first_number=-1;}
else this.set_square(obj);}}
fs_WordGame.prototype.can_insert=function(x,y,word){var x1=this.directions[this.cnt*2];var y1=this.directions[this.cnt*2+1];var l=word.length;if(x+l*x1>=this.size||x+l*x1<0||y+l*y1>=this.size||y+l*y1<0)
return false;for(var i=0;i<l;i++){if(this.ar[y*this.size+x]!=null)return false;x+=x1;y+=y1;}
return true;}
fs_WordGame.prototype.get_position=function(){return parseInt(Math.random()*this.size);}
fs_WordGame.prototype.insert_word=function(w){var x=this.get_position();var y=this.get_position();var tries=0
while(tries<70&&this.can_insert(x,y,w)==false){tries++;if(tries%10==0)
this.cnt=(this.cnt+1)%8;x=this.get_position();y=this.get_position();}
if(tries==70)
return false;var x1=this.directions[this.cnt*2];var y1=this.directions[this.cnt*2+1];var l=w.length;for(var i=0;i<l;i++){this.ar[y*this.size+x]=w.substr(i,1);x+=x1;y+=y1;}}
var fs_word_game=new fs_WordGame();