// JavaScript Document

$.getDocHeight = function()
{
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );
};

$.changeImgSrc = function(imgObj, newSrc)
{
	$(imgObj).attr("src", newSrc);
}

function onBodyLoad()
{
	checkFooterResizeNeeded();
	preloadImages();
	makeFacebookButtonInterractive();
}

function checkFooterResizeNeeded()
{
	var windowHeight = $.getDocHeight();
	var wrapperHeight = $("#wrapperDiv").height();
	
	if (windowHeight > wrapperHeight)
	{
		$("#footerDiv").height($("#footerDiv").height() + windowHeight - wrapperHeight);
		$("#restOfFooterDiv").height($("#restOfFooterDiv").height() + windowHeight - wrapperHeight);
		
		var paddingTopVal = $("#copyrightDiv").css("padding-top").substring(0, $("#copyrightDiv").css("padding-top").indexOf("px"));
		$("#copyrightDiv").css("padding-top", (parseInt(paddingTopVal) + windowHeight - wrapperHeight) + "px");
	}
}

function preloadImages()
{
	$.imgpreload(['img/menu_about_hl.jpg', 'img/menu_contact_hl.jpg', 'img/menu_play_hl.jpg', 'img/menu_report_hl.jpg', 'img/play_btn_click.jpg'], function() {});
}

function yourNameTxtClick()
{
	if (($("#yourNameTxt").css("color") == "rgb(173, 167, 152)") || ($("#yourNameTxt").css("color") == "#ada798"))
	{
		$("#yourNameTxt").val("");
		$("#yourNameTxt").css("color", "#4a4741");
	}
}

function yourNameTxtBlur()
{
	if ($("#yourNameTxt").val() == '')
	{
		$("#yourNameTxt").val("Your Name");
		$("#yourNameTxt").css("color", "#ada798");
	}
}

function playBtnToggleImg()
{
	var imgSrc = ($("#playBtnImg").attr("src") == "img/play_btn_hover.jpg" ? "img/play_btn.jpg" : "img/play_btn_hover.jpg")
	$("#playBtnImg").attr("src", imgSrc);
}

function bugImageChange()
{
	var imgName = "";
	var imgPath = $("#bugImage").val();
	var imgParts = imgPath.split("\\");
	
	if (imgParts.length == 1)
	{
		// try '/', maybe it's linux
		imgParts = imgPath.split("/");
		
	}
	
	imgName = imgParts[imgParts.length - 1];
	
	$("#imageUploadTxt").val(imgName);
}

// from the old one

var gameHasFocus = false;

document.onkeydown = documentKeydown;

function documentKeydown(evt)
{
	if (!evt)
	{
		evt = event;
	}
	
	if ((evt.keyCode == 8) || // backspace
	(evt.keyCode == 115) || // F4
	(evt.keyCode == 116) || // F5
	(evt.ctrlKey && evt.keyCode == 82)) // Ctrl + R
	{
		gameHasFocus = false; 
	}
}

function submitNameAndPlay()
{
	var name = document.getElementById("yourNameTxt").value;
	
	if ((name == "") || (name == "Your Name"))
	{
		document.getElementById("errorMessageDiv").innerHTML = "Please enter your name before playing";
		return;
	}
	
	if (name.length > 30)
	{
		document.getElementById("errorMessageDiv").innerHTML = "Please enter a name less than 30 characters long (it doesn't have to be your full name)";
		return;
	}
	
	document.getElementById("errorMessageDiv").innerHTML = "";
	
	document.getElementById("yourNameForm").submit();
}

function quitGame(evt)
{
	if (gameHasFocus)
	{
		return;
	}
	
	var httpObj = getHttpObj();
	httpObj.open("POST", "actions/quit.php", false);
	httpObj.send();
	
	document.getElementById("gameDiv").style.visibility = "hidden";
	alert("Thank you for playing!");
}

function getHttpObj()
{
	var httpObj;
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		httpObj = new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		httpObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return httpObj;
}

function gameDivHasFocus()
{
	gameHasFocus = true;
}

function gameDivLostFocus()
{
	gameHasFocus = false;
}

