$(function()
{
	/* Navigation Drop Down */
	$("li.sub_menu > a").click(function(e){
		e.preventDefault();
		$(this).parent().children("ul").stop(true,true).slideToggle('slow', 'easeOutQuad');
	});

	/* Mobile menu */
	$("#primary_nav").mobileMenu();
	
	/* Back to Top Button */
	$('#back_to_top').abe_backToTop({
		speed: 1000, // milliseconds - default 1000
		easing: 'easeOutQuad' // easing - default swing
	});
	
	// Sticky Navigation
	$("#navigation").abe_navFollow({
		follow: false, // follow or not by default - true/false
		lock: true, // display lock - true/false
		easing: 'easeOutQuad', // easing - default swing
		speed: 1000 // milliseconds - default 1000
	});
	

	/* Infobar */
	$('.infobar').abe_infobar({
		visible: true // true or false - default true
		/* If you will be using this, don't forget to add "," after "visible: true"
		speed: 600, // milliseconds - default 600
		easing: 'easeOutQuad', // easing - default swing
		button: '.infobar_btn' // open | close button - default .infobar_btn
		*/
	});

	/* Contact Form */
	$(".send_email").click(function(e){
		e.preventDefault();

		var iName, iEmail, iSubject, iMessage, iStatus, name, email, subject, message;
		
		iName = $("#name");
		iEmail = $("#email");
		iSubject = $("#subject");
		iMessage = $("#message");
		iStatus = $("#sendMsg");
		
		iStatus.stop().hide();
		//Collecting all data
		name = iName.val();
		email = iEmail.val();
		subject = iSubject.val();
		message = iMessage.val();
		
		var nameErr = 0, emailErr = 0, messageErr = 0;
		//Validating all required fields
		if (!isValidName(name))
		{ iName.addClass("error_input");  nameErr++; } else { iName.removeClass("error_input"); nameErr = 0; }
		
		if (!isValidEmail(email))
		{ iEmail.addClass("error_input");  emailErr++; } else { iEmail.removeClass("error_input"); emailErr = 0; }
		
		if (message.length < 1)
		{ iMessage.addClass("error_input");  messageErr++; } else { iMessage.removeClass("error_input"); messageErr = 0; }
		
		//Checking test results
		if (nameErr == 0 && emailErr == 0 && messageErr ==0)
		{
			$.ajax({
			type: "POST",
			url: "./scripts/php/mail-ajax.php",
			data: "name=" + name +"&email=" + email + "&subject=" + subject + "&message=" + message,
			dataType: "json"
			}).done(function( data ) {
				if (data.msg == "success")
				{
					iStatus.hide().removeClass("error_text").html("Thank you! Your email was sent.").fadeIn("fast").delay(10000).fadeOut("fast");
					
					//Reset fields
					iName.val("");
					iEmail.val("");
					iSubject.val("");
					iMessage.val("");
				}
				else
				{
					iStatus.hide().addClass("error_text").html("Oops! Something went wrong! Please try again.").fadeIn("fast").delay(10000).fadeOut("fast");
				}
			}).fail(function() {
				iStatus.hide().addClass("error_text").html("Oops! Something went wrong! Please try again.").fadeIn("fast").delay(10000).fadeOut("fast");
			});
		}
		else
		{
			iStatus.hide().addClass("error_text").html("Message was not send. Please fill all required fields and try again.").fadeIn("slow").delay(10000).fadeOut("slow");
		}
		
		//Clearing iStatus
		iStatus.html("");
	});
});

/* Validation Functions */
function isValidEmail(emailAddress) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	
	return pattern.test(emailAddress);
}

function isValidName(name)
{
	var pattern = new RegExp(/^[a-zA-Z\s\']{2,30}$/i);
	
	return pattern.test(name);
}