$(function(){
	$("#side_contact .contact_send").click(function(){

		if(!is_valid_email($("#side_contact .contact_email").val())) {
			alert("Please insert valid email address.");
			return;
		}
		$("#side_contact .contact_send").replaceWith("<img src='/img/wait.gif' alt='Please wait...' class='contact_send'/>");
		$.ajax({
			type: "POST",
			data: "n=" + encodeURIComponent($("#side_contact .contact_name").val()) + 
				"&e=" + encodeURIComponent($("#side_contact .contact_email").val()) + 
				"&m=" + encodeURIComponent($("#side_contact .contact_message").val()),
			url: "/ajax/contact.php",
			success: function() {
				$("#side_contact .contact_send").replaceWith("<span style='padding:0 5px' class='contact_send'>Thank you!</span>");
				$("#side_contact input,#side_contact textarea").val("");
			}
		});
	});

	$("#white_paper_form_submit").click(function(){

		if(!is_valid_email($("#white_paper_form .email").val())) {
			alert("Please insert valid email address.");
			return;
		}
		$("#white_paper_form_submit").replaceWith("<img src='/img/wait.gif' alt='Please wait...' class='submit' />");
		$.ajax({
			type: "POST",
			data: "n=" + encodeURIComponent($("#white_paper_form .name").val()) + 
				"&c=" + encodeURIComponent($("#white_paper_form .company").val()) + 
				"&t=" + encodeURIComponent($("#white_paper_form .title").val()) + 
				"&e=" + encodeURIComponent($("#white_paper_form .email").val()) + 
				"&m=" + encodeURIComponent($("#white_paper_form .comments").val()),
			url: "/ajax/white_paper.php",
			success: function() {
				/*$("#white_paper_form .submit").replaceWith("<span style='padding:0 5px'>Thank you!</span>");
				$("#white_paper_form input,#white_paper_form textarea").val("");*/
				location.href = white_paper_target_url;
			}
		});
	});

	$(".page_a").mouseenter(function(){
		$(this).children(".sub_menu").fadeIn(200);
	});
	$(".page_a").mouseleave(function(){
		$(this).children(".sub_menu").fadeOut(200);
	});

});


function is_valid_email (email){
	return /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

function home_clients_list(arr) {
	if(!arr.length)
		return;
	var current = Math.floor(Math.random()*arr.length);
	var timeout_interval = 1500; //ms

	$img = $("#home_customers_img");
	$img.load(function(){
		$img.fadeIn(1000, function(){
			if(arr.length>1)
				setTimeout(home_clients_list_timeout, timeout_interval);
		});
	});
	$img.attr("src", '/upload/clients159x84/' + arr[current].img_file_small);
	$img.attr("alt", arr[current].name);

	$img.data("arr",arr);
	$img.data("current",current);
}

function home_clients_list_timeout() {
	var arr = $img.data("arr");
	var current = $img.data("current");
	current = (current + /*Math.floor(Math.random()*(arr.length-1))+*/ 1) % arr.length;
	$img.data("current",current);

	$img = $("#home_customers_img");
	$img.fadeOut(1000, function(){
		$img.attr("src", '/upload/clients159x84/' + arr[current].img_file_small);
		$img.attr("alt", arr[current].name);
	});
}
