$(document).ready(function() {	
	main_menu();
	send_to_colleague();
	print_page();	
	email_subscription();
	preload_images();
	video_introduction();
	contact_form();
});

/** 
 * Sets up the main menu using superfish. 
 */
function main_menu() {
	jQuery('#header div.menu ul').superfish({
		delay: 250,
		animation: {opacity: 'show', height: 'show'},
		speed: 'fast',
		autoArrows: false,
		dropShadows: false
	});	
}

/**
 * Attempts to conceal the mailto value as it is quite long and ugly when rendered in the status bar.
 */ 
function send_to_colleague() {
/*	
	$(".send_page a").mouseover(function() {				
		window.status = "";
		return true;
	});
	$(".send_page a").mouseout(function() {		
		window.status = "Done";
		return true;
	});
*/
}

/**
 * Adds an event handler to the send to a colleague hyperlink.
 */ 
function print_page() {
	$('.print_page a').click(function() {
		window.print();
		return false;
	});
}

/**
 * Wires up the email subscription with input labels, validation and ajax submit to campaign monitor. 
 */  
function email_subscription() {
	email_input_label();
		
	$("#campaign_monitor_form").campaignMonitor({
		before_submit_callback: validate_email_subscription
	});
}

/** 
 * Takes the label and adds it as the input value,
 * It is removed on focus and re-added on blur accordingly.
 */
function email_input_label() {
	var email_label = $("#campaign_monitor_form div.input label");
	var email_field = $("#campaign_monitor_form div.input input");
	
	// Set the email label as the input value.
	email_field.val(email_label.text());
	
	// Email field focus (remove default label).
	email_field.focus(function() {
		if (email_field.val() == email_label.text()) {
			email_field.val("");
		}
	});
	
	// Email field blur (add label if empty).
	email_field.blur(function() {
		if (email_field.val() == "") {
			email_field.val(email_label.text());
		}
	});	
}

/**
 * Validates the email subscription form. 
 */ 
function validate_email_subscription() {
	var email_field = $("#campaign_monitor_form div.input input");
	var email_pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email_value = email_field.val();
	
	var is_valid = email_pattern.test(email_value);
	
	if (is_valid) {
		email_field.parent('div.input').removeClass("error");
	}
	else {		
		$(email_field).parent('div.input').addClass("error");
	}
	
	return is_valid;
}

/** 
 * Preloads images in the hope that they will be cached by the browser for future use.
 */
function preload_images() {
	// Just cache the background images used for submenu.		
	// Caching the header images caused more trouble than it was worth.
	$.preloadImages(
		"img/background-submenu-item.jpg", 
		"img/background-submenu-shadow.png"
	);		
}

/** 
 * Converts the video_player div into a flash video player,
 * Passing in the appropriate parameters for the flv video and preview image.
 */
function video_introduction() {	
	$('#video_introduction div.video_player').flash({
	    src: 'flash/player.swf',
	    width: 300,
	    height: 190,
	    flashvars: {
		    file: '../videos/example.swf', 
	    	image: 'videos/example.jpg' 
	    },	    
	});	
}
		
/** 
 * Sets up the contact form with validation and ajax submit.
 */
function contact_form() {
	var form_object = $("form#contact_form");
	form_object.append("<div class='validation'><p></p></div>");
	var validation = form_object.find("div.validation");
		
	form_object.validate({							   
		submitHandler: function(form) {
			$.ajax({
				type: form_object.attr("method"),
				url: form_object.attr("action"),
				data: form_object.serialize(),
				success: function(result) {	
					// Inject the result into the page.
					form_object.before(result);
					var thankyou = form_object.prev("div.thankyou");
					thankyou.hide();
					
					// Fade in the thankyou message.			
					form_object.fadeOut("fast", function() {				
						thankyou.fadeIn("slow");
					});
				}
			});	
		},		
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed a mandatory field (it has been highlighted).'
					: 'You missed ' + errors + ' mandatory fields (they have been highlighted).';
				
				validation.find("p").text(message);
				validation.show();
			} else {
				validation.hide();
			}
		},					
		rules: {			
			your_email_address: "required",						
			your_message: "required"		
		}		
	});
}
