$(function() {
    var slider = $("#slider");

    slider.slider({
        range: "max",
        min: 1,
        max: 6,
        value: 3,
        slide: function(event, ui) {
            $("#text_ad").text(getText(ui.value));
            $("#amount").text(ui.value);
            localStorage.setItem("sliderValue", ui.value.toString());
            updateFace(ui.value);
        }
    });

    var savedValue = localStorage.getItem("sliderValue");
    if (savedValue) {
        slider.slider("value", parseInt(savedValue));
        $("#text_ad").text(getText(savedValue));
        $("#amount").text(savedValue);
    }

    // Manuální spuštění funkce updateFace při načítání stránky
    updateFace(slider.slider("value"));

    slider.slider('pips', {
        first: 'pip',
        last: 'pip',
    });

    function updateFace(value) {
        var face = $(".face-wrapper").children();

        switch (value) {
            case 1:
                removeClass();
                $(face).addClass("case1");
                break;
            case 2:
                removeClass();
                $(face).addClass("case2");
                break;
            case 3:
                removeClass();
                $(face).addClass("case3");
                break;
            case 4:
                removeClass();
                $(face).addClass("case4");
                break;
            case 5:
                removeClass();
                $(face).addClass("case5");
                break;
            case 6:
                removeClass();
                $(face).addClass("case6");
                break;
        }
    }

    function removeClass() {
        $(".face-wrapper").children().removeClass("case1 case2 case3 case4 case5 case6");
    }

    function getText(value) {
        switch (parseInt(value)) {
            case 1:
                return "1 test";
            case 2:
                return "2 test";
            case 3:
                return "3 test";
            case 4:
                return "4 test";
            case 5:
                return "5 test";
            case 6:
                return "6 test";
        }
    }
});