$(document).ready(function() {
	// Обработка изменения ширины экана
	$(function() {
		setInterval(checkBrowserWidth, 500);
	});

	//Инициализация галлереи
	$('.gallery a').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.4,
		imageLoading: '../images/lightbox-ico-loading.gif',
		imageBtnClose:'../images/lightbox-btn-close.gif',
		imageBtnPrev: '../images/lightbox-btn-prev.gif',
		imageBtnNext: '../images/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Изображение',
		txtOf: 'из'
	});

	$('div[@id^=menu_item_]').hover(
		function() { $(this).find('div.popup_menu').show(100); },
		function() { $(this).find('div.popup_menu').hide(100); }
	);

	//Обработка выбора даты (поиск)
	$('#date-box').datepicker({
		dateFormat:'yy-mm-dd',
		onSelect:function(date) {
			//alert('Вы выбрали дату: ' + date);
			//$('#date-box-text').text(date);
			//$('#full_date').val(date);
			document.location = '/news/?do=search_news&full_date='+date;
		}
	});

	//Обработка фокуса поля "поиск"
	$("input[name='search_text']")
		.css('color', '#999999')
		.focus(function() {
			if ($(this).val() == 'Введите фразу') {
				$(this).val('');
			}
			$(this).css('color', '#000000');
		})
		.blur(function() {
			if ($(this).val() == '') {
				$(this).val('Введите фразу');
			}
			$(this).css('color', '#999999');
		});

	//Обработка фокуса поля "e-mail"
	$("input[name='filter_value']")
		.css('color', '#999999')
		.focus(function() {
			if ($(this).val() == 'Введите e-mail') {
				$(this).val('');
			}
			$(this).css('color', '#000000');
		})
		.blur(function() {
			if ($(this).val() == '') {
				$(this).val('Введите e-mail');
			}
			$(this).css('color', '#999999');
		});


	//Обработка фокуса поля "e-mail"
	$("input[name='email']")
		.css('color', '#999999')
		.focus(function()
		{
			if ($(this).val()=='Электронный адрес') $(this).val('');
			$(this).css('color', '#000000');
		})
		.blur(function()
		{
			if ($(this).val()=='') $(this).val('Электронный адрес');
			$(this).css('color', '#999999');
		});

	//Обработка фокуса поля "Имя"
	$("input[name='name']")
		.css('color', '#999999')
		.focus(function()
		{
			if ($(this).val()=='Имя') $(this).val('');
			$(this).css('color', '#000000');
		})
		.blur(function()
		{
			if ($(this).val()=='') $(this).val('Имя');
			$(this).css('color', '#999999');
		});

	//Обработка фокуса текстового поля
	$("textarea[name='text']")
		.css('color', '#999999')
		.focus(function()
		{
			if ($(this).val()=='Текст сообщения') $(this).val('');
			$(this).css('color', '#000000');
		})
		.blur(function()
		{
			if ($(this).val()=='') $(this).val('Текст сообщения');
			$(this).css('color', '#999999');
		});

	// Обработка всплывающего окна дополнительной информации о событиях
	$('.event').hover(
		function() {
			$('.event_popup', this).css('display', 'block');
		},
		function() {
			$('.event_popup', this).css('display', 'none');
		}
	);

	// Скролл эвентов (выставок)
	document.getElementById('event_list').scrollTop = document.getElementById('event_current').offsetTop;
});


/*
 * Проверка формы подписки e-mail
 */
function check_subscribe_form() {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.subscribe_form.email.value)) {
		document.subscribe_form.submit();
	} else {
		alert('Введите корректный e-mail');
		document.subscribe_form.email.value='';
		document.subscribe_form.email.focus();
	}
}


/*
 * Проверка формы обратной связи
 */
function check_feedback_form() {
	var msg="";

	if (document.feedback_form.name.value=='Имя' || document.feedback_form.name.value=='') msg+='\n • Введите Ваше имя';
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.feedback_form.email.value))) msg+='\n • Введите корректный e-mail';
	if (document.feedback_form.text.value=='Текст сообщения' || document.feedback_form.text.value=='') msg+='\n • Введите текст сообщения';

	if (msg!='') {
		alert('Исправьте следующие ошибки:\n'+msg);
	} else {
		document.feedback_form.submit();
	}
}

/*
 * Проверка формы вопросов
 */
function check_question_form() {
	var msg="";

	if (document.question_form.name.value=='Имя' || document.question_form.name.value=='') {
		msg+='\n • Введите Ваше имя';
	}
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.question_form.email.value))) {
		msg+='\n • Введите корректный e-mail';
	}
	if (document.question_form.text.value=='Текст сообщения' || document.question_form.text.value=='') {
		msg+='\n • Введите текст сообщения';
	}

	if (msg!='') {
		alert('Исправьте следующие ошибки:\n'+msg);
	} else {
		document.question_form.submit();
	}
}

/**
 * Обработка раскрытия плашки баннеров
 */
function select_banner(id) {
	if ($('input[name="news[' + id + ']"]').attr('checked')) {
		$('select[name="banner[' + id + ']"]').attr('disabled', false);
	} else {
		$('select[name="banner[' + id + ']"]').attr('disabled', true);
	}
}

/**
 * Определение ширины рабочей области
 */
function getBrowserWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientWidth;
	} else if (document.body) {
		return document.body.clientWidth;
	}
	return 0;
};

/**
 * Определение действий при различных значениях ширины рабочей области
 */
function checkBrowserWidth() {
	var width = getBrowserWidth();

	if (width <= 1260) {
		$('#header_left').css('background-image', 'url(/images/header_left_1.gif)');
	} else {
		$('#header_left').css('background-image', 'url(/images/header_left.gif)');
	}

	if (width <= 1110) {
		$('#header_left').css('background-image', 'url(/images/header_left_2.gif)');
	}

	if (width <= 1050) {
		$('#header_text_2').css('display', 'none');
	} else {
		$('#header_text_2').css('display', 'block');
	}

	//if (width <= 1000) {
	//	$('#header_pics').css('display', 'none');
	//	$('#journal_cover_1').css('display', 'none');
	//} else {
	//	$('#header_pics').css('display', 'block');
	//	$('#journal_cover_1').css('display', 'block');
	//}

	//if (width <= 890) {
	//	$('#header_search').css('display', 'none');
	//} else {
	//	$('#header_search').css('display', 'block');
	//}
}
