var G = -.15;
var d = 0.98;
var treset = 1;
var t = treset;
var multiplier = 1.2;
if(jQuery.browser.msie)
	multiplier = .8;
var vx, vy;
var damper = 0.25;
var ballInterval;
var ball, ballx, bally, balleft, balltop;
var flipTime = new Date();
var x,y;
var ballInPlay = false;
var bounds;
var ballHalfWidth, ballHalfHeight;

jQuery(window).load(function(){
	//alert(parseFloat(jQuery("#playarea").offset().left) + parseFloat(jQuery("#playarea").css("width"))/2 -   parseFloat(jQuery("#ball").css("width")) / 2);
	ball = jQuery("#ball");
	
	//jQuery("#plunger").frameAnimation();
	
	positionElements();
	jQuery('#plunger').jani({ frameWidth: 120, frameHeight: 245, speed: 40, totalFrames: 7, loop: false });
	
	ball.click(launchBall);
	jQuery("#plunger").click(launchBall);

	jQuery("#button_left_target").mousedown(function() { pressLeft(); return false; } );												   
	jQuery("#button_right_target").mousedown(function() { pressRight(); return false; } );
	
	jQuery("#button_left_target").mouseup(function() { releaseLeft(); return false;  } );
	jQuery("#button_right_target").mouseup(function() { releaseRight(); return false;  } );	
	
	jQuery("#flipper_left").mousedown(function() { pressLeft(); return false; } );												   
	jQuery("#flipper_right").mousedown(function() { pressRight(); return false; } );
	
	jQuery("#flipper_left").mouseup(function() { releaseLeft(); return false;  } );
	jQuery("#flipper_right").mouseup(function() { releaseRight(); return false;  } );	
	
	jQuery(document).keydown(function(e) { if(e.which == 188) { pressLeft(); } else if (e.which == 190 ) { pressRight(); } return true; }  );
	jQuery(document).keyup(function(e) { if(e.which == 188) { releaseLeft(); } else if (e.which == 190 ) { releaseRight(); } else if (e.which == 76) { launchBall(); } return true; } );
	
	jQuery(window).resize(positionElements);
	
	ballInterval = setInterval(moveBall, 30);
	
	if(jQuery.browser.msie) {
		ballInterval = setInterval(moveBall, 30);	
	}
	//launchBall();	
});

function positionElements() {
	bounds = new Object();
	
	bounds.left = parseFloat(jQuery("#playarea").position().left);
	bounds.right = bounds.left + parseFloat(jQuery("#playarea").css("width"));	
	
	ballHalfWidth = parseFloat(ball.css("width")) / 2;
	ballHalfHeight = parseFloat(ball.css("height")) / 2;
	
	resetBall();
	
	jQuery("#instructions").css("left", bounds.left + parseFloat(jQuery("#playarea").css("width")) / 2 - parseFloat(jQuery("#instructions").css("width")) / 2);
	jQuery("#instructions").css("top", 50);
																						 
	
	jQuery("#flipper_left").css("left", bounds.left + 120);
	jQuery("#flipper_right").css("left", bounds.right - 370);
	
	jQuery("#flipper_left").css("top", 20);
	jQuery("#flipper_right").css("top", 20);
	
	jQuery("#bumper_left").css("left", bounds.left + 10);
	jQuery("#bumper_right").css("left", bounds.right - 140);
	
	jQuery("#bumper_left").css("top", 4);
	jQuery("#bumper_right").css("top", 4);		
	
	jQuery("#button_left").css("left", bounds.left - 101);
	jQuery("#button_right").css("left", bounds.right + 63);	
	jQuery("#button_left").css("top", 30);
	jQuery("#button_right").css("top", 30);
	
	jQuery("#button_left_target").css("left", bounds.left - 101);
	jQuery("#button_right_target").css("left", bounds.right + 63);
	jQuery("#button_left_target").css("top", 30);
	jQuery("#button_right_target").css("top", 30);

	jQuery("#plunger").css("top", 137);
	jQuery("#plunger").css("left", bounds.right - 120);	
}

