var calcdoor = function () {
	if (! $("#calc, #order").length) return;
	var price;
	var doorWidth;
	var doorHeight;
	var dobory;
	if (/^standard/.test($("input[name=polotno.option]:checked")[0].value)) {
		doorWidth = parseInt ($("select[name=polotno.szs]")[0].value);
		doorHeight = 2000;
	} else if ($('#nonstandardsizes input:text[value=""], #nonstandardsizes input:text.wrong').length) {
		doorWidth = 0;
		doorHeight = 0;
	} else {
		doorWidth = parseInt ($("input[name=polotno.wdt]")[0].value);
		doorHeight = parseInt ($("input[name=polotno.hgt]")[0].value);
	}
	if ($("#dobory:hidden").length) {
		dobory = 0;
	} else if ($('#dobory input:text[value=""], #dobory input:text.wrong').length) {
		dobory = -1;
	} else {
		dobory = Math.ceil (parseInt ($("input[name=korobka.wdt]")[0].value) / 75) - 1;
	}
	if (doorWidth && doorHeight && dobory != -1) {
		if (doorWidth <= 600) price = 23100;
		else if (doorWidth <= 700) price = 24200;
		else if (doorWidth <= 800) price = 25300;
		else price = 26400;
		if (/standard2$/.test($("input[name=polotno.option]:checked")[0].value)) {
			if (doorWidth <= 600) price = 25300;
			else if (doorWidth <= 700) price = 26400;
			else if (doorWidth <= 800) price = 27500;
			else price = 30250;
		}
		switch (doorWidth) {
			case 600:
			case 700:
			case 800:
			case 900:
				break;
			default:
				price *= 1.2;
		}
		if (doorHeight != 2000) {
			if (doorHeight <= 2200) price *= 1.2;
			else if (doorHeight <= 2300) price *= 1.3;
			else if (doorHeight <= 2500) price *= 1.5;
			else price *= 1.8;
		}
		if ($("input[value=double]:checked").length) price *= 2;
		if ($("input[value=arc]:checked").length) price *= 1.6;
		if ($("input[value=75]:checked").length) {
			price += dobory * 2500;
		} else if ($("input[value=mono]:checked").length) {
			price += dobory * 2500 + 3000;
		}

		if ($("input[value=simple]:checked").length) price += 2750;
		else if ($("input[value=cubes]:checked").length) price += 8000;
		else if ($("input[value=kapitel]:checked").length) price += 10000;
		else if ($("input[value=super]:checked").length) price += 12500;
		
		price = Math.floor(price).toString();
		price = price.substr (0, price.length - 3) + "<span>'</span>" + price.substr (price.length - 3);

		$("#resultline p").css("visibility", "visible");
		$("#resultline p span").html(price);
	}
	else {
		$("#resultline p").css("visibility", "hidden");
	}
	if (parseInt ($("select[name=model]").val())
		&& $('#clearsizes input:text[value=""]').length == 0
		&& $('#order input.wrong').length == 0
		&& $('#contacts input:text[value!=""]').length
		) {
		$("#send img").css("opacity", 0.8);
		$("#send p").hide();
		$("#send h3").show();
	} else {
		$("#send img").css("opacity", 0.4);
		$("#send p").show();
		$("#send h3").hide();
	}
}



