$(document).ready(function() {
	var thelink = "http://volkof.ru/web/uslugi-i-ceni/"; //ссылка куда ведет анимированный блок
	
	$("#header-inside ul li a").mouseover(function() {
		var curLeft = $(this).offset().left - $("#header-inside").offset().left;
		if($("#bg-active").hasClass("current")) {
			$("#bg-active").animate( {left:curLeft, width:$(this).width()+10} , 600);					
		} else {			
			$("#bg-active").css("left",curLeft).css("width",$(this).width()+10).fadeIn(300);
		}			
	});
	$("#header-inside ul").hover(function() {		
		$("#bg-active").addClass("current");
	}, function() {
		$("#bg-active").removeClass("current");
		$("#bg-active").fadeOut(300);
	});
	$("#main-top-right ul").click(function() {
		window.location = thelink;
	});
	
	$("div.marker").css("left",$("#main-top-right").offset().left+15+"px");
	$("div.marker").css("top",$("#main-top-right").offset().top + 129 +"px");
	goNext($("#main-top-right ul li:first"));	
	$("div.marker").show();
	
	$(window).resize(function() {
		$("div.marker").css("left",$("#main-top-right").offset().left+15+"px");
	});
	
	function goNext(currentLi) {	
		currentLi.animate( { color:'#6aa2fb' }, 900);
		var currentTop = currentLi.offset().top;
		if(currentLi.index()==0) {
			currentTop = $("#main-top-right").offset().top + 129;
		}
		if(currentLi.index()<4) {
			var nextLi = currentLi.next();
		} else {
			var nextLi = $("#main-top-right ul li:first");
		}
		$("div.marker").animate( { top:currentTop }, 900, function() {
			thetimeout = setTimeout(function() {
				if(currentLi.index()==4) {				
					trembleArrow(7);
					$("div.marker").fadeOut();
					currentLi.animate( { color:'#b9f04c' }, 900);
					return false;				
				}
				currentLi.animate( { color:'#b9f04c' }, 900);
				goNext(nextLi);
			} , 1800);
		});
	}
	
	function trembleArrow(count) {		
		if(count>0) {
			if($("#mtr-arrow").css("margin-left")=="0px") {
				var finishPos = 5;
			} else {
				var finishPos = 0;
			}
				$("#mtr-arrow").animate( {marginLeft:finishPos}, 300, function() {
					trembleArrow(count-1);
				});
		} else {
			thetimeout = setTimeout(function() {
				$("div.marker").fadeIn();		
				$("div.marker").css("left",$("#main-top-right").offset().left+15+"px");
				$("div.marker").css("top",$("#main-top-right ul li:first").offset().top+"px");
				goNext($("#main-top-right ul li:first"));
				return false;
			}, 1800);
		}
	}
});

