$(document).ready(function() {

	// Background color change

	function spectrum(){
		 
		var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';  
		
		$('body').animate( { backgroundColor: hue }, 1000);  
		
		spectrum();  
	}  

	// Modal and hover-text

	$('#modal-link-contact').click(function() {
		$('.modal-contact').modal({
			opacity: 50
		});

		return false;
	});
	
	$('#modal-link-colophon').click(function() {
		$('.modal-colophon').modal({
			opacity: 50
		});

		return false;
	});
	
	$('#modal-link-what').click(function() {
		$('.modal-what').modal({
			opacity: 50
		});

		return false;
	});
	
	$("a.contact").hover(function() {
		$("p.cm").show();
	});
	
	$("a.contact").mouseout(function() {
		$("p.cm").hide();
	});
	
	$("a.colophon").hover(function() {
		$("p.cn").show();
	});
	
	$("a.colophon").mouseout(function() {
		$("p.cn").hide();
	});
	
	$("a.what").hover(function() {
		$("p.wtf").show();
	});
	
	$("a.what").mouseout(function() {
		$("p.wtf").hide();
	});
		
	// Contact form
	
	$("#loading").hide();
	$("input.button-submit").click(function() {
		
		$(".confirm").hide();
		$(".error").hide();
		
		var error = $("p.success").length;
				
		if(error != 0) {
			$("p.success").remove();
		}
		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var contactmail = $("#email").val();
		if(contactmail == '') {
			$(this).after('<p class="error">Please enter an email address.</p>');
			hasError = true;
		} else if(!emailReg.test(contactmail)) {	
			$(this).after('<p class="error">Please enter a valid email address.</p>');
			hasError = true;
		}
		
		if(hasError == false) {
			$("#loading").show();
			
			var email = $("input#email").val();  
			var message = $("textarea#message").val();  
			var dataString = '&email=' + email + '&message=' + message;  
			
			$.ajax({
				type: "POST",
				data: dataString,  
				url: "sendmailcontact.php",
				success: function() {
					$("input.button-submit").after('<p class="success">Thank you. Your message has been sent.</p>');											
					$("#loading").hide();
				}
			});
			
            $("input#email").val('');
            $("textarea#message").val('');

            $("textarea#message").focus('');

		}
		
		return false;
				
	});
	
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-5137280-1']);
	_gaq.push(['_trackPageview']);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();

});

