$(document).ready(function() {
	/**
	* Раскрывающиеся списки
	*/
	var expandables = $('#content .expandable');
	expandables.each(function() {
		var self = $(this);
		var sw = self.find('.switcher:first');
		sw.click(function() {
			var parent = $(this.parentNode);
			var container = parent.hasClass('expandable') ? parent : parent.parent();
			if (container.hasClass('collapsed')) {
				container.removeClass('collapsed');
				container.addClass('expanded');
				var content = container.find('.content:first');
				content.css('visibility', 'hidden');
				content.slideDown('slow');
				content.css('visibility', 'visible');
			} else if (container.hasClass('expanded')) {
				container.find('.content:first').slideUp('slow', function() {
					container.removeClass('expanded');
					container.addClass('collapsed');
				});
			}
			return false;
		});
	});
	/**
	* Всплывающие подменюшки
	*/
	var curPopup;
	var buttons = $('#path .butt');
	var popupIsClosing = false;
	var popupCloseTimeout = null;
	var popupCloseInterval = 1000;
	
	buttons.each(function() {
		var self = $(this);
		var inner = self.find('.box');
		inner.mouseout(function() {
			popupIsClosing = true;
			clearTimeout(popupCloseTimeout);
			popupCloseTimeout = setTimeout(function() {
				hidePopup(inner);
			}, popupCloseInterval);
		});
		var links = inner.find('a');
		links.each(function() {
			var self = $(this);
			self.mouseover(function() {
				popupIsClosing = false;
			});
			self.mouseout(function() {
				popupIsClosing = true;
			});
		});
		var sw = self.find('.inner .switcher');
		sw.mouseover(function() {
			if (curPopup != inner) {
				if (curPopup) curPopup.fadeOut();
				inner.fadeIn();
				curPopup = inner;
			}
		});
	});
	
	function hidePopup(inner) {
		if ((inner == curPopup)&&(popupIsClosing)) {
			inner.fadeOut();
			popupIsClosing = false;
			curPopup = null;
		}
	}
	/**
	* Главное меню
	*/
	var menuItems = $('#menu ul li').filter(function() {
		return this.parentNode.parentNode.id == 'menu';
	});
	var subMenuTimeout = null;
	var subMenuInterval = 500;
	var curSub = null;
	
	menuItems.each(function() {
		var sub = $(this).find('ul');
		sub.css('display', 'none').css('visibility', 'hidden');
		this.onmouseover = function() {
			clearTimeout(subMenuTimeout);
			subMenuTimeout = setTimeout(function() {
				sub.css('visibility', 'visible');
				sub.fadeIn();
				if ((curSub)&&(curSub != sub)) curSub.fadeOut();
				curSub = sub;
			}, subMenuInterval);
		};
		this.onmouseout = function() {
			sub.css('display', 'block')
			clearTimeout(subMenuTimeout);
			subMenuTimeout = setTimeout(function() {
				sub.fadeOut(function() {
					sub.css('visibility', 'hidden');
				});
			}, subMenuInterval);
		};
	});
	/**
	* Горизонтальное меню на главной
	*/
	var links = $('#hmenu .bg a');
	var flash_bg = $('#flash_bg');
	links.hover( function() {flash_bg.addClass('hover');}, function() {flash_bg.removeClass('hover');} );
	
	/**
	* Смена машины на главной
	*/
	var car_swf  = $('#car').get(0);
	if (car_swf)
	{
		$('#vmenu a').add(links).
			hover( function() {car_swf.carChange();}, function() {car_swf.carChange();} );
	}
	
	/**
	* Форма обратной связи
	*/
	$('div.feedback form').
		find(':input[def_value]').each ( function () {
				var item = $(this); 
				var def_value = item.attr('def_value');
				if ( def_value && item.val() == '') item.val(def_value).addClass('empty');
			}).
			focus( function () {
				var item = $(this); 
				var def_value = item.attr('def_value');
				if ( def_value && item.val() == def_value) item.val('').removeClass('empty');
			}).blur( function () {
				var item = $(this); 
				var def_value = item.attr('def_value');
				if ( def_value && item.val() == '') item.val(def_value).addClass('empty');
			}).
			end().
		submit( function () {
			$(this).find('.empty').each ( function () { $(this).val(''); });
			return true;
		});
});
