$(document).ready(function() {
	function do_search(elem){
		$search_query = elem.parent().find(".search_query").attr('value');
		if (($search_query != '') && ($search_query != 'Что ищем?')){
			location.href = '/search/'+$search_query;
			return false;
		}
		else
			alert('Задан пустой поисковый запрос!');
	}	
	$('.search_query').keypress(function(e) {
		if (e.keyCode == '13') { // Если был нажат ENTER
			e.preventDefault(); // Игнорируем действие по умолчанию)
			do_search($(this));
		}
	});
	$(".search_button").click(function(){
		do_search($(this));
	});
	
	$("#full").click( function(){
		$('input[name="fio"]').attr("value", "one1");
		$('input[name="email"]').attr("value", "one3");
		$('input[name="days_in_week"]').attr("value", "5");
		$('input[name="hours_in_day"]').attr("value", "8");
		$('input[name="age"]').attr("value", "18");
		$('input[name="live"]').attr("value", "Москва");
		$('textarea[name="other"]').html("oneoneoneone");	
	});
		
	function show_message(message, backgroundColor, color){
		$.blockUI({ 
			message: message,
			css: { 
				border: 'none', 
				padding: '15px', 
				fontSize: '20pt',
				backgroundColor: backgroundColor, 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: '.5', 
				color: color 
			}
		});
	}

	// меню, показываем вышестоящие блоки текущего пункта меню
	$("li.curr").each( function(){
		$(this).parent().parent().find("ul:first").css("display", "block");		
		$(this).parent().parent().parent().parent().find("ul:first").css("display", "block");
		$(this).parent().parent().parent().parent().parent().parent().find("ul:first").css("display", "block");		
	});
	
	// запрещаем автозаполнение инпутов
	$("input").attr("autocomplete", "off");
													  
	// выравнивание элементов
	$("img[align='right']").each(function(i) {
		$(this).css('margin', '10px 0px 0px 10px');
	});
	
	$("img[align='left']").each(function(i) {
		$(this).css('margin', '10px 10px 0px 0px');
	});
	
	$("img[align='middle']").each(function(i) {
		$(this).css('margin-top', '10px');
		$(this).css('margin-bottom', '10px');
		$(this).css('margin', '0 auto');
	});
	
	$("table[align='center']").each(function(i) {
		$(this).css('margin', '0 auto');
	});
		
	// устанавливаем класс CURR для ссылок ведущих на текущую страницу
	var $location = document.location.pathname;
 	$('a').each(function(){
		$(this).removeClass('curr');
		if ($(this).attr('href') == $location)
			$(this).addClass('curr').parent().addClass('curr');
	})
	
	// уголки скруглённые везде
	$(".corner_all").each(function(){
		var $corner = $(this).attr('corner');
		$(this).append('<div class="'+$corner+' lt"></div>'+
							'<div class="'+$corner+' rt"></div>'+
							'<div class="'+$corner+' lb"></div>'+
							'<div class="'+$corner+' rb"></div>');
	 });
	
	// уголки скруглённые сверху
	$(".corner_top").each(function(){
		 $(this).append('<div class="corner lt"></div>'+
							 '<div class="corner rt"></div>');
	 });	
	
	// уголки скруглённые снизу
	$(".corner_bottom").each(function(){
		 $(this).append('<div class="corner lb"></div>'+
							 '<div class="corner rb"></div>');
	 });		
	
	// выводим сообщение
	$("#message").each(function(){
															alert('привет');
 		$('#video object').hide();		

		var message = $(this).attr('value');			// текст сообщения
		winclass = $(this).attr('winclass');			// тип отображения (модальное или нет)
		href = $(this).attr('href');							// урл для перехода после отображения сообщения, 
																							// если пустой - перегружаем страницу																					
		pattern = new RegExp("^ERROR$.*", "gm");
		if (/ERROR/.test(message)){
			backgroundColor = '#000';
			color = '#f00';
		}
		else{
			backgroundColor = '#C1B3E7';
			color = '#306376';
		}
		
		if (winclass == 'modal'){
			$.blockUI({ 
				message: message+'<br /><input type="button" id="close" href="'+href+'" value="Закрыть" />',
				css: { 
					border: 'none', 
					padding: '15px', 
					fontSize: '20pt',
					backgroundColor: backgroundColor, 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: '.8', 
					color: color 
				}
			});
		}
		else{
			$.blockUI({ 
				message: message,
				css: { 
					border: 'none', 
					padding: '15px', 
					fontSize: '20pt',
					backgroundColor: backgroundColor, 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: '.8', 
					color: color 
				},
				timeout: 2500,
				onUnblock: function() { 
					$('#video object').show();
					if (typeof href !== "undefined"){
						if (href != 'noreboot')
							location.href = href;
					}
					else 
						// по умолчанию - перегружаем страницу для предотвращения повторной отправки форм					
						location.href = document.location.href; 
				}
			})		
		}																								
	})

	$('#close').click(function() { 
		href = $(this).attr('href');
		$.unblockUI(); 
		if (href !== "undefined")
			location.href = href; // переход на указанынй адрес
		else
			location.href = document.location.href; // по умолчанию - перегружаем страницу для предотвращения повторной отправки форм				

		return false; 
	}); 	
});
