/*
 * GLOBAL VARS
 */
 
var score = 0;
var mousex = 0;
var mousey = 0;
var game_started = false;
var flyby_timer = '';
var plane1_hit = false;
var plane2_hit = false;
var start_countdown = false;
var game_countdown = 61;
var imgs = new Array();

/*
 *
 * START: jQuery WINDOW LOAD FUNCTIONS
 *
 */
$(window).load(function() {

	load_images("images/plane1.gif","images/plane2.gif","images/plane3.gif","images/cityscape.gif","images/cloud1.gif","images/cloud2.gif","images/walking_monster.gif","images/explosion.gif","images/rolling_eyes.gif");

});
/*
 * END: jQuery WINDOW LOAD FUNCTIONS
 */



/*
 *
 * START: jQuery WINDOW READY FUNCTIONS
 *
 */
$(window).ready(function() {

	scared_ya();
	
	submit_form();
	
	field_checker();

	$(document).mousemove(function(e) {
		$('#status').html(e.pageX +', '+ e.pageY);
		mousex = e.pageX;
		mousey = e.pageY;
  });

	$('.plane').live('click', function() {
		if(game_started) {
			if(!start_countdown) {
				start_countdown = true;
				start_game_timer();
			}
			
			// SET UP PLANE DETAILS
			var plane = $(this).attr('id');
			var plane_position = $(this).position();
			var plane_length = $(this).width();
			var plane_height = $(this).height();
			var pxl = plane_position.left;
			var pxr = pxl + plane_length;
			var pyl = plane_position.top;
			var pyr = pyl + plane_height;
			var random_plane = Math.floor(Math.random()*3) + 1;;
			
			// CHECK IF WE HAVE A HIT
			if((mousex > pxl && mousex < pxr) && (mousey > pyl && mousey < pyr) && (!$(this).hasClass('explosion'))) {
				// SUCCESSFUL HIT ON PLANE
				$(this).addClass('explosion').html('<img class="hit" src="images/explosion.gif" />');
	
				// HANDLE HIT PLANE 1
				if($(this).attr('id') == 'plane1') {
					$.timer(1500, function (p1_timer_a) {
						$('#plane1').html('<img src="images/plane'+random_plane+'.gif" />').addClass('force_hide');
						p1_timer_a.stop();
						$.timer(1500, function (p1_timer_b) {
							$('#plane1').removeClass('force_hide').removeClass('explosion');
							p1_timer_b.stop();
						})
					})
				}
	
				// HANDLE HIT PLANE 2
				if($(this).attr('id') == 'plane2') {
					$.timer(1500, function (p2_timer_a) {
						$('#plane2').html('<img src="images/plane'+random_plane+'.gif" />').addClass('force_hide');
						p2_timer_a.stop();
						$.timer(1500, function (p2_timer_b) {
							$('#plane2').removeClass('force_hide').removeClass('explosion');
							p2_timer_b.stop();
						})
					})
				}
	
				score ++;
				$('#scoreboard').html(score);
			}
		}
	});
});
/*
 * END: jQuery WINDOW LOAD FUNCTIONS
 */



/*
 * CUSTOM FUNCTIONS
 */
function scared_ya() {
	var delay_time = 2000;	// TEST = 50, LIVE = 2000
	var use_monster = Math.floor(Math.random()*3)+1;
	$('#boo').html('<img id="monster_'+use_monster+'" class="monster" src="images/boo_'+use_monster+'.jpg" />');

	$('#boo').delay(delay_time).animate({
		top: '-=346'
	}, 300, function() {
		$('#boo').delay(delay_time).animate({
			left: '-=230'
		}, delay_time, function() {
			$('#contact').delay(10).fadeIn('slow');
			$('.monster_eyes').show();
			activate_eyes();
		});
	});
}

