// <evgen>
$(document).ready(function(){
    // ---- <Меню каталога> ------//
    $("#left_catalog .list5").click(function() {
        var list5 = $(this).find("div");
        if (list5.css("display") == 'block') {
            list5.css("display", "none");
            $(this).next().slideDown("fast");
        } else {
            list5.css("display", "block");
            $(this).next().slideUp("fast");
        }
    });
    // ---- </Меню каталога> ------//
    // ---- <Форма авроры> ------//
    $("select[name=stars]").change(function() {
        if ($(this).val() != -1 && $(this).val() != 0 && $(this).val() != 5)
            $('#higher').css('display', 'block');
        else
            $('#higher').css('display', 'none');
    });
    // ---- </Форма авроры> ------//
    // ---- <Наши спецы> ----------//
    $("#ourSpec a").click(function() {
        var id = $(this).attr("rel");

        $(".popup").each(function() {
            if ($(this).css("display") == "block")
            $(this).css("display", "none");
        });

        if ($("#"+id).html() == null) {
            $.get("/ajax_index.php?module=Expert&action=ajax_getExpert", {id: id}, function(data) {
                $("body").append(data);
                $("#spec"+id).slideDown();
            });
        } else {
            $("#spec"+id).slideDown();
        }
    });
    // ---- <Наши спецы> ----------//
    // ---- <Вкладка Отзывы> ------//
    $("#submit_vote").click(function() {
        var vote_data = new Object();
        $(".criterion").each(function(i) {
            var num = i + 1;
            vote_data[ $(this).val() ] = $("#scale_" + (i + 1) + " div a.checked").size();
        });
        
        $.ajax({
            url: "/ajax_index.php?action=ajax_addVote",
            data: ({
                module:    $("#module").val(),
                link_id:   $("#link_id").val(),
                vote_data: vote_data
            }),
            dataType: "json",
            success: function (answer) {// Обновляем форму
                if (answer['average_vote']) {
                    $("#rating_ul li").each(function(i) {
                        var vote_value = answer['average_vote'][i];
                        if (vote_value != 0) {
                            $(this).children("div").children("div").animate({ 
                                width: vote_value + "%"
                            }, 500);
                        }
                    });
                }
                $.showMessages(answer["MSG"], answer["STATUS"]);    
            },
            error: function () {
                $.noticeAdd({
                    text: 'Извините! Возникла непредвиденная ошибка.',
                    type: 'error-msg'
                });
                $.noticeAdd({
                    text: 'Попробуйте повторить позднее.',
                    type: 'error-msg'
                });
            }
        });

    });
    $("#submit_comment").click(function() {
        var comment_text = $("#comment_text");
        var comment_name = $("#comment_name");
        if (!comment_text.val()) {
            $.noticeAdd({
                text: 'Заполните текст отзыва',
                type: 'error-msg'
            });
        }
        if (!comment_name.val()) {
            $.noticeAdd({
                text: 'Введите своё имя',
                type: 'error-msg'
            });
        }
        if (comment_name.val() && comment_text.val()) {
            $.ajax({
                url: "/ajax_index.php?action=ajax_addComment",
                type: 'POST',
                data: ({
                    module:       $("#module").val(),
                    link_id:      $("#link_id").val(),
                    comment_name: comment_name.val(),
                    comment_text: comment_text.val()
                }),
                dataType: "json",
                success: function (answer) {// Обновляем форму
                    if (answer["STATUS"] == "OK") {
                        $("#comments_h").css("display", "block");
                        $(".otzyvy").append("<li class='user' style='display:none'>" + comment_text.val() + "<div>" + comment_name.val() + "<span>" + answer['date'] + "</span></div></li>").find("li:last").fadeIn("slow");
                    }
                    $.showMessages(answer["MSG"], answer["STATUS"]);    
                },
                error: function () {
                    $.noticeAdd({
                        text: 'Извините! Возникла непредвиденная ошибка.',
                        type: 'error-msg'
                    });
                    $.noticeAdd({
                        text: 'Попробуйте повторить позднее.',
                        type: 'error-msg'
                    });
                }
            });
        }
    });
    // ---- </Вкладка Отзывы> ------//
});
function on_scale (scale, mark)
{
    var a = document.getElementById(scale).getElementsByTagName('a');
    var b = false;
    for (i=0; a[i]; i++) {
        var red=i*59+16;
        var green=i*25+100;

        if (!b) { 
            a[i].style.backgroundColor = '#' + red.toString(16) + green.toString(16) + '51';
            a[i].setAttribute("class", "checked");
        } else {            
            a[i].style.backgroundColor = '#eeeeee';         
            a[i].setAttribute("class", "unchecked");
        }
        if (a[i] == document.getElementById(mark)) {
            b = 1;
        }
    }
}
function updateHotels(resort_sel, form_id)
{
  $.ajax({
    url: '/ajax_index.php?module=SouthStarFrameService&action=getHotelsByResort',
    data: ({resort_id: resort_sel.value}),
    dataType: "json",
    success: function (data, textStatus) {// Обновляем форму
      $("#"+form_id+" .result").val("default");
      $("#"+form_id+" .hotels").html('<option class="null" value="0">Любой</option>');
      $.each(data['hotel'], function(i, val) { 
        $("#"+form_id+" .hotels").append('<option value="' + i + '">' + val + '</option>');
      });
    },
    error: function () {
      alert('Возникла непредвиденная ошибка\nПопробуйте повторить попытку, либо обновите страницу.');
    }
  });
}
function hotelHasChoosed(hotel_sel, form_id)
{
  if (parseInt(hotel_sel.value))
    $("#"+form_id+" .result").val("by_hotel");
  else
    $("#"+form_id+" .result").val("default");    
}
function showBlockInfo(link)
{
  if ($("#tourBlockInfo").css("display") == "none") {
    $("#tourBlockInfo").slideDown("slow");
    $(link).html("Скрыть дополнительную информацию").next().attr("src", "/imgs/hide_arrow.png");
  } else {
    $("#tourBlockInfo").slideUp("slow");
    $(link).html("Показать дополнительную информацию").next().attr("src", "/imgs/show_arrow.png");
  }
}
function closeSpecWindow(name)
{
  $("#"+name).slideUp(); //css("display", "none");
}
// </evgen>
