$(document).ready(function(){
	if($('h1').next().find('li.selected').size()){
	$('h1').text($('h1').next().find('li.selected').text());
	}
	
	$("div.contentBlock").hide();
	var anchor = location.hash;
	if(anchor == "#contentBlock1" || anchor == "#contentBlock2" || anchor == "#contentBlock3") {
		window.scrollTo(0,0);
		$(anchor).show();
		$("ul.leftMenu li").removeClass("selected");
		$("ul.leftMenu li a[rel='"+ anchor +"']").parent().parent().addClass("selected");
		$("ul.leftMenu li a[href='"+ anchor +"']").parent().parent().addClass("selected");
	} else {
		$("div.contentBlock:first").show();
	}

	$(".consult a").click(function(){
		$("div.orderForm").show();
		return false;
	});
	
	$("a.selection").click(function(){
		$("div.orderForm").show();
		return false;
	});
	
	$(".closeLink").click(function(){
		$("label[for='name']").css("color", "#000000");
		$("label[for='phone']").css("color", "#000000");
		$("#name").val("");
		$("#phone").val("");
		$("div.orderBut input").attr("disabled", "disabled");
		$("div.orderForm").hide();
		return false;
	});
	
	$("div.orderButton").click(function(){
		$("div.serviceForm").show();
		return false;
	});
	
	$(".closeServiceLink").click(function(){
		$("label[for='nameBox']").css("color", "#000000");
		$("label[for='phoneBox']").css("color", "#000000");
		$("label[for='serviceText']").css("color", "#000000");
		$("#nameBox").val("");
		$("#phoneBox").val("");
		$("#serviceText").val("");
		$("div.serviceButton input").attr("disabled", "disabled");
		$("div.serviceForm").hide();
		return false;
	});
});
	
	
	function order_func() {
		var check = true;
		if(trim($("#name").val()).length <= 0) {
			check = false;
			$("label[for='name']").css("color", "#bc0000");
			$("div.orderBut input").attr("disabled", "disabled");
		} 
		if(trim($("#phone").val()).length <= 0) {
			check = false;
			$("label[for='phone']").css("color", "#bc0000");
			$("div.orderBut input").attr("disabled", "disabled");
		}
		if (check) {
			$("label[for='name']").css("color", "#000000");
			$("label[for='phone']").css("color", "#000000");
			$("div.orderBut input").removeAttr("disabled");
		}
	}
	
	
	function service_func() {
		var serviceCheck = true;
		if(trim($("#nameBox").val()).length <= 0) {
			serviceCheck = false;
			$("label[for='nameBox']").css("color", "#bc0000");
			$("div.serviceButton input").attr("disabled", "disabled");
		} 
		if(trim($("#phoneBox").val()).length <= 0) {
			serviceCheck = false;
			$("label[for='phoneBox']").css("color", "#bc0000");
			$("div.serviceButton input").attr("disabled", "disabled");
		}
		if(trim($("#serviceText").val()).length <= 0) { 
			serviceCheck = false;
			$("label[for='serviceText']").css("color", "#bc0000");
			$("div.serviceButton input").attr("disabled", "disabled");
		}
		if(serviceCheck) {
			$("label[for='nameBox']").css("color", "#000000");
			$("label[for='phoneBox']").css("color", "#000000");
			$("label[for='serviceText']").css("color", "#000000");
			$("div.serviceButton input").removeAttr("disabled");
		}
	}
	
	
	$(document).ready(function(){
		$("input").focus(function() {
			$("label").css("color", "#000000");
		});
		$("div.orderForm input[type='text']").blur(function() {
			order_func();
		});
		
		$("#orderForm").submit(function() {
			var array = $(this).serialize();
			var radio = $("input[name='radio']:checked").val();
			$.post('/send_order.php', {
				name : $("#name").val(), 
				phone : $("#phone").val(),
				time : $("input[name='radio']:checked").val()
			}, function(data) {
				$(".orderContent").empty();
				$(".orderContent").html(data);
				return false;
			});
			return false;
		});
		
		
		$("div.serviceForm input[type='text']").blur(function() {
			service_func();
		});
		$("div.serviceForm textarea#serviceText").blur(function() {
			service_func();
		});
		$("#serviceForm").submit(function() {
			var arr = $(this).serialize();
			$.post('/send_service.php', {
				name : $("#nameBox").val(), 
				phone : $("#phoneBox").val(),
				comment : $("#serviceText").val(),
				wood : $("#windowType1").attr("checked") == true?$("#windowType1").val():"", 
				plastic : $("#windowType2").attr("checked") == true?$("#windowType2").val():"", 
				time : $("input[name='radioBox']:checked").val()
			}, function(data) {
				$(".serviceContent").empty();
				$(".serviceContent").html(data);
				return false;
			});
			return false;
		});
	});	
			
	
$(document).ready(function(){
	$("a.lightbox").lightBox();
});

function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length; --i >= 0;) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
