
$(function(){
	var hash = window.location.hash;
	var url = window.location.href.split("/");
	$(".tabbed").tabs().bind("tabsselect", function(event, ui){
		$(".tabbed ul.titles li .corner").remove();
		setTimeout(tabsPutCorner, 10);
	});
	$(".tabbed-too").tabs();
	if (hash.length > 1) {
		document.getElementById(hash.substr(1)).scrollIntoView(true);
	}

	if (url[url.length-1].substr(0,"services.php".length) == "services.php") {
		if (hash.length > 1) {
			$(".menu a[href=\"/services.php"+hash+"\"]").addClass("active");
		}
		$(".submenu a").click(function() {
			$(".menu .active").removeClass("active");
			var hash = $(this).addClass("active").attr("href").split("#")[1];
			$(".tabbed .ui-tabs-nav a[href=\"#"+hash+"\"]").click();
			document.getElementById(hash).scrollIntoView(true);
		});
	}

	if (typeof showMap == "function") {
		showMap();
	}

	if (typeof $.jcarousel == "function" && !($.browser.msie && $.browser.version=="6.0") ) {
		$("#scroller").jcarousel({wrap: 'circular', scroll: 1});
	}

	$(".fold").click(function() {
		$(this).parent().next(".foldable").slideToggle();
		return false;
	});

	$("#button").click(function(){
		try {
			var name = $("#name").val();
			if (name.length < 3) {
				throw "имя";
			}
			var email = $("#email").val();
			if (email.length < 5) {
				throw "телефон или e-mail";
			}
			var message = $("#message").val();
			if (message.length < 5) {
				throw "запрос";
			}
			$.post('/feedback.php', {name:name,email:email,message:message}, function(data) {
				if (data == 'OK') {
					alert("Ваше сообщение отправлено");
				} else {
					alert("Произошла ошибка. Попробуйте повторить попытку позже.");
				}
			});
		} catch(e) {
			alert("Пожалуйста, заполните "+e);
		}
		return false;
	});

	tabsPutCorner();
});


function tabsPutCorner() {
	$(".tabbed ul.titles li.ui-tabs-selected").append('<span class="corner"></span>');
	$(".tabbed .right").each(function(){
		$(this).height("auto");
		var $left = $(this).siblings(".left");
		if ($(this).height() < $left.height()) {
			$(this).height($left.height()-30);
		}
	});
}