function clearBtnClick()
{
	document.getElementById("nameTxt").value = "";
	document.getElementById("emailTxt").value = "";
	document.getElementById("bugTxt").value = "";
	document.getElementById("bugImage").value = "";
	document.getElementById("imageUploadTxt").value = "";
	
	clearBugErrors();
}

function clearContactBtnClick()
{
	document.getElementById("nameTxt").value = "";
	document.getElementById("emailTxt").value = "";
	document.getElementById("subjectTxt").value = "";
	document.getElementById("messageTxt").value = "";
	
	clearContactErrors();
}

function waitFormSubmit(submitBtnId, clearBtnId)
{
	document.getElementById(submitBtnId).disabled = true;
	document.getElementById(submitBtnId).value = "Wait";
	document.getElementById(clearBtnId).disabled = true;
	
	return true;
}

// set all handlers for the play button to make it interractive
function makePlayButtonInterractive()
{
	// set initial opacity
	$('#playBtnImg').animate({opacity: 0.7}, 1);
	
	// hover event handler
	$('#playBtnImg').hover(
		// mouse over
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 1}, 500);
		}, 
		// mouse out
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 0.7}, 500);
		}
	);
	
	// mouse down event handler
	$('#playBtnImg').mousedown(
		function()
		{
			$(this).attr('src', 'img/play_btn_click.jpg');
		}
	);
	
	// mouse up event handler
	$('#playBtnImg').mouseup(
		function()
		{
			$(this).attr('src', 'img/play_btn.jpg');
		}
	);
}

// set handlers to make the facebook button at the bottom interractive
function makeFacebookButtonInterractive()
{
	$('#facebookLink').hover(
		// mouse over
		function(e)
		{
			$(this).css('color', '#ffe299');
		}, 
		// mouse out
		function(e)
		{
			$(this).css('color', '#ffffff');
		}
	);
}

function browseButtonClick()
{
	$('#bugImage').trigger('click');
}

// bug page onload handler
function onBugPageLoad()
{
	activateBugPageTooltips();
	makeBugButtonsInterractive();
	checkErrorExists();
}

// contact page onload handler
function onContactPageLoad()
{
	activateBugPageTooltips();
	makeContactButtonsInterractive();
	checkErrorExists();
}

// activate tooltips for bug page
function activateBugPageTooltips()
{
	$(".errorCell img").qtip({
		position: {
			my: 'left center',
			at: 'right center'
		},
		content: {
			attr: 'title'
		},
		style: {
			classes: 'ui-tooltip-rounded ui-tooltip-shadow ui-tooltip-chessbreak'
		}
	});
}

// make bug submission form interactive
function makeBugButtonsInterractive()
{
	makeBrowseButtonInterractive();
	makeSubmitButtonInterractive();
	makeClearButtonInterractive();
}

// make contact form interactive
function makeContactButtonsInterractive()
{
	makeSubmitButtonInterractive();
	makeClearButtonInterractive();
}

// set all handlers for the browse button to make it interractive
function makeBrowseButtonInterractive()
{
	// set initial opacity
	$('#browseImg').animate({opacity: 0.7}, 1);
	
	// hover event handler
	$('#browseImg').hover(
		// mouse over
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 1}, 500);
		}, 
		// mouse out
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 0.7}, 500);
		}
	);
	
	// mouse down event handler
	$('#browseImg').mousedown(
		function()
		{
			$(this).attr('src', 'img/browse_btn_click.png');
		}
	);
	
	// mouse up event handler
	$('#browseImg').mouseup(
		function()
		{
			$(this).attr('src', 'img/browse_btn2.png');
		}
	);
}

// set all handlers for the submit button to make it interractive
function makeSubmitButtonInterractive()
{
	// set initial opacity
	$('#submitImg').animate({opacity: 0.7}, 1);
	
	// hover event handler
	$('#submitImg').hover(
		// mouse over
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 1}, 500);
		}, 
		// mouse out
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 0.7}, 500);
		}
	);
	
	// mouse down event handler
	$('#submitImg').mousedown(
		function()
		{
			$(this).attr('src', 'img/submit_btn_click.png');
		}
	);
	
	// mouse up event handler
	$('#submitImg').mouseup(
		function()
		{
			$(this).attr('src', 'img/submit_btn.png');
		}
	);
}

