/* Written by Joshua A. S. Allen, SlaphappyGeeks.com, 4/4/2010 */

function WhackARat(rats,hitsCounter,missesCounter,settings)
{// Set image sources.
var imgSrcs={
	start:'toilet_closed.png',
	open:'toilet_open.png',
	open2:'toilet_open.png',
	open3:'toilet_open.png',
	open4:'toilet_open.png',
	open5:'toilet_open.png',
	popup1:'toilet_rat_popup1.png',
	popup2:'toilet_rat_popup2.png',
	popup3:'toilet_rat_popup3.png',
	popup4:'toilet_rat_popup4.png',
	popup5:'toilet_rat_popup4.png',
	popup6:'toilet_rat_popup4.png',
	popup7:'toilet_rat_popup3.png',
	popup8:'toilet_rat_popup2.png',
	popup9:'toilet_rat_popup1.png',
	hit1:'toilet_rat_hit1.png',
	hit2:'toilet_rat_hit2.png',
	hit3:'toilet_rat_hit3.png',
	hit4:'toilet_rat_hit4.png',
	hit5:'toilet_rat_hit5.png',
	hit6:'toilet_rat_hit6.png',
	close:'toilet_closed.png'},
hits=0,		// Number of hits.
misses=0,	// Number of misses.
imgState=new Array(); // Image states.
timeoutID=null;		// Timeout.

// Settings.
settings=settings?settings:{};
settings.speed=settings.speed?settings.speed:1000;
settings.path=settings.path?settings.path:'';

// Mousedown event handler.
var mouseDown=function(event)
{var rat=rats.index(event.target);
if(timeoutID==null){
	return;}
if(rat>=0&&$.inArray(imgState[rat],['popup1','popup2','popup3','popup4','popup5','popup6'])!=-1){
	imgState[rat]='hit1';}}

// 
var nextState=function()
{// window.setTimeout() callback.
// Updates image src to next image.
var nextStates={
	start:'open',
	open:function(){return Math.random()<=0.5?'open2':'popup1'},
	open2:'open3',
	open3:'open4',
	open4:'open5',
	open5:'close',
	popup1:'popup2',
	popup2:'popup3',
	popup3:'popup4',
	popup4:'popup5',
	popup5:'popup6',
	popup6:'popup7',
	popup7:'popup8',
	popup8:'popup9',
	popup9:'close',
	hit1:'hit2',
	hit2:'hit3',
	hit3:'hit4',
	hit4:'hit5',
	hit5:'hit6',
	hit6:'close',
	close:'start'};
rats.each(
	function()
	{var img=this,
	index=rats.index(this),	// Index of this in images.
	state=imgState[index];	// State of image.
	// Is the next state generated by a function?
	if(typeof nextStates[state]=='function'){
		// Call function.
		imgState[index]=nextStates[state]();}
	else{
		if(imgState[index]=='popup7'){
			misses++;}
		if(imgState[index]=='hit1'){
			hits++;}
		imgState[index]=nextStates[state];}	
	img.src=settings.path+imgSrcs[imgState[index]];});
hitsCounter.html(hits);
missesCounter.html(misses);
timeoutID=window.setTimeout(nextState,settings.speed);}

this.unbind=function()
{// Unbind image event listeners.
rats.unbind('onmousedown',mouseDown);
return this;}

this.start=function()
{// Starts changing images.
// Stop if whacking.
this.stop();
// Set new timeout.
timeoutID=window.setTimeout(nextState,settings.speed);
return this;}

this.stop=function()
{// Stops timeout.
if(timeoutID!=null){
	window.clearTimeout(timeoutID);
	timeoutID=null;}
return this;}

this.setSpeed=function(s)
{// Changes speed of change.
settings.speed=s;
return this;}

this.resetImages=function()
{// Resets image srcs.
rats.each(
	function()
	{this.src=settings.path+imgSrcs.close;});
return this;}

// Bind events.
rats.bind(
	'mousedown',mouseDown).
each(
	function()
	{imgState[rats.index(this)]='start';});

// Set hits and misses.
hitsCounter.html(hits);
missesCounter.html(misses);}

