﻿(function($) {
	//Constants
	$.searchHint = 'Поиск по сайту';
	$.mandatoryFieldsHint = 'Пожалуйста, заполните все обязательные поля';
	$.datepickerOptions = {
		dateFormat: 'dd-mm-yy',
		dayNames: ['Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота'],
		dayNamesMin: ['В', 'П', 'В', 'С', 'Ч', 'П', 'С'],
		dayNamesShort: ['Вос', 'Пон', 'Вто', 'Сре', 'Чет', 'Пят', 'Суб'],
		firstDay: 1,
		monthNames: ['ЯНВАРЬ', 'ФЕВРАЛЬ', 'МАРТ', 'АПРЕЛЬ', 'МАЙ', 'ИЮНЬ', 'ИЮЛЬ', 'АВГУСТ', 'СЕНТЯБРЬ', 'ОКТЯБРЬ', 'НОЯБРЬ', 'ДЕКАБРЬ'],
		monthNamesShort: ['Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек'],
		nextText: 'След',
		prevText: 'Пред',
		showAnim: 'slideDown'
	};
	//Functions
	$.createSearch = function() {
		$('#srchBox').val($.searchHint).focus(function(e) {
			if ($(this).val() == $.searchHint) {
				$(this).val('');
			}
		}).blur(function(e) {
			if ($.trim($(this).val()).length == 0) {
				$(this).val($.searchHint);
			}
		});
		$('#srchForm').submit(function(e) {
			var searchString = $.trim($('#srchBox').val());
			return searchString.length != 0 && searchString != $.searchHint;
		});
		$('#srchBtn').click(function(e) {
			$('#srchForm').submit();
		});
	};
	$.createMenu = function() {
		$('.menuList').each(function(idx) {
			$($(this).parent().get(0))
				.mouseenter(function(e) {
					var topMenuItem = $(this);
					var parentTable = $(topMenuItem.parent().parent().get(0));
					var top = parentTable.offset().top;
					var left = topMenuItem.offset().left;
					var height = topMenuItem.height();
					var menuList = $(topMenuItem.children('.menuList').get(0));
					menuList.show().css({ left: left, top: top + height });
					e.stopPropagation();
				})
				.mouseleave(function(e) {
					$('.menuList').hide();
				});
			$(this).children('.listItem')
				.mouseenter(function(e) { $(this).addClass('activeItem'); })
				.mouseleave(function(e) { $(this).removeClass('activeItem'); })
		});
	};
	$.createGradient = function() {
		$('div.pageTitle div').prepend('<span></span>');
	};
	$.createGallery = function(items) {
		$(items).prettyPhoto({
			allowresize: false,
			theme: 'dark_rounded'
		});
	};
	$.prepareStandalonePhotos = function() {
//alert(($('a[rel="prettyPhoto"]').length));
                if ($('a[rel="prettyPhoto"]').length==0) return;
		$('a[rel="prettyPhoto"]').prettyPhoto({
			allowresize: false,
			theme: 'dark_rounded'
		});
	};
	$.showCalendar = function(atElement) {
		//if (document.getElementById(atElement))
                $(atElement).datepicker($.datepickerOptions);
	};
	$.registerForValidation = function(formSelector, requiredFields) {
		$(formSelector).bind('submit', { requiredFields: requiredFields }, function(e) {
			var isValid = true;
			$(e.data.requiredFields.join(',')).each(function() {
				if ($.trim($(this).val()).length == 0) {
					isValid = false;
				}
			});
			if (!isValid) {
				alert($.mandatoryFieldsHint);
			}
			return isValid;
		});
	};
	$(document).ready(function() {
		$.createSearch();
		$.createMenu();
		$.createGradient();
		$.prepareStandalonePhotos();
	});
})(jQuery);
