$(document).ready(function(){
	$(".row").each(function() {
		var maxOffset = 0;
		$(this).find(".content").each(function(i){
				if (this.offsetHeight > maxOffset) {
					maxOffset = this.offsetHeight;
				}
		});
		$(this).find(".content").each(function(i){
				$(this).css({height: maxOffset - 20 + "px"});
		});
	});
	$("#search .string").focus(function() {
		if ($(this).val() == "Поиск") {
			$(this).val("");
		}
	});
	$("#search .string").blur(function() {
		if ($(this).val() == "") {
			$(this).val("Поиск");
		}
	});
	$("#login-mail").focus(function() {
		if ($(this).val() == "Введите Ваш ник") {
			$(this).val("");
		}
	});
	$("#login-mail").blur(function() {
		if ($(this).val() == "") {
			$(this).val("Введите Ваш ник");
		}
	});
	$("#login").click(function(){
		$(this).css({zIndex: 0});
		$("#login-form").fadeIn();
		return false;
	});
	$("#login-form .tab").click(function(){
		$("#login").css({zIndex: 110});
		$("#login-form").fadeOut();
		return false;
	});
	$(".ratings .my li").click(function() {
		if (!$(this).parent().next().hasClass("rated"))
		{
			if ($("#login").text() == "Вход") 
			{
				alert("Чтобы оценить ресторан необходимо зарегистрироваться и войти");
			}
			else 
			{
				var matches = new Array ("Очень плохо", "Плохо", "Нормально", "Хорошо", "Очень хорошо");
				$(this).addClass("on");
				$(this).prevAll().addClass("on");
				$(this).parent().next().addClass("rated");
				var rating = $(this).text();
				$(this).parent().next().text(matches[$(this).text() - 1]);
				$.post('./', {'rating':rating});
			}
		}
	});
	$(".ratings .my li").mouseover(function() {
		var matches = new Array ("Очень плохо", "Плохо", "Нормально", "Хорошо", "Очень хорошо");
		if (!$(this).parent().next().hasClass("rated"))
		{
			$(this).prevAll().addClass("hover");
			$(this).nextAll().removeClass("hover");
			$(this).parent().next().text(matches[$(this).text() - 1]);
		}
		else 
		{
			$(this).parent().addClass("rated");
		}
	});
	$(".ratings .my ul").mouseout(function () {
		$(this).children().removeClass("hover");
		if (!$(this).next().hasClass("rated")) {
			$(this).next().text("Нет оценки");
		}
	});
	$(".gallery .thumbnails a").click(function() {
		$(".gallery .view img").removeAttr("width");
		$(".gallery .view img").removeAttr("height");
		$(".gallery .view img").attr("src", $(this).attr("href"));
		return false;
	});
	question_text();
	$("label[for=id_geo]").before($("div.galadmin").html());
	$("div.galadmin").hide();
	$("input.multiselect_add").after('<img src="/media/img/admin/icon_addlink.gif" class="multiselect_img_add" title="Добавить" />');
	$("input.multiselect_add").before('<ul class="multiselect_ul_add" style="margin-left:0"></ul>');
	$("img.multiselect_img_add").click(function() {
		if ($(this).prev().attr("name")) {
			$(this).prev().attr("tempname", $(this).prev().attr("name"));
			$(this).prev().attr("name", "");
		}
		if ($(this).prev().val() == "") 
		{
			alert('Необходимо ввести значение');
			return;
		}
		else if (!checkUnique($(this).prev().val())) 
		{
			alert('Это значение уже введено');
			return;
		}
		$("ul.multiselect_ul_add").append('<li><span>' + $(this).prev().val() + '</span><img src="/media/img/admin/icon_deletelink.gif" title="Удалить" class="multiselect_img_del"><input type="hidden" name="' + $(this).prev().attr('tempname') + '" class="multiselect_hidden" value="' + $(this).prev().val() + '" /></li>');
		$(this).prev().val("");
		$("img.multiselect_img_del").click(function(){
			if (confirm("Удалить значение?")) {
				$(this).parent().remove();
			}
		});

		return false;
	});
	$("img.multiselect_img_del").click(function(){
		if (confirm("Удалить значение?")) {
			$(this).parent().remove();
		}
	});
});

/* Вспомогательные функции для Piterline */

/* Показывает popUP окно 
/* @param link - ссылка на содержимое popUP  */
function showPopUp(link) {
    var href = link.href;
    var name = 'Имя';
    var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
    win.focus();   
    return false;
}

function _hide_question_text() {
    $('label[for=id_question_text]').hide();
    $('#id_question_text').hide();
	$('#id_question_text').next().hide();
}

function _show_question_text() {
    $('label[for=id_question_text]').show();
    $('#id_question_text').show();
	$('#id_question_text').next().show();
}

function question_text() {
   if ($('#id_question_choice').val() != 'D') {
        _hide_question_text()    
   }
   $('#id_question_choice').change(function() {
        if (this.value == 'D') {
            _show_question_text()    
        } else {
            _hide_question_text()    
        }
   })
}

function checkUnique(value) {
    elements = $('ul.multiselect_ul_add>li>input.multiselect_hidden');
    for (i = 0; i < elements.length; i++) {
        if (elements[i].value == value) return false;
    }
    return true;
};
