jQuery(function () {
	var $ = jQuery;
	var monthList = [
			'Январь',
			'Февраль',
			'Март',
			'Апрель',
			'Май',
			'Июнь',
			'Июль',
			'Август',
			'Сентябрь',
			'Октябрь',
			'Ноябрь',
			'Декабрь',
		];
	$('select[name=arhiv_meseatz],select[name=arhiv_god]').change(function () {
		// Грузим html-код новостей и список месяцев за текущий год
		$.post('/news/archive/',{
			ajax 		: 1,
			month 		: $('select[name=arhiv_meseatz]').val(),
			year 		: $('select[name=arhiv_god]').val(),
		},function (json) {
			var data = eval('(' + json + ')');
			$('#news_list').replaceWith(data['html']);
		});
	});
	// Основное меню
	$('ul.top_menu > li').hover(function () {
		$(this).addClass('active');
		$(this).find('ul.m1').show();
	},function () {
		if (!$(this).hasClass('staticActive')) {
			$(this).removeClass('active');
		}
		$(this).find('ul.m1').hide();
		$(this).find('ul.m2').hide();
	});
	var ulM2Hovered = false;
	// Подменю
	$('ul.m1 > li').hover(function () {
		if ($(this).find('span').length > 0) {
			var pos = $(this).prevAll('li').find('span').length;
			$(this).parent().parent().find('ul.m2:eq(' + pos +')').show();
		}
	},function () {
		if ($(this).find('span').length > 0) {
			// Делаем заглушку
			var self = this;
			window.setTimeout(function () {
				if (!ulM2Hovered) { 
					var pos = $(self).prevAll('li').find('span').length;
					$(self).parent().parent().find('ul.m2:eq(' + pos +')').hide();
				}
			}, 100)
		}
	});
	$('ul.m2').hover(function () {
		ulM2Hovered = true;
	},function () {
		ulM2Hovered = false;
		$(this).hide();	
	});
	
	$('#header_img').each(function () {
		var self = this;
		var count = $(this).find('img').length;
		$(this).find('img').each(function () {
			var img = new Image();
			img.src = $(this).attr('src');
		});
		window.setInterval(function () {
			$(self).find('img:visible').each(function () {
				$(this).hide();
				var pos = $(this).prevAll('img').length;
				var img = 0;
				if (pos < count - 2) {
					img = $(this).next(); 
					
				} else {
					img = $(self).find('img:eq(0)');
				}
				img.css('opacity','0').css('display','block').animate({
					opacity : 1
				},1000);
				
			});
		},10000);
	});
	$('.tab a').click(function () {
		$('.tab').removeClass('active');
		$(this).parent().addClass('active');
		$('div.a2,div.a3,div.a4,div.diva4,div.a5').hide();
		var className = $(this).parent().attr('rel');
		$('div.' + className).show();
		return false;
	});
	var results = window.location.search.match(/\?tab=([0-9])/)
	if (results)
	{
		$('.tab:eq(' + (parseInt(results[1]) - 1)+') a').click();
	}
		
});

