$(function() {
	/*Для блока с вкладками*/
	var flowtabs_timeout = 1000;
	$("#flowtabs a").click(function() {
		var is_current = $(this).parent().hasClass("current")

		if (!is_current) {
			$(this).parent().siblings().removeClass("current");
			$(this).parent().addClass("current");
		}
		
		var index = $(this).parent().prevAll().length;
		var width = $("#flowtabs").width() - 40;

		if (!is_current || flowtabs_timeout === 0) {
			$("#flowpanes div.items div.wrap").animate({ left: "-" + width * index + "px"}, flowtabs_timeout);
		}
		if (flowtabs_timeout === 0) flowtabs_timeout = 1000;
	})	

	if (window.location.hash) {
		var flowtabs_timeout = 0;
		$("#flowtabs a[href=" + window.location.hash + "]").click();
	} else {
		$("#flowtabs a:first").parent().addClass("current");
	}

	/*Верхнее меню*/
	$('.menu a:not(.current)')
		.css( {backgroundPosition: "-20px 34px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-20px 94px)"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(40px 35px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-20px 34px"})
			}})
		})

	/*Разбивка слов для стикеров*/
	$('.break').each(function() {
		var input = $(this).text();
		var words = input.split(" ");

		var lines = [];
		var line = [words.shift()];
		while (words.length) {
			var current_word = words.shift();

			var line_length = -1;
			for (var i in line) {
				line_length += line[i].length + 1;
			}

			if (line_length + current_word.length > 11) {
				lines.push(line.join(" "));
				line = []
			}

			line.push(current_word);
		}
		if (line.length) lines.push(line.join(" "));

		for (var i in lines) {
			lines[i] = "<span>" + lines[i] + "</span> ";
		}
		$(this).html(lines.join(""));
	});

	$(window).resize(function() {
		$(".cross_list > div").each(function() {
			var ha = $(".about", $(this)).height();
			var hn = $(".break", $(this)).height();
			var mt = (ha - hn) / 2;
			$(".name", $(this)).css("margin-top", mt + "px");
		})

		var width = $("#flowtabs").width() - 40;
		$("#flowpanes div.items div.wrap div").css("width", width - 40 + "px");

		flowtabs_timeout = 0;
		$("#flowtabs li.current a").click();

		$("#flowpanes").css("height", $("#flowpanes div.items div.wrap").height() + 20 + "px");
	}).resize();

	$(".request").click(function() {
		$("#request_from form").show();
		$("#request_from_result").empty();
		return false;
	}).overlay({
    	expose: {
    		color: '#333',
    		loadSpeed: 200,
    		opacity: 0.9
    	}
	});

	// Not robot
	$("form").append("<input type='hidden' name='js_enabled' value='1'>");
	
	// Request from
	$("#request_from form").append("<input type='hidden' name='ajax' value='1'>");

	$("#request_from form").ajaxForm({
		success : function(responseText, statusText) {
			$("#request_from_result").html(responseText);
			if ($("#request_from_result div.ok").length) $("#request_from form").hide();
		}
	});

	$("#request_from .close").mouseover(function(){
		$(this).css('background-image', 'url(/img/overlay/close_over.png)' );
	}).mouseout(function(){
		$(this).css('background-image', 'url(/img/overlay/close.png)' );
	});
	
	// Faq
	$("#faq a").click(function() {
		$(this).next().toggle();
		return false;
	});
	if (window.location.hash) $("#faq a[href=" + window.location.hash + "]").click();
});