$(function () {
//  Commons
	$("a[href$=" + location.pathname.substr(location.pathname.lastIndexOf("/") + 1) + "] p").addClass("current");
/*	if (location.pathname === "/index.html" || location.pathname === "/" || /^\/door-\d\d\.php$/.test(location.pathname)) {
		$("body").css("background", "#fff1de url(images/NY_Right.jpg) right top no-repeat");
	}*/
	$("#shop, #gallery, #mail").hover(
		function (event) {
			var src = $(this).attr("src");
			$(this).attr("src", src.replace (".gif", "_.gif"))
		},
		function (event) {
			var src = $(this).attr("src");
			$(this).attr("src", src.replace ("_.gif", ".gif"))
		}
	 );
//  Models list preparation
	for (var i = 54; i >= 10; i--) {
		$("select[name=model]").prepend($('<option value="' + i + '">' + i + '</option>'));
	}
	$("select[name=model]").prepend($('<option value="0">Ваш выбор</option>'));
	$("select[name=model] option:first").attr("selected", "selected");
//  Bands
	$(".prices tr:odd").css("background-color", "#f3e8cc");
	$("select option:odd").css("background-color", "#e9e1c3");
//  Calculator events
	$("input[value^=standard]")
		.click(
			function () {
				$("#standardsizes").show();
				$("#nonstandardsizes").hide();
			}
		 )
	$("input[value=standard]")
		.click()
	$("input[value^=nonstandard]")
		.click(
			function () {
				$("#standardsizes").hide();
				$("#nonstandardsizes").show();
				if ($('#nonstandardsizes input:text[value=""]').length == 2) {
					$("input[name=polotno.wdt]")[0].value = $("select[name=polotno.szs]")[0].value;
					$("input[name=polotno.hgt]")[0].value = 2000;
				}
				calcdoor();
			}
		 )
	$("input[value=arc]")
		.click(
			function () {
				$("#arcgroup").css("display", $(this).attr("checked") ? "block" : "none");
			}
		 )
	$("#arcgroup").hide()
	$("input[value=150]")
		.click(
			function () {
				$("#dobory").hide();
			}
		 )
	$("#dobory").hide();
	$("input[value=75], input[value=mono]")
		.click(
			function () {
				$("#dobory").show();
				if ($('#dobory input:text[value=""]').length) {
					$("input[name=korobka.wdt]")[0].value = 75;
				}
				calcdoor();
			}
		 )
	$("input[name=nal.option]")
		.click(
			function () {
				$("#calc img").hide();
			}
		 )
	$("input[value=simple]")
		.click(
			function () {
				$("#calc img[src*=3511]").show();
			}
		 )
	$("input[value=cubes]")
		.click(
			function () {
				$("#calc img[src*=3676]").show();
			}
		 )
	$("input[value=kapitel]")
		.click(
			function () {
				$("#calc img[src*=2224]").show();
			}
		 )
	$("input[value=super]")
		.click(
			function () {
				$("#calc img[src*=3561]").show();
			}
		 )
//  Digit fields check
	$("#calc input[type=text], #clearsizes input[type=text]")
		.blur(
			function () {
				var errTile = $("#clearerror p");
				switch ($(this).attr("name")) {
					case "polotno.wdt" :
						var pmin = 500;
						var pmax = 1500;
						errTile = $("#nonstandardsizes .error p");
						break;
					case "clearwdt" :
						var pmin = 500;
						var pmax = 1500;
						break;
					case "polotno.hgt" :
						var pmin = 1500;
						var pmax = 2700;
						errTile = $("#nonstandardsizes .error p");
						break;
					case "clearhgt" :
						var pmin = 1500;
						var pmax = 2700;
						break;
					case "clearthc" :
						var pmin = 0;
						var pmax = 1500;
						break;
					default :
						var pmin = 0;
						var pmax = 300;
						errTile = $("#dobory .error p");
				}
				$(this).val($.trim($(this).val()));
				if ($(this).val() == "") {
					errTile.hide();
					return;
				} else if (/\D/.test($(this).val())) {
					$(this).addClass("wrong");
					errTile.show().text("Недопустимые символы")
				} else if (parseInt ($(this).val()) < pmin) {
					$(this).addClass("wrong");
					errTile.show().text("Размер меньше " + pmin)
				} else if (parseInt ($(this).val()) > pmax) {
					$(this).addClass("wrong");
					errTile.show().text("Размер больше " + pmax)
				} else {
					errTile.hide()
				}
				calcdoor();
			}
		 )
		.focus(
			function () {
				if ($(this).hasClass("wrong")) {
					$(this)
						.removeClass("wrong")
						.val("")
				}
			}
		 )
//  Order events
	$("select[name=model]")
		.click(
			 function () {
				 $(this).children(":first").attr("disabled","disabled");
				 $("#thumb:empty").append("<img>");
				 $("#thumb img").attr("src", "thumbs/" + $(this).val() + ".jpg");
			 }
		)
	$("input[name=phone], input[name=email]")
		.blur(
			function () {
				$(this).val($.trim($(this).val()));
				var str = $(this).val();
				var errTile = $("#contacts .error p");
				if (str == "") {
					errTile.hide();
					return;
				} else if ($(this).attr("name") == "phone") {
					str = str.replace(/[- .()]/g, "");
					if (/^(\+7|8)?\d{7,10}$/.test(str)) {
						errTile.hide()
					} else {
						$(this).addClass("wrong");
						errTile.show().text("Недопустимый формат записи")
					}
				} else {
					if (/^\w+@\w+\.\w{2,3}$/.test(str)) {
						errTile.hide()
					} else {
						$(this).addClass("wrong");
						errTile.show().text("Недопустимый формат записи")
					}
				}
				calcdoor();
			}
		 )
		.focus(
			function () {
				$("#contacts p span").hide();
				$(this).removeClass("wrong");
				$(this).prev("p").children("span").show();
			}
		 )
		.focus()
		.blur()

	$("#calc :input, #order :input")
		.change(calcdoor)
		
	$("#send h3")
		.hover(
			function (event) {
				var e = $("#send img");
				var src = e.attr("src");
				e.attr("src", src.replace (".jpg", "_.jpg")).css("opacity", 1);
			},
			function (event) {
				var e = $("#send img");
				var src = e.attr("src");
				e.attr("src", src.replace ("_.jpg", ".jpg")).css("opacity", 0.8);
			}
		)
		.click(
			 function () {
				$.ajax({
						url : "mail.php",
						type : "post",
						data : $("form").serialize(),
						success : function (data) {
							$("#sent").show();
							if (data == "0") {
								$("#sent p")
									.text("В процессе отправки вашего сообщения возникли проблемы.")
									.addClass("red")
							}
						}
				 })
				$("#order").hide();
				$(this).stopPropagation();
			 }
		)
});

$(window).load(calcdoor);
