﻿$(document).ready(function(){

	$("#content div").hide();

	$("#content div:first").show();
	$("#nav li:first").addClass("active");

	$("#nav li a").click(function(){
		var $t = $(this);
		$("#content div:visible").fadeOut("fast", function(){
			$('#nav li').removeClass("active");
			$t.parent().addClass("active");

			var current = $t.attr("href");


		
			$("#content").stop().animate({"height":$(current).height()},function(){
				$(current).fadeIn("fast");
			});		
		});

		
		return false;	
	});
	$('input:not(#submit_btn), textarea').bind('focus', function() {
		oldValue = $(this).val();
		if(oldValue == 'پيغام شما...' || oldValue == 'نام' || oldValue == 'موضوع پيغام' || oldValue == 'ايميل'){
			$(this).val('');
		}
	});
	$('input:not(#submit_btn), textarea').bind('blur', function() {
		newValue = $(this).val();
		if (newValue == '') {
			$(this).val(oldValue);
		}
	});
	
	//Contactform functions
	var active = 0;
	function show_message(themessage){
		if(active == 0){
			active =1;
			$('#contact').append('<div id="message">'+themessage+'</div>');
			$('#contactform').fadeTo(0.5,250);
			$('#message').animate({'opacity':0},1,function(){
				$('#message').css({'display':'block'})
				.animate({'opacity':1},250)
				.animate({'opacity':1},1500)
				.animate({'opacity':0},250,function(){
						$(this).hide().remove(); active = 0;
					});
			});
		};
	};
	
	$('#submit_btn').click(function(){
		var c_message = $('#contact_message').val();
		var c_name = $('#contact_name').val();
		var c_subject = $('#contact_subject').val();
		var c_email = $('#contact_email').val();
		
		var error_message = '';
		var error_name = '';
		var error_subject = '';
		var error_email = '';
		
		if(c_message == "" || c_message == "پيغام شما..." || c_message.length < 5){
			error_message = 'error';
			show_message('لطفا پيغام خود را وارد کنيد');
		}else{
			error_message = '';
			$('#contact_message').animate({'opacity':0.3});
		};
		
		if(c_name == "" || c_name == "نام" || c_name.length < 3){
			error_name = 'error';
			show_message('لطفا نام خود را وارد کنيد');
		}else{
			error_name = '';
			$('#contact_name').animate({'opacity':0.3});
		};
		
		if(c_subject == "" || c_subject == "موضوع پيغام" || c_subject.length < 3){
			error_subject = 'error';
			show_message('لطفا موضوع پيغام را وارد کنيد');
		}else{
			error_subject = '';
			$('#contact_subject').animate({'opacity':0.3});
		};
		
		if(c_email == "" || c_email == "ايميل" || c_email.length < 3){
			error_email = 'error';
			show_message('لطفا ايميل خود را وارد کنيد');
		}else{
			var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
			if (!filter.test(c_email)) {
				error_email = 'error';
				show_message('لطفا ايميل صحيح را وارد کنيد');
			}else{
				error_email = '';
				$('#contact_email').animate({'opacity':0.3});
			}
		};
			
			if(error_message == '' && error_name == '' && error_subject == '' && error_email == ''){
				//Make a string with all informations
				var c_data = 'submit=1&name=' + c_name + '&subject=' + c_subject + '&email=' + c_email + '&message=' + c_message;
				//Send the datastring to the phpfile which sends it
				$.ajax({
				   type: "POST",
				   url: "_sendmail.php",
				   data: c_data,
				   success: function(d){
					   if (d=="ok"){
							show_message('پيغام شما با موفقيت ارسال شد!');
							$('#submit_btn').fadeOut('fast');
						}else{
							show_message('خطا در ارسال!');
							$('#contactform').find("input, textarea").css("opacity", 1)
						}
				   }
				});
			}else{
				
			};
			
		return false;
	});
});