function submit_form() {
	$('#submit').click(function() {
		// CHECK ALL REQUIRED FIELDS ARE SELECTED
		var proceed = true;
		$('.error').removeClass('error');
		$('.required').each(function() {
			if($(this).val()=='') { 
				$(this).addClass('error');
				proceed = false;
			}
		});
		var what_monster = $('.monster').attr('id');
		var correct_hair = '';
		switch(what_monster) {
			case 'monster_1':
				correct_hair = 'yellow';
				break;
			case 'monster_2':
				correct_hair = 'pink';
				break;
			case 'monster_3':
				correct_hair = 'blue';
				break;
		}
		if($('#contact_check').val() != correct_hair) {
			proceed = false;
			$('#contact_check').addClass('error');
		}
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var email = $('#contact_mail');
		if(!emailReg.test(email.val())) {
			proceed = false;
			email.addClass('error')
		}
		
		if(!proceed) {
			var distance = 5;
			$('#boo').animate({
				left: '-='+distance
			}, 50, function() {
				$('#boo').animate({
					left: '+='+distance
				}, 50, function() {
					$('#boo').animate({
						left: '+='+distance
					}, 50, function() {
						$('#boo').animate({
							left: '-='+distance
						}, 100, function() {
							$('#warning').fadeIn();
						});
					});
				});
			});
		} else {
			// OK TO PROCEED
			$('#warning').hide();	// REMOVE WARNING HOVER
			if(!$.browser.msie) {
				$('.monster_eyes').html('<img src="images/rolling_eyes.gif" width="15" height="15" />');
				activate_eyes();
			}
			$('.required').each(function() {
				$(this).attr('disabled', true);
			});
			$('#submit').attr('disabled', true);
			var d1 = $('#contact_name').val();
			var d2 = $('#contact_mail').val();
			var d3 = $('#contact_regard').val();
			$.ajax({
				url: 'mail.php',
				data: 'd1='+d1+'&d2='+d2+'&d3='+d3,
				async: false,
				success: function(sts) {
					$('#submit').attr('disabled', false);
					// CLEAR FORM FIELDS FOR FIREFOX
					$('.required').each(function() {
						$(this).attr('disabled', false).val('');
					});
					$('#contact_container').html('<h2>Thank you</h2><p>I will have received that email by now and depending on whether I am awake or asleep, should get back in touch with you pretty soon.</p><p>Cheers</p>');
					$('.monster_eyes').html('<img src="images/pupil.png" width="15" height="15" />');
					activate_eyes();
					$('#boo, .monster_eyes').animate({
						left: '+=100',
						top:	'+=172'
					});
				}
			});
			final_scene();
		}
		return false;
	});
}

function field_checker() {
	$('.required').change(function() {
		$('#warning').fadeOut();
		if($(this).val()!='') {
			$(this).removeClass('error');
		}
	});
}

function activate_eyes() {
	EYES.follow('left-eye');
	EYES.follow('right-eye');
}

function final_scene() {
	// REMOVE MONSTER EYES FOR IE BROWSERS
	if($.browser.msie) {
		$('.monster_eyes').remove();
	}

	var wait = 1000;
	$('#city').fadeIn().delay(wait).pan({fps: 30, speed: 0.5, dir: 'right'});
	if(!$.browser.msie) {
		$('#cloud1').fadeIn().delay(wait).pan({fps: 50, speed: 0.5, dir: 'right'});
		$('#cloud2').fadeIn().delay(wait).pan({fps: 70, speed: 0.5, dir: 'right'});
	}
	$('#big-walking-monster').fadeIn().delay(wait).sprite({fps: 12, no_of_frames: 4}).animate({"left": "-=110%"}, 10000);
	$('#plane1').delay(3000).fadeIn('fast').animate({"left": "-=110%"}, 9000);
	$('#plane2').delay(4500).fadeIn('fast').animate({"left": "-=110%"}, 7500);
	$('#plane3').delay(6000).fadeIn('fast').animate({"left": "-=110%"}, 6000);
	
	var start_game_timer = 5000;  //15000;
	flyby_timer = setInterval(flyby, start_game_timer);
}


// SET NUMBER OF PLANES - THIS INCREMENTS UNTIL A STOP VALUE IS MET
var planes_on_screen = 1;

function flyby() {
	var scr_wid = $(document).width() - 100;
	var scr_hgt = $(document).height() - 50;
	switch(planes_on_screen) {
		case 1:
			$('#plane1').sprite({fps: 4, no_of_frames: 1}).spRandom({top: 0, bottom: scr_hgt, left: 0, right: scr_wid});
			break;
		case 2:
			$('#plane2').sprite({fps: 4, no_of_frames: 1}).spRandom({top: 0, bottom: scr_hgt, left: 0, right: scr_wid});
			start_game();
			clearInterval(flyby_timer);
			break;
	}
	planes_on_screen++;
}

function start_game() {
	game_started = true;
	$('body').addClass('game-on');

	// ONLY REACTIVATE EYES IF NOT IE	
	if(!$.browser.msie) {
		$('.monster_eyes').html('<img src="images/rolling_eyes.gif" width="15" height="15" />');
		activate_eyes();
	}
}

function start_game_timer() {
	$.timer(1000, function (game_timer) {
    game_countdown--;
		$('#countdown').html(game_countdown);
		if(game_countdown <= 0) {
			// GAME HAS ENDED
		if(!$.browser.msie) {
			$('.monster_eyes').html('<img src="images/pupil.png" width="15" height="15" />');
			activate_eyes();
		}
		$('body').removeClass('game-on');
	    game_timer.stop();
			game_started = false;
			$('#scoreboard, #countdown').fadeOut();
			
			var text = $('#contact_container').html();
			var score_text = '';
			if(score < 10) {
				score_text = '<h3>Meh, better luck next time!</h3>';
			}
			if(score >= 10 && score < 20) {
				score_text = '<h3>Not bad!</h3>';
			}
			if(score >= 20) {
				score_text = '<h3>Awesome skills</h3>';
			}
			var game_text = score_text + '<p>Your final score was '+score+', thanks for playing.</p>';
			$('#contact_container').html(text + game_text);
		}
	});
}

function load_images() {
	for (x=0; x<load_images.arguments.length; x++) {
		imgs[x] = new Image();
		imgs[x].src = load_images.arguments[x];
	}
}

