// Jason Grimme, StudioShorts.com Jan 2011

// Validate contact form and send data to server
function contactFormSubmit(form){
	// Set the slate clean
	var errors = false;
	$("#form-errors").html("");
	var name = $('[name="name"]');
	var email = $('[name="email"]');
	var message = $('[name="message"]');
	var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	// Poor validation for now
	if(name.val().length < 2){
		errors = true;
		$("#form-errors").append("<li>Please provide a valid name.</li>");
	}
	if(emailReg.test(email.val()) == false) {
		errors = true;
		$("#form-errors").append("<li>Please provide a valid email address.</li>");
	}	
	if(message.val().length < 10){
		errors = true;
		$("#form-errors").append("<li>Please enter in a meaningful message.</li>");
	}	
	if(errors){
		$("#form-errors").show(200);
		return false;
	}else{
		$("#form-errors").hide(200);		
		$.post("contact-submit.php", form.serialize(),
		function(data){
			return contactFormResponse(data);
		});	
		return false;
	}
	

}
// Handles server response of contact form.
function contactFormResponse(response){
	if(response == 1){
		$("#form-success").show(200);
		$('#contact-form')[0].reset();
		return true;
	}else{
		$("#form-errors").append(response);
		$("#form-errors").show(200);
		return false;
	}
}

	
// Switches the inner 'page' to the supplied one	
function switchContentBoxTo(boxID){
	// If there is a visible box, fade in.  Otherwise, display immediately
	var visibleBoxFound = false;
	$("#contentBox .dynamic").each(function(i){
		if($(this).css('display') != "none"){
			visibleBoxFound = true;
			$(this).fadeOut(100, function () {$("#" + boxID).fadeIn(200)});
		}
	});
	if(!visibleBoxFound){
		$("#" + boxID).show();
	}
}

// Indicate the current 'page' with a different style
function setCurrentViewLink(linkElement){
	$(".dynamic.current").removeClass('current');
	linkElement.addClass('current');	
}

// Called on page load
// Prepares nav to move up when clicked for the first time
function setMenuToHide(){
	$('#homeNav li.dynamic').each(function(i){
		$(this).unbind();
		$(this).click(function(event){
			event.preventDefault();
			$("#contentBox").fadeIn(500);
			hideMenu();
			//setMenuToShow();
		});		
	});
}

// Lowers the main navigation
function setMenuToShow(){
	$('#homeNav li.dynamic').each(function(i){
		$(this).unbind()
		$(this).click(function(event){
			event.preventDefault();
			showMenu();	
			setMenuToHide();
		});
	});
}

function hideMenu(){
	$("#homeCenter").animate({ 
		"margin-top": "-220px"
	}, 500 );
	
}
function showMenu(){
	$("#homeCenter").animate({ 
		"margin-top": "200px"
	}, 500 );
	
}

