jQuery(document).ready(function() {
	 if (jQuery(".gallery ul li").length <= 6) {jQuery(".down, .up").remove();}
	jQuery(".mail").click(function() {
		jQuery('#form').animate({
			height: 'toggle', opacity: 'toggle'
		}, 500);
	});
	
	jQuery(".down").click(function() {gScroll("-")});
	jQuery(".up").click(function() {gScroll("+")});
	
	var currentId = 0;
	jQuery(".gallery ul li a").click(function() {
		var index = jQuery(this).parent().index();
		jQuery(".gallery ul li").eq(currentId).children().removeClass("current");
		jQuery(this).addClass("current");
		jQuery("#images").find("img").eq(currentId).hide();
		jQuery("#images").find("img").eq(index).show();
		currentId = index;
	});
	
	var offset = 115;
	var current = 0;
	var count = Math.round((jQuery('.gallery_items ul li').size()-6)/2);
	var list = jQuery('.gallery_items ul');
	
	function gScroll(dir) {
		
		if (dir == "+") {current--;}
		else if (dir == "-") {current++;}
	
		if (current == count) jQuery('.down').hide();
		else jQuery('.down').show();
		
		if (current == 0) jQuery('.up').hide();
		else jQuery('.up').show();
		
		//jQuery('.up').html(current);
		
		list.stop();
		//jQuery('.down').html(list.css('marginTop').replace("px",""));
		
		list.animate({
			marginTop: -offset*(current)
		}, 350);
	}
});
function checkform() {
	var error_msg = "";
	if (jQuery("#message").val() == "") {
		error_msg = "заполните поле";
	} else {
		error_msg = "";
	}
	jQuery("#message").parent().find(".error").html(error_msg);
	
	if (jQuery("#email").val() == "") {
		error_msg = "заполните поле";
	} else if (!checkmail(jQuery("#email").val())) {
		error_msg = "неверный e-mail";
	} else {
		error_msg = "";
	}
	jQuery("#email").parent().find(".error").html(error_msg);
	
	var formIsReady = true;
	jQuery(".error").each(function () {
		if (jQuery(this).html() != "") formIsReady = false;
	});
	
	jQuery(".form_error, .form_success").html("");
	if (formIsReady) {
		jQuery(".form_success").html("Ваше сообщение отправлено");
		var msg = 'send_mail.php?email='+jQuery("#email").val()+'&message='+jQuery("#message").val()+'&stype='+jQuery("#stype").val();
		jQuery.get(msg);
		t = setInterval(sec, 1000)
	}
	else {
		jQuery(".form_error").html("(!) Ошибка, сообщение не отправлено.");
	}
}

var t;
var tcount = 0;
function sec() {
	jQuery(".form_success").append(".");
	if (tcount == 4) {
		clearInterval(t);
		tcount = 0;
		if (jQuery('#form').css("display") == "block")
		jQuery('#form').animate({
			height: 'toggle', opacity: 'toggle'
		}, 500, function () {jQuery(".form_error, .form_success").html(""); jQuery('#email, #message').val("")});
	}
	tcount++;
}

function checkmail(email) {
	return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}



