$(function() {

	$("#top_menu ul li").hover(function() {
		var d = $(this).find("div." + $(this).attr("class"));
		var level = parseInt($(this).attr("class").substr(6));
		if (level > 1) {
			d.css("left", $(this).outerWidth());
		}

		d.show(300);
	}, function() {
		$(this).find("div").hide(300);
		// $(this).find("div").slideUp();
	});

	$("a.photo").fancybox({
		'autoScale' : false,
		'titlePosition' : 'inside',
		'transitionIn' : 'none',
		'transitionOut' : 'none'
	});

	$("input[name='telephon']").focusin(function() {
		if ($(this).val() == "Телефон") {
			$(this).removeClass("gray").removeClass("error");
			$(this).val('');
		}
		return false;
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass("gray").addClass("error");
			$(this).val("Телефон");
		}
		return false;
	});

	$("input[name='email']").focusin(function() {
		if ($(this).val() == "Электронная почта") {
			$(this).removeClass("gray").removeClass("error");
			$(this).val('');
		}
		return false;
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass("gray").addClass("error");
			$(this).val("Электронная почта");
		}
		return false;
	});

	$("input[name='name']").focusin(function() {
		if ($(this).val() == "Контактное лицо") {
			$(this).removeClass("gray");
			$(this).val('');
		}
		return false;
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass("gray");
			$(this).val("Контактное лицо");
		}
		return false;
	});

	$("textarea[name='notes']").focusin(function() {
		if ($(this).val() == "Примечания") {
			$(this).removeClass("gray");
			$(this).val('');
		}
		return false;
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).addClass("gray");
			$(this).val("Примечания");
		}
		return false;
	});

	$("input[name='telephon']").keyfilter(/[0-9 \(\)\-]/i);
	$("button#send_order")
			.click(
					function() {
						var t = $("input[name='telephon']");
						var e = $("input[name='email']");
						var n = $("input[name='name']");
						var stat = true;
						if (typeof n.val() == 'undefined' || n.val() == ''
								|| n.val() == 'Ваше имя') {
							n.addClass("error");
							stat = false;
						} else {
							n.removeClass("error");
						}
						if (typeof t.val() == 'undefined' || t.val() == ''
								|| t.val() == 'Телефон') {
							t.addClass("error");
							stat = false;
						} else {
							t.removeClass("error");
						}
						if (typeof e.val() == 'undefined'
								|| e.val() == ''
								|| !(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
										.test(e.val(), ''))) {
							e.addClass("error");
							stat = false;
						} else {
							e.removeClass("error");
						}
						if (stat) {
							$("form#form_order").submit();
							return true;
						}
						return false;
					});

});

