$(document).ready(function(){
	//Анимация вертелок
	speed = 400;
	tmOut = 3000;
	$('.left_item').each(function(i, o) {
		//Установить начальные параметры для каждого оъекта
		o.imgs = $('img', o);
		o.descr = $('.descr', o);
		o.current = 0;
		o.is_animate = true;
		//Если изображений меньше двух то не анимируем
		if(o.imgs.length < 2) {
			return;
		}
		setHover(o);
		//Добавить события на стрелки право/влево
		$('a.rarr', o).click(function() {
			if (!is_anim(o))
				move_right(o);
			return false;
		});
		$('a.larr', o).click(function() {
			if (!is_anim(o))
				move_left(o);
			return false;
		});
		//Установить события на наведение мыши и убирании мыши на объект 
		//необходимо для того чтобы не анимировать элементы в момент наведения
		$(o).mouseover(function() { o.stop_anim = true; }).mouseout(function() { o.stop_anim = false; });
		//Запустить цикличное движение слева направо
		interval = setInterval(function go() {
			if (!o.stop_anim) {
				move_right(o);
			}
		}, tmOut);
	});
	//Подсказки для поиска
	search_suggestions_init();
});
//Создать событие при наведении
function setHover(o) {
	$(o).hover(
		function() {
			$(this).find('.arrs').show();
		},
		function() {
			$(this).find('.arrs').hide();
		}	
	);
}
//Движение слева направо
function move_right(o) {
		var currentSlide = o.current;
		var nextSlide = ++o.current == o.imgs.length ? 0 : o.current;
		$(o.imgs[nextSlide]).css({ left: "-176px" }).show().animate({ left: 0 }, speed, "swing");
		$(o.imgs[currentSlide]).show().animate({ left: "176px" }, speed, "swing", function() { $(this).hide(); });
		o.current = nextSlide;
		changeDescr(o);
}
//Движение справа налево
function move_left(o) {
		var currentSlide = o.current;
		var nextSlide = ++o.current == o.imgs.length ? 0 : o.current;
		$(o.imgs[currentSlide]).show().animate({ left: "-176px" }, speed, "swing", function() { $(this).hide(); });
		$(o.imgs[nextSlide]).css({ left: "176px" }).show().animate({ left: 0 }, speed, "swing");
		o.current = nextSlide;
		changeDescr(o);
}
//Изменить описание элемента в объекте
function changeDescr(o) {
	o.descr.hide();
	$(o.descr[o.current]).fadeIn('slow');
}
//Проверить анимирован ли объект
function is_anim(o) {
	return $('img:animated', o).length != 0;
}
//menu
jQuery(function(){
	jQuery('#top_menu').superfish({
		autoArrows: false, 
		delay: 700
	});
});
//PngFix
$(function(){$(document).pngFix();});
//Suggestion
var interval2;
var s_form;
var s_input;
var suggestions;
var f = 0;

function search_suggestions_init() {
	s_form = $('#search form');
	s_input = $('input', s_form);
	suggestions = $('#suggestions');
	//Сделать возможным отправку формы без ajax по кнопке
	$('.search_but', s_form).click(function() {
		document.location = s_form.attr('action') + '?search=' + s_input.val();
	});
	s_input
	.click(function() {
		if (f == 0)
			$(this).val('');
		f++;
	})
	.attr({'autocomplete' : 'off'})
	.keyup(function() {
		clearInterval(interval2);
		interval2 = setInterval( function() {
			s_form.submit();
			clearInterval(interval2); 
		}, 700);
	});
	s_form.ajaxForm({
    	beforeSubmit:function(){
			//Не отправлять запрос до тех пор пока длина значения поиска не превысит два символа
			if (s_input.val().length < 2)
			{
				suggestions.fadeOut();
				return false;
			}
			else
			{
//				alert('go');
				return true;
			}
		},
		success:function(response){
//			alert(response);
			response = $('#results', $(response));
//			alert(response.html());
//			suggestions.hide();
			if (response.html() != null && (suggestions.html() != response.html() || suggestions.css('display') == 'none'))
			{
				suggestions.html(response.html());
				suggestions.fadeIn();
				$('#close_guggestions', suggestions).click(function() { suggestions.fadeOut(); });
			}
		}
	});
}

