var ScatterGame=new Class({tenths:0,zindex:9999,initialize:function(d){this.quiz_ids=d.quiz_ids;
this.embedding=window.location.toString().indexOf("embed")!=-1;this.recordTime=Cookie.get("scatter-"+this.quiz_ids);
this.nowShowing=Math.min(8,$("scatterBox").childNodes.length/2);var b=$("get-started");
if(b){var c=(window.getHeight()-b.offsetHeight)/2;new Fx.Styles(b).start({opacity:[0,1],top:[c+20,c]})
}else{this.startGame()}var a=$("words-cut");if(a.offsetHeight>200){a.setStyle("height","200px")
}if(this.embedding){$("roundedwrap").setStyle("height",$("footer").getTop()+"px")
}},instructionStart:function(){var a=$("get-started"),b=new Fx.Styles(a,{onComplete:function(){a.remove();
this.startGame()}.bind(this)});b.start({opacity:0,top:a.getTop()+20});return false
},getTermIndexes:function(){var b=[],d=[],c,a;for(c=0,a=$("scatterBox").childNodes.length/2;
c<a;c++){b.push(c)}b=shuffle(b).slice(0,this.nowShowing);for(c=0,a=b.length;c<a;c++){d.push(b[c]*2,b[c]*2+1)
}return d},generatePositions:function(){var l=this.getTermIndexes();var k=$("scatterBox"),r=$("footer").getTop(),d=k.getTop(),p=250,c=k.offsetLeft,e=k.offsetWidth+c,a=[],j=k.childNodes,b,f,h,o,i,g,n;
for(b=0,f=l.length;b<f;b++){n=l[b];h=$(j[n]);if(window.ie&&h.offsetWidth>p){h.setStyle("width",p+"px")
}o={x:[c,e-h.offsetWidth],y:[d,r-h.offsetHeight]};if(n%2===0){i=o}g=this.findOpenSpace(o,a,h);
a.push({i:n,lims:o,x1:g.x,x2:g.x+h.offsetWidth,y1:g.y,y2:g.y+h.offsetHeight})}return a
},startGame:function(){this.secondTimer=this.incTimer.periodical(100,this);var g=this.generatePositions(),f=250,e=$("scatterBox").setOpacity(0.9999).childNodes,c=0,d,h,b,a;
for(h=0,b=g.length;h<b;h++){a=g[h];d=c;c=$(e[a.i]);if(window.ie&&c.offsetWidth>f){c.setStyle("width",f+"px")
}c.setStyles({left:a.x1+"px",top:a.y1+"px",visibility:"visible"});if(a.i%2==1){[c,d].each(function(i){i.addEvent("mouseout",this.mOut);
i.addEvent("mouseover",this.mOver);i.addEvent("drop",this.mLeave.bind(this))}.bind(this));
c.makeDraggable({onStart:this.mStart.bind(this),onComplete:this.mEnd,droppables:d,limit:a.lims});
d.makeDraggable({onStart:this.mStart.bind(this),onComplete:this.mEnd,droppables:c,limit:g[h-1].lims})
}}},mStart:function(a){a.setStyle("z-index",--this.zindex);a.removeClass("over");
a.addClass("dragging")},mEnd:function(a){a.removeClass("dragging")},mOver:function(){this.addClass("over")
},mOut:function(){this.removeClass("over")},mLeave:function(b,a){[b,a.droppables[0]].each(function(c){c.removeEvents();
c.effect("opacity",{duration:500,onComplete:c.remove.bind(c)}).start(1,0);c.className="correct"
});this.nowShowing--;if(this.nowShowing===0){this.endGame()}},endGame:function(){$clear(this.secondTimer);
var d;if(!this.recordTime||this.tenths<this.recordTime){Cookie.set("scatter-"+this.quiz_ids,this.tenths,{path:"/"})
}if(!this.recordTime){d="Good start...Now beat that time!"}else{var c=this.recordTime-this.tenths;
if(c<-50){d="Your previous record eclipsed you!"}else{if(c<-10){d="Uhh, you've done better..."
}else{if(c<0){d="Oh so close!"}else{if(c==0){d="You matched your record!"}else{if(c<10){d="Nice, you edged out your last time."
}else{if(c<50){d="Getting better!"}else{d="You eclipsed your previous record. Woohoo!"
}}}}}}}$("congrats-title").appendText(d);$("this-time").appendText(this.humanTime(this.tenths));
$("your-record").appendText(this.humanTime(this.recordTime||this.tenths));if(this.recordTime){$("win-loss").appendText((c<0)?"Missed by":"Improved By");
$("win-loss-diff").appendText(this.humanTime(c))}else{$($("win-loss-diff").parentNode).remove()
}var b=$("game-over"),a=(window.getHeight()-b.offsetHeight)/2,e=$("scatterBox").getTop()+5;
if(a<e){a=e}new Fx.Styles(b,{duration:1700}).start({opacity:[0,1],top:[a-20,a]})},toggleList:function(a,c){var d=$(c),b=d.getTop();
new Fx.Styles(d,{duration:500,onComplete:function(){if(c=="term-list"){this.element.setStyle("top","-999px")
}d=$(a);b=(window.getHeight()-d.offsetHeight)/2;var e=$("scatterBox").getTop()+5;
if(b<e){b=e}new Fx.Styles(d,{duration:500}).start({opacity:[0,1],top:[b-20,b]})}}).start({opacity:0,top:[b,b+20]});
return false},tryAgain:function(b,a){setTimeout(function(){window.location=a.href
},400);this.toggleList("fadeGhost",b);return false},convertTime:function(d){d=Math.abs(d);
var a=Math.floor(d/600),c=Math.floor((d-600*a)/10),b=d-600*a-10*c;return{minutes:a,seconds:(c<10)?"0"+c:""+c,tenths:b}
},humanTime:function(b){var c="",a=this.convertTime(b);if(a.seconds.substring(0,1)=="0"){a.seconds=a.seconds.substring(1)
}if(a.minutes>0){c+=a.minutes+((a.minutes==1)?" minute, ":" minutes, ")}c+=a.seconds+"."+a.tenths+" seconds";
return c},serialTime:function(b){var a=this.convertTime(b);return a.minutes+":"+a.seconds+"."+a.tenths
},incTimer:function(){this.tenths++;if(this.tenths==this.recordTime){new Fx.Style("timeshow","color",{duration:1500}).start((this.embedding)?"#b00":"#FD3636")
}var a=this.convertTime(this.tenths);$("tens").setHTML(a.tenths);$("seconds").setHTML(a.seconds);
$("minutes").setHTML(a.minutes)},findOpenSpace:function(b,p,f){var c,d,g,o,l,j,a,e,k,h;
for(c=0;c<7;c++){o=$random(b.x[0],b.x[1]);l=$random(b.y[0],b.y[1]);j=l+f.offsetHeight;
a=o+f.offsetWidth;e=true;for(g=0,d=p.length;g<d;g++){h=p[g];if(((h.x1<=o&&h.x2>=o)||(h.x1<=a&&h.x2>=a))&&((h.y1<=l&&h.y2>=l)||(h.y1<=j&&h.y2>=j))){e=false;
break}}k={x:o,y:l};if(e){return k}}return k}});