//**********************************************************

function initialize()
{
	is 		 = new BrowserCheck()
	paddle1 	 = new Paddle("paddle1Div")
	paddle2	 = new Paddle("paddle2Div")
	paddle1.name = "paddle1"
	paddle2.name = "paddle2"
	Head		 = new Thing("HeadDiv")
	Head.name	 = "Head"
	mug		 = new Thing("MugDiv")
	outline	 = new Thing("outlineDiv")

	
	headPicked = false
	// Preload Blood Alcohol Meter Pictures
	preload('BA0','ba0.gif')
	preload('BA1','ba1.gif') 
	preload('BA2','ba2.gif') 
	preload('BA3','ba3.gif') 
	preload('BA4','ba4.gif') 
	preload('BA5','ba5.gif')
	preload('BA6','ba6.gif')
	
	document.onkeydown = keyDown
	document.onkeyup   = keyUp
	if (is.ns) document.captureEvents(Event.KEYDOWN | Event.KEYUP)

}

//*********************************************************** 

function Thing(id)
{
	if (is.ns)
	{
		this.css 		= document.layers[id]
		this.xpos	   	= this.css.left
		this.ypos	   	= this.css.top
	}
	if (is.ie)
	{
		this.css		= document.all[id].style
		this.xpos		= this.css.pixelLeft
		this.ypos		= this.css.pixelTop
	}
	
	this.name		= "noname"
	this.top		= this.ypos
	this.direction 	= 0
	this.BAL		= 0
	this.angle		= 0 
	this.startx		= this.xpos
	this.starty 	= this.ypos
	this.active		= false
	this.reposition	= repositionObject
	this.moveTo		= moveObject
	this.bounceAround = bounceAround
			
}

function Paddle(id)
{
	if (is.ns)
	{
		this.css 		= document.layers[id]
		this.xpos	   	= this.css.left
		this.ypos	   	= this.css.top
	}
	if (is.ie)
	{
		this.css		= document.all[id].style
		this.xpos		= this.css.pixelLeft
		this.ypos		= this.css.pixelTop
	}
	this.name		= "noname"
	this.direction 	= 0
	this.top		= this.ypos
	this.startx		= this.xpos
	this.starty		= this.ypos
	this.active		= false
	this.plane		= false
	this.reposition	= repositionObject
	this.moveTo		= moveObject
	this.slide		= slidePaddle
}	



//********************************************************************

function BrowserCheck()
{ 
	var b = navigator.appName 
	if (b=="Netscape") this.b = "ns" 
	else if (b=="Microsoft Internet Explorer") this.b = "ie" 
	else this.b = b 
	this.v   = parseInt(navigator.appVersion) 
	this.ns  = (this.b=="ns" && this.v>=4) 
	this.ns4 = (this.b=="ns" && this.v==4) 
	this.ns5 = (this.b=="ns" && this.v==5) 
	this.ie  = (this.b=="ie" && this.v>=4) 
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0) 
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0)
	if (this.ie5) this.v = 5 
	this.min = (this.ns||this.ie) 
}

//*************************************************************

// The following function will place an object in location x,y

function moveObject(x,y)
{
	this.xpos =  x
	this.ypos =  y
	this.css.left = this.xpos
	this.css.top  = this.ypos
}

//**************************************************************

// The following function will reposition an object by a distance of 
// deltaX&Y 

function repositionObject( deltaX, deltaY)
{
	
	this.xpos += deltaX
	this.ypos += deltaY
	this.css.left = this.xpos
	this.css.top  = this.ypos
} 

//****************************************************************

// The following will handle the paddles' sliding up and down, if 
// step is negative the paddle will move up & vice versa

function slidePaddle(step)
{
	tableTop    	= 5
	tableBottom 	= 405
   	tableLeft   	= 5
	tableRight  	= 605

	if (this.ypos < tableTop+10) 
	{						// Dont let the paddles 
		this.active = false		// leave the table.
		this.reposition(0, 5)
	}
	if (this.ypos > tableBottom-20) 
	{
		this.active = false
		this.reposition( 0, -5)
	}
	if (this.active)
	{
		this.reposition(0, step)
		setTimeout( this.name+".slide("+step+")",27)
	}
}

//******************************************************************

function pickHead()
{	
	if (is.ns) 
	{
		document.splashDiv.visibility   = "hide"
		document.pickheadDiv.visibility = "show"
	}
	if (is.ie) 
	{
		splashDiv.style.visibility   = "hidden"
		pickheadDiv.style.visibility = "visible"
	}
}


//******************************************************************

// function to slow things down at start

