﻿function MM_preloadImages()
{ //v3.0
	var d = document;

	if (d.images)
	{
		if(!d.MM_p) d.MM_p = new Array();

		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		
		for(i = 0; i < a.length; i++)
			if(a[i].indexOf("#") != 0)
			{
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
	}
}

function MM_swapImgRestore()
{ //v3.0
	var i, x, a = document.MM_sr;

	for(i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}

function MM_findObj(n, d)
{ //v4.01
	var p, i, x;

	if(!d) d = document;

	if((p = n.indexOf("?")) > 0 && parent.frames.length)
	{
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}

	if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage()
{ //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

var globalVars = new Array();
globalVars["b_firstAnimationTime"] = 4000; //Время (мс) движения баннеров после загрузки главной странички
globalVars["b_step"] = 220;   //Шаг перемещения баннера. Это значение равно: стандартная ширина баннера + 20!!!
globalVars["b_ContainerWidth"] = 0;
globalVars["b_InnerWidth"] = 0;
globalVars["b_steps"] = 0;
globalVars["b_varyfirstAnimation"] = true;
globalVars["b_firstAnimation"] = true;
globalVars["b_bannerMoveing"] = false;
globalVars["b_bannerVertMoveing"] = false;
globalVars["c_step"] = 143;   //Шаг перемещения автомобилей по горизонтали. 
globalVars["x_step"] = 145;   //Шаг перемещения автомобилей при отображении всего модельного ряда. 
globalVars["v_step"] = 140;   //Шаг перемещения баннера по по вертикали. Это значение равно: стандартная высота баннера + 20!!!
globalVars["c_ContainerWidth"] = 0;
globalVars["c_InnerWidth"] = 0;
globalVars["c_steps"] = 0;
globalVars["x_steps_1"] = 0;
globalVars["x_steps_2"] = 0;
globalVars["x_steps_3"] = 0;
globalVars["c_carsMoveing"] = false;
globalVars["x_carsMoveing"] = false;
globalVars["c_carsSubmenu1"] = 0;
globalVars["c_carsSubmenu2"] = 0;
globalVars["x_rangeActive"] = 0;

/* Функция перехода по указанному URL */
function url(addr)
{
	document.location = addr;
}

$(document).ready(function() {
	$('a[rel^="prettyPhoto"]').prettyPhoto({ theme: 'facebook' });

	globalVars["isie"] = navigator.appName.indexOf("icrosoft") > 0;
	globalVars["b_firstAnimation"] = false;
	globalVars["b_varyfirstAnimation"] = false;
	globalVars["b_ContainerWidth"] = $("#banner_container").width();
	globalVars["b_InnerWidth"] = $("#banner_inner").width();

	//Установка прозрачности блока с формой Онлайн-консультации в ноль
	if(globalVars["isie"]) {
		globalVars["feedbackHeight"] = $("#b_feedbackForm").height();
		globalVars["feedbackWidth"] = $("#b_feedbackForm").width();
		globalVars["feedbackMLeft"] = parseInt($("#b_feedbackForm").css("margin-left"));
	}
	else
		$("#b_feedbackForm").css("opacity", "0");

	$('#b_feedbackContainer div').click(function() {
		if(globalVars["isie"]) {
			$("#b_feedbackForm").css("width", "0px");
			$("#b_feedbackForm").css("margin-left", (globalVars["feedbackMLeft"] + globalVars["feedbackWidth"]) + "px");
			$("#b_feedbackForm").show();
			$("#b_feedbackForm").animate({ width: globalVars["feedbackWidth"] + "px", marginLeft: globalVars["feedbackMLeft"] + "px" }, 300);
		}
		else
			$("#b_feedbackForm").show().animate({ opacity: 1 }, 500);

		$('#b_feedbackForm input[name="name"]').focus();
		$('#b_feedbackForm form').attr('action', '/feedback.html');
	});

	$('#b_feedback_inner')
		.mouseover(function() { $(this).css('background-position', '0px -73px'); })
		.mouseout(function() { $(this).css('background-position', '0px 0px'); });
	$('#b_feedback')
		.mouseover(function() { $(this).css('background-position', '0px -107px'); })
		.mouseout(function() { $(this).css('background-position', '0px 0px'); });

	$('#b_feedbackOnline_inner').click(function() {
		$("#b_feedbackForm").show();
		$("#b_feedbackForm").animate({ opacity: 1 }, 500);
	});

	$('#b_buttonClose').click(function() {
		if(globalVars["isie"])
			$("#b_feedbackForm").animate({ width: "0px", marginLeft: (globalVars["feedbackMLeft"] + globalVars["feedbackWidth"]) + "px" }, 300, function() { $("#b_feedbackForm").hide(); });
		else
			$("#b_feedbackForm").animate({ opacity: 0 }, 500, function() { $("#b_feedbackForm").hide(); });
	});

	//Анимация баннеров на главной страничке после загрузки 
	/*
	if(document.getElementById("banner_container"))
	{
	$('#banner_inner').animate({ marginLeft: "0px" }, 1500, 
	function() 
	{ 
	globalVars["b_varyfirstAnimation"]=false;
	globalVars["b_ContainerWidth"]=$("#banner_container").width();
	globalVars["b_InnerWidth"]=$("#banner_inner").width();
	pos=-(globalVars["b_InnerWidth"]-globalVars["b_ContainerWidth"]);
	globalVars["b_steps"]=-1*parseInt((globalVars["b_InnerWidth"]-globalVars["b_ContainerWidth"])/globalVars["b_step"]);
								
	$('#banner_inner').animate({ marginLeft: pos+"px" }, globalVars["b_firstAnimationTime"], 
	function() 
	{ 
	globalVars["b_firstAnimation"]=false;				
	}
	);
	}
	);
	}
	*/

	//Определяем начальные значения переменных для корректного отображения модельного ряда
	globalVars["c_ContainerWidth"] = $("#cars_container").width();
	globalVars["c_InnerWidth"] = $("#cars_inner").width();

	//Растягиваем вертикальную полосу прокрутки баннеров, если страничка высокая
	if(document.getElementById("i_bannerContainer")) {
		outH = $(".b_text").height();
		innerH = $("#i_bannerInner").height();
		additionH = $("#banner_top").height() + $("#banner_bottom").height();
		visibleAmount = Math.floor((outH - additionH - 20) / globalVars["v_step"]);
		fitToH = visibleAmount * globalVars["v_step"];
		fitToH = (fitToH > innerH) ? innerH : fitToH;
		if(fitToH > globalVars["v_step"]) {
			$("#i_bannerContainer").css("height", fitToH + "px");
		}
	}

	$('.subm li').click(function() {
		window.location = $(this.childNodes[0]).attr('href');
		return false;
	});

	$('.b_modelLink a').click(function() {
		$(".x_range").show(); //$(".x_range").animate({ opacity: 1 }, 500);

		$('.x_row').each(function() {
			if($('.x_body', this).width() >= $('.x_inner > table', this).width()) {
				$('.x_left', this).css("visibility", "hidden");
				$('.x_right', this).css("visibility", "hidden");
			}
		});

		return false;
	});

	$('.x_close a').click(function() {
		$(".x_range").hide();
		//$(".x_range").animate({ opacity: 0 }, 500, function(){ $(".x_range").hide(); });
		return false;
	});

	$('.x_range').click(function() {
		if(!globalVars["x_rangeActive"]) {
			$(".x_range").hide();
			//$(".x_range").animate({ opacity: 0 }, 500, function(){ $(".x_range").hide(); });
		}
	});

	$('.x_photo').click(function() {
		window.location = $(this.parentNode.childNodes[1].childNodes[0]).attr('href');
	});

	//Функция вызывается при наведении на автомобили

	$('div.cars_container').mouseover(function() {
		$('#cars_container').css("height", "352px");
		$('.cars_top', this).css("border-left-width", "1px");
		$('.cars_top', this).css("border-right-width", "1px");
		$('.cars_top', this).css("padding-left", "0px");
		$('.cars_top', this).css("padding-right", "0px");
		$('.x_photo', this).css("background-position", "0px -50px");
		$('.cars_name', this).attr("class", "cars_name_active");
		$('.cars_submenu', this).show();
	}).mouseout(function() {
		$('#cars_container').css("height", "159px");
		$('.cars_top', this).css("border-left-width", "0px");
		$('.cars_top', this).css("border-right-width", "0px");
		$('.cars_top', this).css("padding-left", "1px");
		$('.cars_top', this).css("padding-right", "1px");
		$('.x_photo', this).css("background-position", "0px 0px");
		$('.cars_name_active', this).attr("class", "cars_name");
		$('.cars_submenu', this).hide();
	});

	$('.cars_subm li').click(function() {
		document.location = $(this.childNodes[0]).attr('href');
	});

	$('#cars_left').click(function() {
		carsShift('left');
	}).mouseover(function() {
		//hlDiv('cars_left',-63);
		$(this).css('background-position', '0px -63px');
	}).mouseout(function() {
		//hlDiv('cars_left',0);
		$(this).css('background-position', '0px 0px');
	});

	$('#cars_right').click(function() {
		carsShift('right');
	}).mouseover(function() {
		hlDiv('cars_right', -63)
	}).mouseout(function() {
		hlDiv('cars_right', 0);
	});

	$('.x_row').mouseover(function(){
		globalVars["x_rangeActive"] = 1;
	});

	$('.x_row').mouseout(function(){
		globalVars["x_rangeActive"] = 0;
	});

	$('.x_left').click(function(){
		x_carsShift(this.parentNode.parentNode, 'left');
	}).mouseover(function() {
		$(this).css('background-position', '0px -63px');
	}).mouseout(function() {
		$(this).css('background-position', '0px 0px');
	});

	$('.x_right').click(function(){
		x_carsShift(this.parentNode.parentNode, 'right');
	}).mouseover(function(){
		$(this).css('background-position', '0px -63px');
	}).mouseout(function(){
		$(this).css('background-position', '0px 0px');
	});

	$('.x_wrapper').mouseover(function(){
		if(this.timer)
		{
			window.clearTimeout(this.timer);
			this.timer = null;
		}

		$(this).css('z-index', '500');
		$('.x_container', this).css("border-left-width", "1px");
		$('.x_container', this).css("border-right-width", "1px");
		$('.x_container', this).css("padding-left", "0px");
		$('.x_container', this).css("padding-right", "0px");

		$('.x_name', this).attr("class", "x_name_active");
		$('.x_photo', this).css("background-position", "0px -50px");

		$('.x_submenu', this).show();
	}).mouseout(function(){
		var wr = this;

		this.timer = window.setTimeout(function(){
			$('#cars_container').css("height", "159px");
			$('.x_container', wr).css("border-left-width", "0px");
			$('.x_container', wr).css("border-right-width", "0px");
			$('.x_container', wr).css("padding-left", "1px");
			$('.x_container', wr).css("padding-right", "1px");

			$('.x_photo', wr).css("background-position", "0px 0px");
			$('.x_name_active', wr).attr("class", "x_name");

			$('.x_submenu', wr).hide();
		}, 10);
	});
});

//Функция, срабатывающая при наведении на DIV-ы
function hlDiv(id, y)
{
	$("#" + id).css("background-position", "0px " + y + "px");
}

//Функция вызывающая метод формы SUBMIT
function submitForm(id)
{
	$('#' + id).submit();
}

//Функция, перемещающая баннеры
function bannersShift(direction)
{
	if(!globalVars["b_bannerMoveing"] && !globalVars["b_firstAnimation"])
	{
		globalVars["b_firstAnimation"] = false;
		direction = direction == 'left' ? 1 : -1;
		pos = (globalVars["b_steps"] + direction) * globalVars["b_step"];

		if(pos <= 0)
		{
			$("#banner_left").animate({ opacity: 1 }, 500);

			if(pos >= -1 * (globalVars["b_InnerWidth"] - globalVars["b_ContainerWidth"]))
			{
				$("#banner_right").animate({ opacity: 1 }, 500);
				globalVars["b_steps"] += direction;
				$('#banner_inner').stop();
				globalVars["b_bannerMoveing"] = true;
				$('#banner_inner').animate({ marginLeft: pos + "px" }, 700, function() { globalVars["b_bannerMoveing"] = false; });
			}
			else
				$("#banner_right").animate({ opacity: 0 }, 500);
		}
		else
			$("#banner_left").animate({ opacity: 0 }, 500);
	}
}

function shiftBannersVert(direction)
{
	if(!globalVars["b_bannerVertMoveing"])
	{
		step = globalVars["v_step"];
		direction = direction == 'top' ? 1 : -1;
		mTop = $("#i_bannerInner").css("margin-top");
		if(mTop == "auto") { mTop = "0px"; }
		mTop = mTop.substr(0, mTop.indexOf("px"));
		pos = parseInt(mTop) + parseInt(direction * step);

		if(pos <= 0)
		{
			$("#banner_top").animate({ opacity: 1 }, 500);

			if(pos >= -1 * ($("#i_bannerInner").height() - $("#i_bannerContainer").height()))
			{
				$("#banner_bottom").animate({ opacity: 1 }, 500);
				$('#i_bannerInner').stop();
				globalVars["b_bannerVertMoveing"] = true;
				$('#i_bannerInner').animate({ marginTop: pos + "px" }, 700, function() { globalVars["b_bannerVertMoveing"] = false; });
			}
			else
				$("#banner_bottom").animate({ opacity: 0 }, 500);
		}
		else
			$("#banner_top").animate({ opacity: 0 }, 500);
	}
}

//Срабатывает при отведении мыши от баннеров
function bannersKeepMoveing()
{
	/*
	if(globalVars["b_firstAnimation"])
	{
	pos=-(globalVars["b_InnerWidth"]-globalVars["b_ContainerWidth"]);
	time=parseInt(globalVars["b_firstAnimationTime"]*(pos-parseInt($('#banner_inner').css("margin-left")))/pos);
	$('#banner_inner').animate({ marginLeft: pos+"px" }, time, function() { globalVars["b_firstAnimation"]=false; });	
	}
	*/
}

//Срабатывает при наведении мыши на баннера
function bannersStopMoveing()
{
	/*
	if(!globalVars["b_varyfirstAnimation"])
	{
	if(globalVars["b_firstAnimation"])
	{
	$('#banner_inner').stop();
	}
	}
	*/
}

//Функция, перемещающая машинки
function carsShift(direction)
{
	globalVars["c_ContainerWidth"] = $("#cars_container").width();
	globalVars["c_InnerWidth"] = $("#cars_inner").width();

	if(globalVars["c_carsMoveing"]) return;

	direction = direction == 'left' ? 1 : -1;
	pos = (globalVars["c_steps"] + direction) * globalVars["c_step"];

	if(pos <= 0)
	{
		$('#cars_left').animate({ opacity: 1 }, 500);

		if(pos >= (-1 * (globalVars["c_InnerWidth"] - globalVars["c_ContainerWidth"]) - 20))
		{
			$("#cars_right").animate({ opacity: 1 }, 500);
			globalVars["c_steps"] += direction;
			$('#cars_inner').stop();
			globalVars["c_carsMoveing"] = true;
			$('#cars_inner').animate({ marginLeft: pos + "px" }, 700, function() { globalVars["c_carsMoveing"] = false; });
		}
		else
			$("#cars_right").animate({ opacity: 0 }, 500);
	}
	else
		$("#cars_left").animate({ opacity: 0 }, 500);
}

//Функция, перемещающая машинки
function x_carsShift(row, direction)
{
	globalVars["x_ContainerWidth"] = $('.x_body', row).width();
	row.xInnerWidth = $('.x_inner > table', row).width();
	row.xSteps = row.xSteps ? row.xSteps : 0;

	if(!globalVars["x_carsMoveing"])
	{
		direction = direction == 'left' ? 1 : -1;
		pos = (row.xSteps + direction) * globalVars["x_step"];

		if(pos <= 0)
		{
			$('.x_left', row).animate({ opacity: 1 }, 500);

			if(pos >= -1 * (row.xInnerWidth - globalVars["x_ContainerWidth"]))
			{
				$('.x_right', row).animate({ opacity: 1 }, 500);
				row.xSteps += direction;
				$('.x_inner > table', row).stop();
				globalVars["x_carsMoveing"] = true;
				$('.x_inner > table', row).animate({ marginLeft: pos + "px" }, 700, function() { globalVars["x_carsMoveing"] = false; });
			}
			else
				$('.x_right', row).animate({ opacity: 0 }, 500);
		}
		else
			$('.x_left', row).animate({ opacity: 0 }, 500);
	}
}

function x_rowAction(state)
{
	globalVars["x_rangeActive"] = state;
}