// set all handlers for the clear button to make it interractive
function makeClearButtonInterractive()
{
	// set initial opacity
	$('#clearImg').animate({opacity: 0.7}, 1);
	
	// hover event handler
	$('#clearImg').hover(
		// mouse over
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 1}, 500);
		}, 
		// mouse out
		function(e)
		{
			$(this).hoverFlow(e.type, {opacity: 0.7}, 500);
		}
	);
	
	// mouse down event handler
	$('#clearImg').mousedown(
		function()
		{
			$(this).attr('src', 'img/clear_btn_click.png');
		}
	);
	
	// mouse up event handler
	$('#clearImg').mouseup(
		function()
		{
			$(this).attr('src', 'img/clear_btn.png');
		}
	);
}

// submit bug sending form
function submitBtnClick()
{
	clearBugErrors();
	
	if (validateBugForm())
	{
		$('#submitBugForm').submit();
		$('#submitImg').unbind('click');
	}
}

// submit contact sending form
function submitBtnContactClick()
{
	clearContactErrors();
	
	if (validateContactForm())
	{
		$('#submitBugForm').submit();
		$('#submitImg').unbind('click');
	}
}

// validate the bug form and display errors
function validateBugForm()
{
	var res = true;
	var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	
	if ($('#nameTxt').val() == '')
	{
		$('#nameErrorImg').css('visibility', 'visible');
		res = false;
	}
	
	if ($('#bugTxt').val() == '')
	{
		$('#descriptionErrorImg').css('visibility', 'visible');
		res = false;
	}
	
	if ($('#emailTxt').val() == '')
	{
		$('#emailErrorImg').attr('title', 'Please fill in your e-mail');
		$('#emailErrorImg').css('visibility', 'visible');
		res = false;
	}
	else if (!$('#emailTxt').val().match(emailRegex))
	{
		$('#emailErrorImg').attr('title', 'Please provide a valid e-mail');
		$('#emailErrorImg').css('visibility', 'visible');
		res = false;
	}
	
	return res;
}

// validate the contact form and display errors
function validateContactForm()
{
	var res = true;
	var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	
	if ($('#nameTxt').val() == '')
	{
		$('#nameErrorImg').css('visibility', 'visible');
		res = false;
	}
	
	if ($('#emailTxt').val() == '')
	{
		$('#emailErrorImg').attr('title', 'Please fill in your e-mail');
		$('#emailErrorImg').css('visibility', 'visible');
		res = false;
	}
	else if (!$('#emailTxt').val().match(emailRegex))
	{
		$('#emailErrorImg').attr('title', 'Please provide a valid e-mail');
		$('#emailErrorImg').css('visibility', 'visible');
		res = false;
	}
	
	if ($('#subjectTxt').val() == '')
	{
		$('#subjectErrorImg').css('visibility', 'visible');
		res = false;
	}
	
	if ($('#messageTxt').val() == '')
	{
		$('#messageErrorImg').css('visibility', 'visible');
		res = false;
	}
	
	return res;
}

// clear errors on the bug form
function clearBugErrors()
{
	$('#nameErrorImg').css('visibility', 'hidden');
	$('#descriptionErrorImg').css('visibility', 'hidden');
	$('#emailErrorImg').css('visibility', 'hidden');
}

// clear errors on the contact form
function clearContactErrors()
{
	$('#nameErrorImg').css('visibility', 'hidden');
	$('#emailErrorImg').css('visibility', 'hidden');
	$('#subjectErrorImg').css('visibility', 'hidden');
	$('#messageErrorImg').css('visibility', 'hidden');
}

// check if error div exists and if so, shift form table up
function checkErrorExists()
{
	if ($('.formError').length > 0)
	{
		var errorHeight = $('.formError').first().height();
		$('#formTable').css('margin-top', '-' + errorHeight + 'px');
	}
}