function pauseStart(headPicked)
{ 
	if (headPicked)
	{
	
	if (is.ns) 
	{
		document.pickheadDiv.visibility	= "hide"
		document.winnerDiv.visibility 	= "hide"
		Head.css.visibility			= "show"
		document.meterDiv.visibility    	= "show"
		document.controlsDiv.visibility 	= "show"
		outline.css.visibility 	 	  	= "hide"

	}
	if (is.ie) 
	{
		pickheadDiv.style.visibility  	= "hidden"
		winnerDiv.style.visibility    	= "hidden"
		Head.css.visibility	     		= "visible"
		meterDiv.style.visibility     	= "visible"
		controlsDiv.style.visibility  	= "visible"
		outline.css.visibility 			= "hidden"
	}
	Head.moveTo (Head.startx, Head.starty)
	paddle1.moveTo (paddle1.startx, paddle1.starty)	//Turn on paddles
	paddle2.moveTo (paddle2.startx, paddle2.starty)
	showObject (Head)						//Turn on head
	changeImage('meterDiv','meter','BA0')		//Turn on meter
	changeImage('HeadDiv','headImg', 'HEAD')

	Head.BAL	= 0	

	startTimer  = setTimeout(startHead,3000)		//Delay start so everyone can get set
	}
	else window.alert("You must pick a head")
}


//*****************************************************************
// Let's get things rolling

function startHead()
	{
	if (is.ns) document.controlsDiv.visibility = "hide"
	if (is.ie) controlsDiv.style.visibility    = "hidden"
	
	Head.active   		= true	
	mug.active 	  		= false

	paddle1.direction 	= 0
	paddle2.direction 	= 0

	startx 			= 4
	starty 			= getRandom(-3,3)

	bounceCount   		= 0
	beersDrank			= 0
	
	changeImage('HeadDiv','headImg', 'HEADR')
	Head.bounceAround(startx, starty)

	}


//**************************************************************************

//bounceAround works with a simple angle of incidence = angle of reflection algorithim

function bounceAround(xdirection, ydirection)
{
		if (this.active)
	 
		{
			tableTop    	= 5
			tableBottom 	= 405
   			tableLeft   	= 5
			tableRight  	= 605

			hitLeft 	= mug.xpos - 40
			hitRight 	= mug.xpos + 40
			hitTop   	= mug.ypos - 40
			hitBottom 	= mug.ypos + 50
			if ((this.xpos > hitLeft) && (this.xpos < hitRight) && (this.ypos > hitTop) && (this.ypos < hitBottom))
			{
				this.BAL++
				drinkBeer()
			}

			if (this.xpos < tableRight-45 && this.xpos > tableLeft){
					
				inplane 		= false
				paddle1.plane 	= false
				paddle2.plane 	= false



				if (this.xpos < paddle1.startx+5 && this.xpos > paddle1.startx-5 )  //Head's in paddle1 plane
				{ 	
					inplane 		= true
					headBottom 		= Head.ypos - 50
					paddletop    	= paddle1.ypos
					paddlebottom 	= paddle1.ypos - 50
					paddle1.plane 	= true
				}
				if (this.xpos + 40 < paddle2.startx+5 && this.xpos + 40 > paddle2.startx-5)  //Head's in paddle2 plane
				{    	
					inplane 		= true
					headBottom 		= Head.ypos - 50
					paddletop    	= paddle2.ypos
					paddlebottom 	= paddle2.ypos - 50
					paddle2.plane 	= true
				}
				if (inplane)
				{
					if (this.ypos >= paddlebottom && headBottom <= paddletop) //in plane and lines up 
					{
						if (bounceCount == 3) {getBeer()}
						bounceCount++
						if ( ((this.ypos < tableTop)&&(ydirection < 0)) || ((this.ypos > tableBottom) && (ydirection > 0)) ){ydirection *=  -1}
						if (paddle1.plane)
						{
							changeImage('HeadDiv','headImg', 'HEADR')
							ydirection += paddle1.direction
						}
						if (paddle2.plane)
						{
							changeImage('HeadDiv','headImg', 'HEADL')
							ydirection += paddle2.direction
						}

						xdirection *= -1
					
						this.reposition(xdirection, ydirection)
						setTimeout("Head.bounceAround("+xdirection+","+ ydirection+")",30) 
										}
					else	//in plane but doesn't line up
					{
							if ( ((this.ypos < tableTop)&&(ydirection < 0)) || ((this.ypos > tableBottom) && (ydirection > 0)) )							{
								ydirection *=  -1
								this.reposition(xdirection, ydirection)
								setTimeout("Head.bounceAround("+xdirection+","+ ydirection+")",30) 
							}
							else
							{
								this.reposition(xdirection, ydirection)
								setTimeout("Head.bounceAround("+xdirection+","+ ydirection+")",30) 							
							}
					}
				}	
				else		//not in plane
				{
					if ( ((this.ypos < tableTop)&&(ydirection < 0)) || ((this.ypos > tableBottom) && (ydirection > 0)) )					
					{
						ydirection *=  -1
						this.reposition( xdirection, ydirection)
						setTimeout("Head.bounceAround("+xdirection+","+ ydirection+")",30) 
					}
					else
					{
						if(this.BAL > 0) {this.angle = this.angle + .8}
						
							if (this.BAL == 1 || this.BAL == 2)
							{
								ydirection = ydirection - (this.BAL/20)*Math.sin(this.angle*Math.PI/180)
								if (ydirection > 9) ydirection = 9
							}
							if (this.BAL >= 3)
							{
								ydirection = ydirection - (3/20)*Math.sin(this.angle*Math.PI/180)
								xdirection = xdirection - ((this.BAL-2)/20)*Math.sin(this.angle*Math.PI/180)
								if (ydirection > 9) ydirection = 9
								if (xdirection > 8) xdirection = 8	
							}
						this.reposition( xdirection, ydirection)
						setTimeout("Head.bounceAround("+xdirection+","+ ydirection+")",30) 
					}

				}
			
		}
		else	
		{
			this.active = false
			this.BAL    = 0
			mug.Active  = false
			mug.moveTo(650, 10)
			changeImage('HeadDiv','headImg', 'HEAD')
			setTimeout("pauseStart("+headPicked+")",1500)
		}
	}
}



