$("document").ready(function() {
	$(".mid").width($(".header").width()-60);
	var eachSection = new Array($(".aboutHolder"),$(".imagesHolder"),$(".gamesHolder"),$(".contactHolder"));
	var currentHeight = 0;
	var windowHeight = $(window).height();
	var RE = /^\d*$/;
	for(var i in eachSection)
	{
		eachSection[i].css('top', currentHeight);
		currentHeight = currentHeight + (windowHeight - eachSection[i].height());
	}
	
	$(window).resize(function(event){
		currentHeight = 0;
		windowHeight = $(window).height();
		i = 0;
		for(i in eachSection)
		{
			if(RE.test(currentHeight)){
				eachSection[i].css('top', currentHeight);
				currentHeight = currentHeight + (windowHeight - eachSection[i].height());
			}
		}
		changePage(currentPlace);
	})
	$("a#seascape").fancybox();
	$("a#rainbowness").fancybox();
	$("a#bokeh").fancybox();
})

$(".header-centre").click(function() {
	window.location = "#about";
})

var removedContents = new Array();

function changePage(sourcePlace) {
	currentPlace = sourcePlace;
	window.location = "#"+sourcePlace;
}

function changeFormColour(form) {
	form.css('color', '#999999');
}

var isChanged;

function removeFirstContents(form) {
	i=0;
	isChanged = false;
	for(i in removedContents)
	{
		if(removedContents[i] == form)
		{
			isChanged = true;
		}
	}
	if(isChanged == false)
	{
		form.val("");
		removedContents.push(form);
	}
}

function sendForm(){
	var subject = String($("#subject").val());
	var message = String($("#message").val());
	var fromaddress = String($("#fromaddress").val());
	var fromname = String($("#fromname").val());
	$("#formSection").html("<p style='text-align: center;'><img src='img/loader.gif' alt='loader' /></p>");
	var name = $("#subject").val();
	$.ajax({
		cache: false,
		url: "mail.php",
		data: "subject="+subject+"&message="+message+"&fromname="+fromname+"&fromaddress="+fromaddress,
		success: function(data){
			$("#formSection").html(data)
		}
	})
};

var emailText = '<input id="subject" onFocus="removeFirstContents($(\'#subject\')); changeFormColour($(\'#subject\'))" value="Subject" /><br /><br /><textarea id="message" onFocus="removeFirstContents($(\'#message\'));changeFormColour($(\'#message\'))" rows="10">Message</textarea><br /><br />	<input id="fromname" onFocus="removeFirstContents($(\'#fromname\')); changeFormColour($(\'#fromname\'))" value="Your name" /><br /><br />	<input id="fromaddress" onFocus="removeFirstContents($(\'#fromaddress\')); changeFormColour($(\'#fromaddress\'))" value="Your email address" /><br /><br /><input type="submit" value="Send" onMouseDown="sendForm()" />';
	
function redoMail()
{
	$("#formSection").html(emailText);
}

function navLinks(type){
	var listArray = ['About','Images','Games','Contact'];
	var endString = "";
	var extraClass;
	for(i = 0; i<listArray.length; i++){
		if(type == listArray[i].toLowerCase())
		{
			extraClass = " active";
		} else {
			extraClass = "";
		}
		
		endString = endString + '<a href="javascript:changePage(\''+listArray[i].toLowerCase()+'\')"><li class="navLink'+extraClass+'">'+listArray[i]+'</li></a>\n';
	}
	return endString;
}