$(document).ready(function(){
//Rounded corner inputs (cheaty)
	$('input.txt').each(function(){
		$(this).wrap('<div class="inpBG"></div>');
	});

	$('textarea.txt').each(function(){
		$(this).wrap('<div class="txtBG"></div>');
	});

//Initialise the menu
	$('#mnu').supersubs({
		minWidth: 8,
		maxWidth: 30,
		extraWidth: 5
	}).superfish();

//Overlay the text in the search box
	$('.labelOver').labelOver();

//Fit the header text
	$('#tagline').fitTextToBox();

//Hide the various contact us forms
	//$('.hiddenForm').toggle("fold");
	$('.hiddenForm').hide();
	$('#molName').focus(function(){
		if($('#showHideContactUs').length <= 0){
			//$('#contactUs').toggle("fold");
			$('#contactUs').slideDown();
			$('#contactUs').before('<p id="showHideContactUs"><a href="#">Hide the form</a></p>');
			$('#showHideContactUs a').click(HideForm);
		}
	});

//Form validation
	// validate signup form on keyup and submit
	$("#contactForm").validate({
		errorClass: "invalid",
		rules: {
			name: "required",
			phone: {
				required: true,
				minlength: 10
			},
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			name: "Please enter your name",
			phone: {
				required: "Please enter a phone number",
				minlength: "Your phone number must consist of at least 10 characters"
			},
			email: "Please enter a valid email address"
		},
		//TODO: Remove the valid classes when revalidating
		success: function(l) {
			//Work out which control the label is for
			var f = l.attr('for');
			l.remove();
			if (f) {
				//Add the class to that control
				$('#' + f).parent().addClass("valid");
			}
		},
		errorPlacement: function(error, element) {
			error.insertAfter(element);
			//Find the element and fix the background etc
			element.parent().removeClass("valid");
			element.parent().addClass("invalid");
		}
	});

//Settings
	//Max number of decreases
	var maxDown = -1;
	//Max number of increases
	var maxUp = 5;

	var clicks = 0;
	var originalFontSize = $('body').css('font-size');
	
//Add the links
	$('#aeTxt').prepend('<a class="increaseFont" href="javascript:;" style="font-size: 1.2em;">+A</a>&nbsp;&nbsp;<a class="decreaseFont" href="javascript:;" style="font-size: 0.9em;">-A</a>&nbsp;&nbsp;&nbsp;');

// Reset Font Size
	$(".resetFont").click(function(){
		$('body').css('font-size', originalFontSize);
		clicks = 0;
	});

// Increase Font Size
	$(".increaseFont").click(function(){
		if(clicks >= maxDown && clicks < maxUp){
			var currentFontSize = $('body').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum + 1.2;
			clicks++;
			$('body').css('font-size', newFontSize);
			$(".decreaseFont").removeClass('disabled');
			$(".increaseFont").removeClass('disabled');
		}
		
		if(clicks == maxUp)
			$(".increaseFont").addClass('disabled');

		return false;
	});

// Decrease Font Size
	$(".decreaseFont").click(function(){
		if(clicks > maxDown && clicks <= maxUp){
			var currentFontSize = $('body').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum - 1.2;
			clicks--;
			$('body').css('font-size', newFontSize);
			$(".decreaseFont").removeClass('disabled');
			$(".increaseFont").removeClass('disabled');
		}
		
		if(clicks == maxDown)
			$(".decreaseFont").addClass('disabled');

		return false;
	});
});
function HideForm(){
	//$('#contactUs').toggle("fold");
	$('#contactUs').slideUp();
	$('#showHideContactUs').remove();
	
	//Clear any validation
	var validator = $("#contactForm").validate();
	validator.resetForm();
	
	return false;
}