//**************************************************************************
// The following two functions will handle the key up and key down events required to
// move the paddles.

function keyDown(e) {
	if (is.ns) {var nKey=e.which; var ieKey=0}
	if (is.ie) {var ieKey=event.keyCode; var nKey=0}
//
// Now define routines for pressing control keys (different for netscape and ie)
//
// if "q" key is pressed, move paddle1 up
	if ((nKey==113 || ieKey==81) && !paddle1.active) {
		paddle1.active = true
		paddle1.direction = -2
		paddle1.slide(-5)
        }
// if "z" key is pressed move paddle1 down
      if ((nKey==122 || ieKey==90) && !paddle1.active) {   
      	paddle1.active = true
		paddle1.direction = 2
            paddle1.slide(5)
      }
// if "p" is pressed, move paddle2 up
	if ((nKey==112 || ieKey==80) && !paddle2.active) {
		paddle2.active = true
		paddle2.direction = -2	
		paddle2.slide(-5)
        }
// if "." is pressed, move paddle2 down
	if ((nKey==46 || ieKey==190) && !paddle2.active) {
		paddle2.active = true
		paddle2.direction = 2
		paddle2.slide(5)
        }

}

//
// Ditto for releasing control keys
//
function keyUp(e) {
                if (is.ns) {var nKey=e.which; var ieKey=0}
                if (is.ie) {var ieKey=event.keyCode; var nKey=0}
// "z" key
                if (nKey==122 || ieKey==90) {

                        paddle1.active = false
				paddle1.direction = 0   
                }
// "q" key
                if (nKey==113 || ieKey==81) {
                        paddle1.active = false
				paddle1.direction = 0   
                }
// "p" key
                if (nKey==112 || ieKey==80) {
                        paddle2.active = false
				paddle2.direction = 0   
                }
// "." key
                if (nKey==46 || ieKey==190) {
                        paddle2.active = false
				paddle2.direction = 0   
                }
}


//************************************************************************************

function preload(imgObj,imgSrc) 
{		// Preloads images for swapouts
           	if (document.images) 
		{
      	      eval(imgObj+' = new Image()')
	            eval(imgObj+'.src = "'+imgSrc+'"')
           	}
}


function changeImage(layer,imgName,imgObj) 
{
	if (document.layers && layer!=null) eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src');
      else document.images[imgName].src = eval(imgObj+".src");
}

//**********************************************************************************
// Show/hide functions for pointer objects

function showObject(obj) 
{
	if (is.ns) obj.visibility = "show"
	else if (is.ie) obj.visibility = "visible"
}

function hideObject(obj) 
{
	if (is.ns) obj.visibility = "hide"
	else if (is.ie) obj.visibility = "hidden"
}

//*********************************************************************************
// Window Controllers