function resetBall() {
	vx = 0;
	vy = 0;
	ballInPlay = false;
	ball.css("left", bounds.right - parseFloat(jQuery("#ball").css("width")) / 2 - 60);
	ball.css("top", 10);
	ball.css("display", "block");
	ballleft = parseFloat(jQuery("#ball").css("left"));
	balltop = parseFloat(jQuery("#ball").css("top"));
	ballx = ballleft + ballHalfWidth - bounds.left;
	bally = balltop + ballHalfHeight;
}

function launchBall() {
	if(!ballInPlay) {
		jQuery('#plunger').jani.play();
		setTimeout(shoot, 250);
	}
}

function shoot() {
	ballInPlay = true;
	t = treset;
	vx = -1 * (Math.random() * 20 + 10);
	vy = -1 * (Math.random() * 15 + 30);
}

function pressLeft() {
	if(jQuery("#flipper_left").css("background-image").indexOf("up") == -1) {
		jQuery("#flipper_left").css("background-image", "url(/wp-content/themes/bigfeature/images/flipper_l_up.png)");
		jQuery("#button_left").css("background-image", "url(/wp-content/themes/bigfeature/images/button_l_press.png)");
		flipTime = new Date();
	}
}
function pressRight() {
	if(jQuery("#flipper_right").css("background-image").indexOf("up") == -1) {
		jQuery("#flipper_right").css("background-image", "url(/wp-content/themes/bigfeature/images/flipper_r_up.png)");
		jQuery("#button_right").css("background-image", "url(/wp-content/themes/bigfeature/images/button_r_press.png)");
		flipTime = new Date();
	}
}

function releaseLeft() {
	jQuery("#flipper_left").css("background-image", "url(/wp-content/themes/bigfeature/images/flipper_l.png)");
	jQuery("#button_left").css("background-image", "url(/wp-content/themes/bigfeature/images/button_l.png)");
}
function releaseRight() {
	jQuery("#flipper_right").css("background-image", "url(/wp-content/themes/bigfeature/images/flipper_r.png)");
	jQuery("#button_right").css("background-image", "url(/wp-content/themes/bigfeature/images/button_r.png)");
}
	

function checkGameOver() {
	if(bally > 200) {
		ball.css("display", "none");
		ballInPlay = false;
		//alert("reset");
		setTimeout(resetBall, 2000);
		return true;		
	}
	else {
		return false;
	}
}

var mouseDown = 0;
document.body.onmousedown = function() { 
    mouseDown = 1;
}
document.body.onmouseup = function() {
    mouseDown = 0;
}

