<!--

var self;
function Squash()
{
    self = this;this.tmID=null;this.gameOver=true;
    this.batArr = [[], [7,9,10,11,12], [6,7,8,9,10,11,12], [6,8,10,11,12], [5,7,9,11,12], [4,6,8,10,11], [2,3,5,7,9,11,12], [1,2,10,11,4,6,8], [5,7,9,2,3], [4,6,8,1,2], [1,2,3,5,7], [1,2,3,4,5,6,7], [1,2,3,4,6]];
    this.batPos = [[0,0],[60,40],[140,40],[220,40],[40,60],[240,60],[40,140],[240,140],[40,220],[240,220],[60,240],[140,240],[220,240]];
    this.sBall = null;this.usrBat = 0;this.deltaX=1;this.deltaY=1;
    this.startTm = 0;this.delay=150;this.elArr = new Array(13);
    this.usrScr=0;this.cmpScr=0;this.END = 21;
    		
	this.initGame = function()
	{
	    var str = '';this.gameOver=true
	    str += this.getEndRow(1);
	    str += this.getMidRows(4);
	    str += this.getEndRow(10);
		var o = document.getElementById("squashbrd"); if (o != null) {o.innerHTML = str;}
		
		this.sBall = document.createElement("div");
		this.sBall.className = "sball";
		this.sBall.style.visibility = "visible";
		o.appendChild(this.sBall);
		
		var x = this.getRandom(1,12);
		this.sBall.style.left = this.batPos[x][0]+"px";
		this.sBall.style.top = this.batPos[x][1]+"px";	
		
		for (var i = 1; i < 13; i++) {var oo = document.getElementById("bat_"+i); if (oo != null) {this.elArr[i] = oo;}}
		var f = document.getElementById("m_1");
		if (f != null) {f.innerHTML = '<div class="bat_s btntxt" onClick="self.startGame('+x+');">Start</div>';}
	}
	
	this.startGame = function(nx) {this.usrBat=0;this.usrScr=0;this.cmpScr=0;this.gameOver=false;var x = document.getElementById("m_1");if (x != null) {x.innerHTML = ''};this.bounceBall(nx);}
	
	this.getEndRow = function(sIdx)
	{
	    var str = '<div class="sqr_c"></div>';
	    for (var i = 0; i < 3; i++) {str += '<div class="bat_h_0" id="bat_'+sIdx+'" onClick="self.batTouch('+sIdx+');"></div>';sIdx++;}
	    str += '<div class="sqr_c"></div>';
	    return str;
	}
	
	this.getMidRows = function(sIdx)
	{
	    var str = '';
	    for (var i = 0; i < 3; i++)
	    {
	        str += '<div class="bat_v_0" id="bat_'+sIdx+'" onClick="self.batTouch('+sIdx+');"></div>';
	        str += '<div class="sqr_m" id="m_'+i+'"></div>';
	        sIdx++;
	        str += '<div class="bat_v_0" id="bat_'+sIdx+'" onClick="self.batTouch('+sIdx+');"></div>';
	        sIdx++;
	    }
	    return str;
	}

	this.showScoreBoard = function()
	{
	    var str = '<div class="usrscr">Miss:' + this.cmpScr + '</div>';
	    str += '<div class="cmpscr">Hit:' + this.usrScr + '</div>';
	    
	    var o = document.getElementById("scrbrd");
	    if (o != null) {o.innerHTML = str;}
	    if (this.usrScr + this.cmpScr == this.END) 
	    {
	    	this.gameOver = true;
			var str = '<div class="bat_s btntxt" onClick="self.submitfrm();">Score</div>';
			var f = document.getElementById("m_1"); if (f != null) {f.innerHTML = str;}        
	    }
	}
	
	this.submitfrm = function()
	{
		var f = document.getElementById("squashbrd");
		if (f != null) 
		{
			var score = Math.floor(100*this.usrScr/this.END);
			f.innerHTML = "";
			var str = "Congratulations!!<br>Your score is " + score + "%";
			var o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="helppage" value="squashhelp.html" />';
			var o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="gamepage" value="squash.html" />';
			o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="msg" value="' + str + '" />';
		}

		document.squash_frm.submit();
	}
		
		
	this.hOrV = function(n)
	{
	    var hOrV = "v";
	    if (n >= 1 && n <= 3) {hOrV = "h";}
	    else if (n >= 10 && n <= 12) {hOrV = "h";}
	    return hOrV;
	}
	
	this.changeClass = function(n,clr)
	{
	    if (this.elArr[n] != null) {this.elArr[n].className = "bat_"+this.hOrV(n)+"_"+clr;}
	}
	
	this.bounceBall = function(x)
	{
		this.showScoreBoard();
		if (this.usrScr + this.cmpScr == this.END) {return;}
	    
	    this.changeClass(x, 0);
	    var n = this.getRandomBat(x);var divnum = 10;
	    var xOff = Math.abs(this.batPos[x][0] - this.batPos[n][0]);
	    var yOff = Math.abs(this.batPos[x][1] - this.batPos[n][1]);
	    if (xOff > yOff) {divnum = xOff/10;} else {divnum = yOff/10;}
	    
	    this.deltaX = xOff/divnum; if (this.batPos[n][0] < this.batPos[x][0]) {this.deltaX *= -1;}
	    this.deltaY = yOff/divnum; if (this.batPos[n][1] < this.batPos[x][1]) {this.deltaY *= -1;}
	    this.startTm = (new Date()).getTime();
	    this.bounceSlow(n);
	}
	
	this.checkBounds = function(newpos, reqpos, delta)
	{
	    var calpos = newpos;
	    if (delta < 0)
	    {
	        if (newpos < reqpos) {calpos = reqpos;}
	    }
	    else if (delta > 0)
	    {
	        if (newpos > reqpos) {calpos = reqpos;}
	    }
	    
	    return calpos;
	}
	
	this.hitOrMiss = function(n)
	{
		var clr=2; 
		if (n == self.usrBat) {clr=1;self.usrScr++;} else {self.cmpScr++;}
		self.changeClass(n,clr);
		setTimeout("self.bounceBall("+n+")",200);	
	}
	
	this.bounceSlow = function(n)
	{
	    var tm = (new Date()).getTime() - this.startTm;
	    tm = tm/this.delay; if (tm < 1) tm = 1;
		var x = parseInt(self.sBall.style.left) + self.deltaX*tm; x = self.checkBounds(x,self.batPos[n][0],self.deltaX); //if (x > self.batPos[n][0]) {x = self.batPos[n][0];}
		var y = parseInt(self.sBall.style.top)  + self.deltaY*tm; y = self.checkBounds(y,self.batPos[n][1],self.deltaY); //if (y > self.batPos[n][1]) {y = self.batPos[n][1];}
		self.sBall.style.left = x + 'px';self.sBall.style.top = y + 'px';
		if (x != self.batPos[n][0] || y != self.batPos[n][1]) {setTimeout("self.bounceSlow(" + n + ")", this.delay);}
		else {setTimeout("self.hitOrMiss("+n+");",100);}
	}
	
	this.batTouch = function(n)
	{
	    if (this.gameOver) {return;}
	    var oldid=this.usrBat;this.usrBat = n;
	    this.changeClass(oldid,0);
	    this.changeClass(n,1);
	}
	
	
	this.getRandomBat = function(n)
	{
	    var x = this.getRandom(0,this.batArr[n].length-1);
	    return this.batArr[n][x];
	}

	this.getRandom = function(min,max)
	{
		return min + Math.floor(Math.random()*(max-min+1))
	}
}


-->