function reloadGame()
{

	if (is.ns) 
	{
		document.pickheadDiv.visibility	= "show"
		document.winnerDiv.visibility 	= "hide"
		Head.css.visibility			= "hide"
		document.meterDiv.visibility    	= "show"
		document.controlsDiv.visibility 	= "hide"
		outline.css.visibility 	 	  	= "hide"

	}
	if (is.ie) 
	{
		pickheadDiv.style.visibility  	= "visible"
		winnerDiv.style.visibility    	= "hidden"
		Head.css.visibility	     		= "hidden"
		meterDiv.style.visibility     	= "visible"
		controlsDiv.style.visibility  	= "hidden"
		outline.css.visibility 			= "hidden"
	}
	
	Head.active = false
	Head.moveTo (Head.startx, Head.starty)
	paddle1.moveTo (paddle1.startx, paddle1.starty)	//Turn on paddles
	paddle2.moveTo (paddle2.startx, paddle2.starty)

	pickHead()
}


//*********************************************************************************
function quitGame()
{
	window.close()	
}

function stopObject(obj)
{
	clearTimeout(startTimer)
	obj.active = false
}

//------------------------------------------------------------------
// This function will show or hide the beer mug in a random place 
// on the board

function getBeer()
{
	if (!mug.active){
		mug.active = true
		a = getRandom(paddle1.startx+30,paddle2.startx-65)
		b = getRandom(tableTop+15,tableBottom-65)
		mug.moveTo(a,b)
		}
	else {
		mug.active = false
		mug.moveTo(650, 10)
		}
	bounceCount = 0
	}

//------------------------------------------------------------------
// This function is called when the head intersects the mug

function drinkBeer()
{
	mug.Active = false
	mug.moveTo(650, 10)
	bounceCount = 0
	meterObj='BA'+ Head.BAL
	changeImage('meterDiv','meter',meterObj)
	
	if (Head.BAL == 6)			//WINNER
	{ 			
		stopObject(Head)
		if (is.ns) { document.winnerDiv.visibility = "show"}
		if (is.ie) { winnerDiv.style.visibility = "visible"}
	}
}


//------------------------------------------------------------------
//Returns a random number x such that  a <= x <= b

function getRandom(a,b) 		
{
   	rand = Math.random()
	numb = (b-a)*rand + a
	return numb
}


//************************************************************************************

//	The pickBandmember functions simply preload and switch in the correct head pictures

function pickTate() 
{
	outline.moveTo(75,250)
	if (is.ns) outline.css.visibility = "show"
	if (is.ie) outline.css.visibility = "visible"
	headPicked = true
	preload('HEAD' ,'tatepong1.gif')
	preload('HEADL','tatepong2.gif')
	preload('HEADR','tatepong3.gif')
	
	changeImage('HeadDiv','headImg', 'HEAD')
}

function pickDave()
{	
	outline.moveTo(230,250)
	if (is.ns) outline.css.visibility 	= "show"
	if (is.ie) outline.css.visibility 	= "visible"
	headPicked = true
	preload('HEAD' ,'davepong2.gif')
	preload('HEADL','davepong1.gif')
	preload('HEADR','davepong3.gif')
	
	changeImage('HeadDiv','headImg', 'HEAD')
}

function pickGeorge()
{
	outline.moveTo(155,250)
	if (is.ns) outline.css.visibility 	= "show"
	if (is.ie) outline.css.visibility 	= "visible"
	headPicked = true
	preload('HEAD' ,'georgepong1.gif')
	preload('HEADL','georgepong2.gif')
	preload('HEADR','georgepong3.gif')
	
	changeImage('HeadDiv','headImg', 'HEAD')

}

function pickRobert()
{
	outline.moveTo(308,250)
	if (is.ns) outline.css.visibility 	= "show"
	if (is.ie) outline.css.visibility 	= "visible"
	headPicked = true
	preload('HEAD' ,'chafpong1.gif')
	preload('HEADL','chafpong2.gif')
	preload('HEADR','chafpong3.gif')
	
	changeImage('HeadDiv','headImg', 'HEAD')

}

function pickBryan()
{
	outline.moveTo(385,250)
	if (is.ns) outline.css.visibility 	= "show"
	if (is.ie) outline.css.visibility 	= "visible"
	headPicked = true
	preload('HEAD' ,'bryanpong2.gif')
	preload('HEADL','bryanpong1.gif')
	preload('HEADR','bryanpong3.gif')
	
	changeImage('HeadDiv','headImg', 'HEAD')

}
	
function pickJeff()
{
	outline.moveTo(458,250)
	if (is.ns) outline.css.visibility 	= "show"
	if (is.ie) outline.css.visibility 	= "visible"
	headPicked = true
	preload('HEAD' ,'jeffpong1.gif')
	preload('HEADL','jeffpong2.gif')
	preload('HEADR','jeffpong2.gif')
	
	changeImage('HeadDiv','headImg', 'HEAD')

}
