var ss = ss || {};
ss.init = {
	stack : [],
	init : function() {
		soundManagerInit();
		for (var i=0, j=ss.init.stack.length; i < j; i++) {
			ss.init.stack[i]();
		};
	}
};
ss.games = {}
ss.games.pong = {
	init : function() {
		$('#pongBox').mousemove(ss.games.pong.mousemove);
		ss.games.pong.start();
	},
	playerScore : 0,
	computerScore : 0,
	maxScore : 10,
	timer : null,
	mousemove : function(evt) {
		var intY = evt.clientY;
		var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
		var dsoctop=document.all? iebody.scrollTop : pageYOffset;
		intY += dsoctop;
		ss.games.pong.movePaddle($('#playerPaddle'),intY);
		ss.games.pong.movePaddle($('#computerPaddle'),intY);
	},
	movePaddle : function(elPaddle,intY) {
		intY -= 100;
		intY = intY-(elPaddle.height()/2)-140;
		if (intY < 0) {intY = 0;}
		intMaxY = $('#pongBox').height() - elPaddle.height()
		if (intY > intMaxY) {intY = intMaxY}
		elPaddle.css('top',intY);		
	},
	start : function() {
		$('div.pongInfo').fadeOut(function(){
			$('#pongMessage').remove();	
		});
		ss.games.pong.ballTrajectory[0] = ss.games.pong.initialSpeed;
		ss.games.pong.playerScore = 0;
		ss.games.pong.computerScore = 0;
		$('#playerScore').text(ss.games.pong.playerScore);
		$('#computerScore').text(ss.games.pong.computerScore);
		booPause = false;
		if ($('#playerPaddle').is(':hidden')) {
			$('#playerPaddle').show('explode',{},1000);
			booPause = true;
		}
		if ($('#computerPaddle').is(':hidden')) {
			$('#computerPaddle').show('explode',{},1000);
			booPause = true;
		}
		if (booPause) {
			setTimeout(function() {ss.games.pong.drawBall(ss.games.pong.goBall)},1000)
		} else {
			ss.games.pong.drawBall(ss.games.pong.goBall);					
		}
	},
	pause : function() {
		timeBasedTaskList.pong = null;
	},
	unpause : function() {
		timeBasedTaskList.pong = function() {ss.games.pong.animateBall();ss.games.pong.collisionDetection();}
	},
	endTurn : function() {
		if (ss.games.pong.playerScore == ss.games.pong.maxScore) {
			$('#winner').text('Player');
			$('#computerPaddle').hide('explode');
		}
		if (ss.games.pong.computerScore == ss.games.pong.maxScore) {
			$('#winner').text('Computer');
			$('#playerPaddle').hide('explode')
		}
		if (ss.games.pong.playerScore == ss.games.pong.maxScore || ss.games.pong.computerScore == ss.games.pong.maxScore) {
			ss.games.pong.end();
		} else {
			$('div.pongInfo').fadeIn(function(){
				$('div.pongInfo').fadeOut(function(){
					ss.games.pong.nextTurn();												
				})
			})
		}
	},
	end : function() {
		$('#pongBall').remove();
		ss.games.pong.ballTrajectory[0] = ss.games.pong.initialSpeed;
		timeBasedTaskList.pong = null;
		$('#playPong .text').text('Play');
		$('#playPong').unbind('click').click(function() {
			$(this).find('.text').text('Pause');
			$(this).unbind('click').click(ss.games.pong.pausePong);
			ss.games.pong.start();
			return false;
		});
		$('div.pongInfo').fadeIn();
		$('#endGame').fadeIn();
	},
	nextTurn : function() {
		timeBasedTaskList.pong = null;
		$('#pongBall').remove();
		if (ss.games.pong.ballTrajectory[0] > 0) {
			ss.games.pong.ballTrajectory[0]++;
		} else {
			ss.games.pong.ballTrajectory[0]--;
		}
		ss.games.pong.ballTrajectory[0] = ss.games.pong.ballTrajectory[0] * -1.0;
		ss.games.pong.drawBall(ss.games.pong.goBall);
	},
	drawBall : function(callBack) {
		$('#pongBox').append('<div id="pongBall" style="display:none;"><img src="img/games/pong/ball.gif" width="14" height="14" alt="Ball"/></div>')
		$('#pongBall').show('explode',{},1000);
		setTimeout(callBack,1000);
	},
	dir : 0,
	initialSpeed : 10.0,
	speedIncrement : 1.0,
	ballTrajectory : [0.0,0.0],
	goBall : function() {
		ss.games.pong.ballTrajectory[1] = 1.0;
		timeBasedTaskList.pong = function() {ss.games.pong.animateBall();ss.games.pong.collisionDetection();}
	},
	animateBall : function() {
		$('#pongBall').css('left',parseInt($('#pongBall').css('left')) + ss.games.pong.ballTrajectory[0]);
		$('#pongBall').css('top',parseInt($('#pongBall').css('top')) + ss.games.pong.ballTrajectory[1]);
	},
	collisionDetection : function() {
		var ball = $('#pongBall');
		if (ball.length == 0) {
			return false;
		}
		var box = $('#pongBox');
		var playerPaddle = $('#playerPaddle');
		var computerPaddle = $('#computerPaddle');
		var oBallCoords = {
			left : parseInt(ball.position().left),
			top : parseInt(ball.position().top),
			right : parseInt(ball.position().left) + ball.width(),
			bottom : parseInt(ball.position().top) + ball.height()
		};
		var oPlayerPaddleCoords = {
			left : parseInt(playerPaddle.position().left),
			top : parseInt(playerPaddle.position().top),
			right : parseInt(playerPaddle.position().left) + playerPaddle.width(),
			bottom : parseInt(playerPaddle.position().top) + playerPaddle.height()
		};
		var oComputerPaddleCoords = {
			left : parseInt(computerPaddle.position().left),
			top : parseInt(computerPaddle.position().top),
			right : parseInt(computerPaddle.position().left) + computerPaddle.width(),
			bottom : parseInt(computerPaddle.position().top) + computerPaddle.height()
		};
		//Paddle collision
		if (
		(oBallCoords.left <= oPlayerPaddleCoords.right &&  oBallCoords.right >= oPlayerPaddleCoords.left) 
		&&
		(oBallCoords.bottom >= oPlayerPaddleCoords.top && oBallCoords.top <= oPlayerPaddleCoords.bottom)
		&&
		(ss.games.pong.ballTrajectory[0] < 0)
		) {
			//hit player's paddle
			ss.games.pong.ballTrajectory[0] = ss.games.pong.ballTrajectory[0] * -1.0;
			//Get position on paddle
			intPaddlePos = 0.0 + oBallCoords.bottom - oPlayerPaddleCoords.top;
			intYTrajectory = -1.0 * ((playerPaddle.height()/2.0)-intPaddlePos + (ball.height()/2.0))
			intYTrajectory = intYTrajectory/15.0;
			ss.games.pong.ballTrajectory[1] = intYTrajectory;
			soundManager.play('pong2');
		}
		if (
		(oBallCoords.left <= oComputerPaddleCoords.right &&  oBallCoords.right >= oComputerPaddleCoords.left) 
		&&
		(oBallCoords.bottom >= oComputerPaddleCoords.top && oBallCoords.top <= oComputerPaddleCoords.bottom)
		&&
		(ss.games.pong.ballTrajectory[0] > 0)
		) {
			//hit computer's paddle
			ss.games.pong.ballTrajectory[0] = ss.games.pong.ballTrajectory[0] * -1.0;
			//Get position on paddle
			intPaddlePos = 0.0 + oBallCoords.bottom - oComputerPaddleCoords.top;
			intYTrajectory = -1.0 * ((computerPaddle.height()/2.0)-intPaddlePos + (ball.height()/2.0))
			intYTrajectory = intYTrajectory/15.0;
			ss.games.pong.ballTrajectory[1] = intYTrajectory;
			soundManager.play('pong2');
		}
		//Border collision
		if (oBallCoords.top <=0) {
			if (oBallCoords.top < 0) {ball.css('top',0)};
			//top border collision
			ss.games.pong.ballTrajectory[1] = ss.games.pong.ballTrajectory[1] * -1.0;
			soundManager.play('pong1');
		}
		if (oBallCoords.bottom >= box.height()) {
			if (oBallCoords.bottom > box.height()) {ball.css('top',box.height()-ball.height())};
			//bottom border collision
			ss.games.pong.ballTrajectory[1] = ss.games.pong.ballTrajectory[1] * -1.0;
			soundManager.play('pong1');
		}
		if (oBallCoords.right <= 0) {
			//player loses!
			ball.remove();
			ss.games.pong.computerScore++;
			$('#computerScore').text(ss.games.pong.computerScore);
			ss.games.pong.endTurn();
		}
		if (oBallCoords.left >= box.width()) {
			ball.remove();
			//computer loses!
			ss.games.pong.playerScore++;
			$('#playerScore').text(ss.games.pong.playerScore);
			ss.games.pong.endTurn();
		}
	},
	preparePong : function() {
		$(this).find('.text').text('Pause');
		$(this).unbind('click').click(ss.games.pong.pausePong);
		ss.init.init();
		return false;
	},
	pausePong : function() {
		$(this).find('.text').text('Unpause');
		ss.games.pong.pause();
		$(this).unbind('click').click(ss.games.pong.unpausePong);
		return false;
	},
	unpausePong : function() {
		$(this).find('.text').text('Pause');
		ss.games.pong.unpause();
		$(this).unbind('click').click(ss.games.pong.pausePong);
		return false;
	}
}
ss.init.stack.push(ss.games.pong.init);