var left = 0;
var right = 0;
var activeBrand;

$(document).ready(function(){
	/* ============================================================
					Лого
	============================================================ */
	$(".main .logo").flashembed("/_common/flash/logo.swf");
		

	/* ============================================================
					Машинки на главной
	============================================================ */
	//Дизейблим кнопки
	checkLeftShown();
	checkRightShown();
	
	$(".left-arr").click(function(){
		moveCars("right");
		return false;
	})
	
	$(".right-arr").click(function(){
		moveCars("left");
		return false;
	})
	
	$(".models dl li a").click(function(){
		if(!($(this).parent("li").hasClass("active")))	{
			activeBrand = $(this).attr("class");
			$(".models-wrapper ul").each(function(){
				if($(this).hasClass(activeBrand))
					$(this).removeClass("no-display").addClass("display")
				else
					$(this).removeClass("display").addClass("no-display")
			})
			$(".models li a").each(function(){
				if($(this).hasClass(activeBrand))
					$(this).parent("li").addClass("active")
				else
					$(this).parent("li").removeClass("active")
			})
			$(".brands li a").each(function(){
				if($(this).hasClass(activeBrand))
					$(this).parent("li").addClass("active")
				else
					$(this).parent("li").removeClass("active")
			})
			checkLeftShown();
			checkRightShown();
		}
		return false;
	})

	$(".brands li a").click(function(){
		if(!($(this).parent("li").hasClass("active")))	{
			activeBrand = $(this).attr("class");
			$(".models-wrapper ul").each(function(){
				if($(this).hasClass(activeBrand))
					$(this).removeClass("no-display").addClass("display")
				else
					$(this).removeClass("display").addClass("no-display")
			})
			$(".models li a").each(function(){
				if($(this).hasClass(activeBrand))
					$(this).parent("li").addClass("active")
				else
					$(this).parent("li").removeClass("active")
			})
			$(".brands li a").each(function(){
				if($(this).hasClass(activeBrand))
					$(this).parent("li").addClass("active")
				else
					$(this).parent("li").removeClass("active")
			})
			checkLeftShown();
			checkRightShown();
		}
		return false;
	})
});

function moveCars(direction)	{
	if((direction == "right" && checkLeftShown()) || (direction == "left" && checkRightShown()))	{
		width = $(".models-wrapper .display li").outerWidth();
		
		//Создаем контейнер с новым элементом списка
		if(direction == "right")	{
			$(".models-wrapper ul.display").prepend("<div class='leftMove temp-container'>"+$(".models-wrapper .display li").eq(left-1).html()+"</div>"); 
			$(".leftMove").css({left: -width+"px", width: width});
		} else	{
			$(".models-wrapper ul.display").append("<div class='rightMove temp-container'>"+$(".models-wrapper .display li").eq(left+5).html()+"</div>"); 
			$(".rightMove").css({right: -width+"px", width: width});
		}
		
		//Двигаем список и контейнер на ширину элемента списка
		$(".models-wrapper ul.display").animate({
			"margin-left": ((direction == "right") ? width : -width)
		}, 500);
		if(direction == "right")
			$(".models-wrapper .display .leftMove").animate({left: '0px'},500,function(){
				//Переназначаем классы в списке и удаляем контейнер
				$(".models-wrapper ul.display").css("margin-left","0px");
				$(".models-wrapper .display li").eq(left-1).removeClass("no-display");
				$(".models-wrapper .display li").eq(left+4).addClass("no-display");
				$(".leftMove").remove();
				checkLeftShown();
				checkRightShown();
			})
		else
			$(".models-wrapper .display .rightMove").animate({right: '0px'},500,function(){
				//Переназначаем классы в списке и удаляем контейнер
				$(".models-wrapper ul.display").css("margin-left","0px");
				$(".models-wrapper .display li").eq(right+1).removeClass("no-display");
				$(".models-wrapper .display li").eq(right-4).addClass("no-display");
				$(".rightMove").remove();
				checkLeftShown();
				checkRightShown();
			})
	}
}

function checkLeftShown()	{
	left = $(".models-wrapper .display li").not(".no-display").first().index();
	right = left+4;
	
	if (left>0)	{
		$(".left-arr").removeClass("disabled");
		return true;
	} else	{
		$(".left-arr").addClass("disabled");
		return false;
	}
}

function checkRightShown()	{
	right = $(".models-wrapper .display li").not(".no-display").last().index();
	left = right-4;
	
	if (right<$(".models-wrapper .display li").last().index())	{
		$(".right-arr").removeClass("disabled");
		return true;
	} else	{
		$(".right-arr").addClass("disabled");
		return false;
	}
}