function checkHit(flipperName) {
	var flipper = jQuery(flipperName);
		
	if(flipperName == "#flipper_left") {
		// test for hit and adjust ball mechanics if true hit with flipper determined	
		if(bally > flipper.position().top && ballx < 120 + parseFloat(flipper.css("width"))) {
			t = treset;
			
			if(flipper.css("background-image").indexOf("up") == -1 || ballx < 120) { // flipper is down or bumper hit
				if(bally >= ballx * 0.439 - 30) {
					//jQuery("#debug").val("hit");
					//totalv = Math.abs(vy) + Math.abs(vx);
					//newslope = -1 * vy/vx + 0.439;
					//vy = totalv * newslope;
					//vx = totalv = vy;
					vx += Math.max(0.25 * Math.abs(vy), 1);
					
					vy = -1 * Math.abs(vy) * damper ^ 2;
					bally = ballx * 0.439 - 30 - 1;
					//clearInterval(ballInterval);
				}
			} else { //flipper is up
				var nowTime = new Date();
				if(bally >= ballx * -0.439 + 85) {
					//jQuery("#debug").val("hit");
					//totalv = Math.abs(vy) + Math.abs(vx);
					//newslope = -1 * vy/vx + 0.439;
					//vy = totalv * newslope;
					//vx = totalv = vy;
					if(nowTime - flipTime < 300){
						vy = -40 * Math.max((ballx - 120) / parseFloat(flipper.css("width")), 0);
						//jQuery("#debug").val(vy);
						vx -= 20 * (nowTime - flipTime - 150) / 150;
						//vx -= 10;					
					} else {
						vy = -1 * Math.abs(vy) * damper;
						vx -= 0.5 * 1 / Math.abs(vy);
						//vx *= damper;
					}
					bally = ballx * -0.439 + 85 - 1;
					//clearInterval(ballInterval);
				}			
			}
		}
	} else {
		if(bally > flipper.position().top && ballx > 710 - parseFloat(flipper.css("width"))) {
			t = treset;
			
			if(flipper.css("background-image").indexOf("up") == -1 || ballx > 710) { // flipper is down or bumper hit
				if(bally >= ballx * -0.39 + 296) {
					//jQuery("#debug").val("hit");
					//totalv = Math.abs(vy) + Math.abs(vx);
					//newslope = -1 * vy/vx + 0.439;
					//vy = totalv * newslope;
					//vx = totalv = vy;
														
					vx -= Math.max(0.25 * Math.abs(vy), 1);
					vy = -1 * Math.abs(vy) * damper ^ 2;
					
					bally = ballx * -0.39 + 296 - 1;
					//clearInterval(ballInterval);
				}
			} else { //flipper is up
				var nowTime = new Date();
				if(bally >= ballx * 0.313 - 220) {
					//jQuery("#debug").val("hit");
					//totalv = Math.abs(vy) + Math.abs(vx);
					//newslope = -1 * vy/vx + 0.439;
					//vy = totalv * newslope;
					//vx = totalv = vy;
					jQuery("#debug").val(nowTime - flipTime);
					if(nowTime - flipTime < 300){
						vy = -40 * Math.max((710 - ballx) / parseFloat(flipper.css("width")), 0);
						//vy = -50;
						//jQuery("#debug").val(vy);
						vx += 20 * (nowTime - flipTime - 50) / 150;
						//vx += 10;										
					} else {
						vy = -1 * Math.abs(vy) * damper;
						vx += .5 * 1 / Math.abs(vy);
						//vx *= damper;
					}
					bally = ballx * 0.313 - 220 - 1;
					//clearInterval(ballInterval);
				}			
			}
		}
	}
}

function moveBall() {	
	//ballleft = parseFloat(jQuery("#ball").css("left"));
	//balltop = parseFloat(jQuery("#ball").css("top"));
	
	//ballx = ballleft + ballHalfWidth - bounds.left;
	//bally = balltop + ballHalfHeight;
	
	//jQuery("#debug").val(ballx + "," + bally);
	if(ballInPlay) {	
		if(!checkGameOver()) {	
			if(ballleft >= bounds.right - ballHalfWidth - 40) {
				vx = -1 * Math.abs(vx);	
			}
			
			if(ballleft <= bounds.left){
				vx = Math.abs(vx);
			}
			
			vx *= d;
			ballx = ballx + vx * multiplier;		
			
			
			if(balltop < 200){
				if(jQuery.browser.msie)
					t += 1;
				else 
					t += 0.5;
				vy = vy - G * t;
				bally = bally + vy * multiplier	
			}
			
			checkHit("#flipper_left");
			checkHit("#flipper_right");	
		}
		
		//if(mouseDown)
		jQuery("#debug").val(jQuery("#debug").val() + '\n' + G * Math.pow(t, 2));
		
		ballleft = ballx - ballHalfWidth + bounds.left
		balltop = bally - ballHalfHeight;
		
		ball.css("left", ballleft);
		ball.css("top", balltop);
	}
}
