﻿function UpdateOnline() {
    SendUpdateOnline();
}
function SendUpdateOnline() {
    $.getJSON("/online/", function(data) { });
    setTimeout(UpdateOnline, 60000);
}


function ScorePosts(e, number) {
    var parent = e.parent();
    var postId = parent.attr("id").substring(5);


    $.getJSON("/vote/" + postId + "/" + number,
        function(data) {

            if (!data.Error) {
                $(".rating-score", parent.parent()).text(data.newRating);
                parent.empty();
            } 
        });
    }

function DeleteNotice(e) {
 
    var parent = e.parent();
    var noticeId = parent.attr("id").substring(6);
    $.getJSON("/deletenotice/" + noticeId,
        function(data) { });
    parent.fadeOut("slow");
    if ($(".notifications > :visible").size() == 1) {
        $(".notifications").next().remove();
    }
}

$().ready(function() {
    $("#CloseGetWeight").click(function() {
        $.getJSON("/closeweight", function(data) {
            if (!data.Error) {
                $("#CloseGetWeight").parent().next().remove();
                $("#CloseGetWeight").parent().fadeOut("slow");
            }
        });
    });

    var sliderObj = $("#slider");
    if (typeof (sliderObj) == "object") {
        sliderObj.slider({
            value: 60,
            max: 130,
            min: 40,
            step: 0.5,
            slide: function(event, ui) {
                $("#value-weight").text(ui.value);
            }
        });
    }
    $("#SaveWeight").click(function() {
        var value = $("#slider").slider('option', 'value');
        $.get("/saveweight", { weight: value });
        $("#CloseGetWeight").parent().next().remove();
        $("#CloseGetWeight").parent().fadeOut("slow");
    });
    $(".notice-delete").click(function() { DeleteNotice($(this)); });

    $(".delete").click(function() { return confirm('Точно удалить?'); });

    $(".vote1").click(function() { ScorePosts($(this), 1); });
    $(".vote2").click(function() { ScorePosts($(this), 2); });
    $(".vote3").click(function() { ScorePosts($(this), 3); });

    $(".vote1").mouseenter(function() {
        $(this).addClass("selected");
    });
    $(".vote2").mouseenter(function() {
        $(this).addClass("selected");
        $(this).prev().addClass("selected");
    });
    $(".vote3").mouseenter(function() {
        $(this).addClass("selected");
        $(this).prev().addClass("selected");
        $(this).prev().prev().addClass("selected");
    });

    $(".add-rating > div").mouseleave(function() {
        $(".add-rating > div").removeClass("selected");
    });

    $(".main-menu .menu-item").click(function() {
        var href = $("a", $(this)).attr("href");
        window.location = href;
    });

    SendUpdateOnline();


});


