$(document).ready(function() {

			$('#about_bt_content').hide();
			$('#say_hello_content').hide();
			$('#be_friends_content').hide();
			
			$('a.outlink').click(function() { window.open($(this).attr('href')); return false; });
			$('a.flash_closer').click(function() { layerOut();return false; });
			
			
	function resetForms(){
				
				$("span.error").remove();
				$('p.thanks-message').remove();
				$("#moreForm").show();
				$("#getInTouchForm").show();
				$("#be_friends_intro").show();
				$("#hello_intro").show();
				$("#emailAddress").val('');
				$("#more_emailAddress").val('');
				$("#more_Name").val('');
				$("#personName").val('');
				$("#personMessage").val('');
				
				
	}
	
	
	function hideLayers(){
			$('.content_layers').hide();
			resetForms();
	}
	

	function layerIn(showLayer){
	$('#shoebox-app').addClass('moved');
					$('#shoebox-app').animate({ 
        								left: 300
      									},300, function(){
      									$(showLayer).show();
      									});
     }
	
	function layerOut(){
					
					$('#shoebox-app').removeClass('moved');
					$('#shoebox-app').animate({ left: 0 }, 
												{duration: 300, complete: hideLayers});	
												
												
	}

	
	function move_layer(showLayer){
	
		if($('#shoebox-app').hasClass('moved')){
					
					layerOut();
					layerIn(showLayer);	
					
					} else {
					
      				
      				layerIn(showLayer);								
      									
      				}
	}
	
	
	
	$('#about_biscuit_tin').click(function(){
						
						if($('#about_bt_content').css('display')== 'block'){
								layerOut();
						}
						else {
						
						move_layer('#about_bt_content');
						}
						return false;
					
					
	});
	
	$('#say_hello').click(function(){
					if($('#say_hello_content').css('display')== 'block'){
								layerOut();
						}
						else {
						
					move_layer('#say_hello_content');
					}
					return false;
					
					
	});
	
	$('#be_friends').click(function(){
					if($('#be_friends_content').css('display')== 'block'){
								layerOut();
						}
						else {
					move_layer('#be_friends_content');
					}
					return false;
					
					
	});
	
	$('#shoebox-app').click(function(){
					layerOut();
	});
	
	
	
	
	///// This is for our Ajax forms
	
	$("#getInTouchForm input#mail_submit").click(function(){
		
		
		//// Check the values
		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		$("span.error").remove();
		
		var emailToVal = $("#emailAddress").val();
		if(emailToVal == '') {
			if($("body").hasClass("iphone")){
				$("#emailAddress").after('<span class="error">Hmmm - you might want to check that email field.<br/></span>');
			} else{
				$("#emailAddress").after('<span class="error"><br/>Hmmm - you might want to check that email field.<br/></span>');
			}
			hasError = true;
		} else if(!emailReg.test(emailToVal)) {
			if($("body").hasClass("iphone")){
				$("#emailAddress").after('<span class="error">Hmmm - you might want to check that email field.<br/></span>');
			} else{
				$("#emailAddress").after('<span class="error"><br/>Hmmm - you might want to check that email field.<br/></span>');
			}
			hasError = true;
		}
		
		var nameVal = $("#personName").val();
		if(nameVal == '') {
			if($("body").hasClass("iphone")){
					$("#personName").after('<span class="error">That\'s not your name... that\'s not your name.<br/></span>');
			} else{
				$("#personName").after('<span class="error"><br/>That\'s not your name... that\'s not your name.<br/></span>');
			}
			hasError = true;
		} 
		
		var messageVal = $("#personMessage").val();
		
		if(hasError == false){
				
	
	
				$.post( 
					"/contact/",
					{ personName : nameVal, emailAddress: emailToVal, personMessage: messageVal} ,
			  		function(message){ 
				        		$("#getInTouchForm").hide();
        						$("#hello_intro").hide();
        				if (message.length > 0){ 
            					$('#formSpace').append(message);} 
    						},
				    	"html"		    		
						);
		}
					
					
	
					

	/// Stop the form sending via normal means
		return false;
		
		
	});
	
	$("#moreForm input#more_submit").click(function(){
		
		
		//// Check the values
		
		var more_hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		$("span.error").remove();
		
		var more_emailToVal = $("#more_emailAddress").val();
		if(more_emailToVal == '') {
			$("#more_emailAddress").after('<span class="error"><br/>Hmmm - you might want to check that email field.<br/></span>');
			more_hasError = true;
		} else if(!emailReg.test(more_emailToVal)) {
			$("#more_emailAddress").after('<span class="error"><br/>Hmmm - you might want to check that email field.<br/></span>');
			more_hasError = true;
		}
		
		var more_nameVal = $("#more_Name").val();
		if(more_nameVal == '') {
			$("#more_Name").after('<span class="error"><br/>That\'s not your name... that\'s not your name.<br/></span>');
			hasError = true;
		} 
				
		if(more_hasError == false) {
			 
			 $.ajax({  
 			type: "POST",  
 			url: "/about/",  
			data: "more_emailAddress=" + more_emailToVal + "&more_Name=" + more_nameVal,
  			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			success: function(more_message){ 
        		
        		$("#moreForm").hide();
        		$("#be_friends_intro").hide();
        		
        		if (more_message.length > 0){ 
        			
            $('#formSpace2').append(more_message);} 
    		
    		},
    		error: function(){ 
        		 
        		
        		$("#moreForm").hide();
        		$("#be_friends_intro").hide();
        		
            $('#formSpace2').append('<p class="thanks-message">Sorry there seems to be a problem please try again later.</p>'); 
    		
    		}
    		
				});  
				return false;
	
					}
					
		if(hasError == false){
				
	
	
				$.post( 
					"/about/",
					{ more_emailAddress : more_emailToVal, more_Name: more_nameVal} ,
			  		function(more_message){
			  		
			  		$("#moreForm").hide();
        			$("#be_friends_intro").hide();
        		
        		if (more_message.length > 0){ 	
            			$('#formSpace2').append(more_message);} 
    		
    			},
				    	"html"		    		
						);
		}

	/// Stop the form sending via normal means
		return false;
		
		
	});


});