$(document).ready(function(){
	
	// Newsletter
	
	$("#newsletter").submit(function(){
		
		$("a.nl_back").live("click", function(){
			$("#newsletterStatus").hide();
			$("#newsletterFields").show();
		});
		
		$("#newsletterFields").hide();
		$("#newsletterStatus").html('Submitting...').show();
		
		$.ajax({
			type: "POST",
			url: "newsletter/",
			data: {
				email: $("#newsletter_email").val()
			},
			success: function( data, textStatus, XMLHttpRequest ){
				if( data == "1" ){
					$("#newsletterStatus").html('Thanks for subscribing!').show();
				}else{
					$("#newsletterStatus").html('<span style="color: #bb1600;">Email address invalid. Please <a href="javascript:;" class="nl_back" style="color: #7c1700;">go back</a> and try again.</span>').show();
				}
			},
			error: function(){
				$("#newsletterStatus").html('<span style="color: #bb1600;">An error has occured. Please <a href="javascript:;" class="nl_back" style="color: #7c1700;">go back</a> and try again.</span>').show();
			}
		});
		
	});
	
	// Form tips
	
	$("input.inline_tip").focus(function(){
		if( $(this).hasClass("empty") ){
			$(this).val("").removeClass("empty");
		}
	}).blur(function(){
		if( $(this).val() == "" ){
			$(this).addClass("empty").val( $(this).attr("title") );
		}
	}).blur();
	
	// Modal
	
	$(".modal").colorbox({
		opacity: 0.6,
		height: 500,
		width: 600
	});
	
	// Serial remove
	
	$("a.remove_serial_yes").live("click", function(){
		document.location = $(this).parents("p.serial:first").find("a.serial_remove").attr("href");
		return false;
	});
	
	$("a.remove_serial_no").live("click", function(){
		$(this).parents("span.remove_serial_confirmation:first").remove();
		return false;
	});
	
	$("p.serial a.serial_remove").click(function(){
		$(this).after('<span class="remove_serial_confirmation"> - are you sure? <a href="" class="remove_serial_yes">Yes</a> / <a href="" class="remove_serial_no">No</a></span>');
		return false;
	});
	
	// Move onto next field once maxlength is reached
	
	$("input[type=text]").keyup(function(){
		
		if( parseInt( $(this).attr("maxlength") ) > 1 && $(this).val().length == parseInt( $(this).attr("maxlength") ) ){
			$(this).next("input").focus().select();
		}
		
	});
	
});
