jQuery(function () {
	var stopSlide = false;
	// Флажок первой установки активной категории
	var firstSlide = true;
	$('div.menu li.category').hover(function () {
		
		var active = getActive();
		active.removeClass('activeCategory');
		unhoverMenuItem(active);
		//
		hideSlide(active,firstSlide);
		if (firstSlide)
		{
			firstSlide = false;
		}
		$(this).addClass('activeCategory');
		showSlide($(this));
		hoverMenuItem($(this))
	},function () {
		// Возвращаем изображеие
		unhoverMenuItem($(this))
	});
	$('a.prev').click(function () {
		var active = getActive();
		active.removeClass('activeCategory');
		unhoverMenuItem(active);
		//
		hideSlide(active,firstSlide);
		if (firstSlide)
		{
			firstSlide = false;
		}

		if (active.prev().length != 0) {
			active = active.prev().prev();
		} else {
			active = $('li.category:last');
		}
		active.addClass('activeCategory');
		showSlide(active);
		hoverMenuItem(active);
	});
	$('a.next').click(function () {
		var active = getActive();
		hideSlide(active,firstSlide);
		if (firstSlide)
		{
			firstSlide = false;
		} else {
			active.removeClass('activeCategory');
			unhoverMenuItem(active);
			if (active.next().length != 0) {
				active = active.next().next();
			} else {
				active = $('li.category:eq(0)');
			}
			active.addClass('activeCategory');
			//
			
		}

		showSlide(active);
		hoverMenuItem(active);

	});
	// Скрипт, каждые 4 секунды, обновляет изображени
	
	function showSlide(obj) {

		var pos = obj.prevAll('.category').length + 1;
		$('#slideShow img').stop();
		$('#slideShow img:eq('+ pos +')').css({
			opacity : 0.4,
			display : 'block'
		});
		$('#slideShow img:eq('+ pos +')').animate({
			opacity : 1
		},500);
		
	}
	function hideSlide(obj,firstSlide) {
		var pos = obj.prevAll('.category').length + 1;
		if (firstSlide)
		{
			pos = 0;
		}

		// Показываем новый, делаем текущий наведенным
		$('#slideShow img:eq('+ pos +')').hide();
	}
	function hoverMenuItem(obj) {
		var id = obj.attr('rel');
		obj.find('a').css('color','#6CA744');
		
		obj.find('img').attr('src','/files/catalog_category/hover/' + id)
			.css('height','56px')
			.css('width','124px')
			.css('margin-left','0px')
			.css('margin-top','16px');
	}
	function unhoverMenuItem(obj) {
		if (obj.hasClass('currentCategory'))
		{
			return;
		}
		var id = obj.attr('rel');
		obj.find('a').css('color','#000000');
		obj.find('img').attr('src','/files/catalog_category/' + id).css('height','50px').css('width','110px')
			.css('margin-left','7px')
			.css('margin-top','20px');
	}
	function getActive() {
		var active = '';
		// Получаем текущий элемент
		if ($('div.menu li.activeCategory').length == 0)
		{
			// Если нету, ставим первый
			active = $('div.menu li.category:eq(0)');
		} else {
			active = $('div.menu li.activeCategory');
		}
		return active;
	}
});
jQuery(function () {
	var $ = jQuery;
	$('a.hlLink').click(function () {
		$('#askForm').show('slow');
		return false;
	});
	$('#askForm a#close').click(function () {
		$('#askForm').hide();
		return false;
	})
	$('#askForm a.submit').click(function () {
		$('#askForm form').submit();
		return false;
	});
	$('#askForm form').validate({
		rules : {
			name : {
				required : true
			},
			email : {
				required : true,
				email    : true
			},
			content : {
				required : true
			}
		},
		messages : {
			name : {
				required : 'Обязательное поле'
			},
			email : {
				required : 'Обязательное поле',
				email    : 'Некорректный email'
			},
			content : {
				required : 'Обязательное поле'
			}
		},
		
		errorPlacement : function (error, element) {
			error.appendTo( element.parent().parent() );
		}
	});
});
jQuery(function () {
	var $ = jQuery;

	$('#searchLink').click(function () {
		$(this).parents('form').submit();
		return false;
	});

	$('#searchField').focus(function () {

		if (this.value == 'Поиск по сайту')
		{
			this.value = '';
		}
	});

	$('#searchField').blur(function () {
		if (this.value.length == '')
		{
			this.value = 'Поиск по сайту';
		}
	})
});
jQuery(function () {
	var $ = jQuery;
	$('select[name=year],select[name=month]').change(function () {
		// Грузим html-код новостей и список месяцев за текущий год
		$.post('/news/',{
			ajax 		: 1,
			month 		: $('select[name=month]').val(),
			year 		: $('select[name=year]').val(),
		},function (json) {
			var data = eval('(' + json + ')');
			$('.arhive_news').replaceWith(data['html']);
		});
	});
});
