function cleanTempPlayList() {
    $(".tabs-pl .plstemp").empty()
}

function cleanActiveClasses() {
    $("div,a").removeClass("playlist-item-play").removeClass("play-active")
}

function createPlaySong(a) {
    if ($(".playlist-item-play").length) var e = $(".playlist-item-play").parent();
    else e = $(".playlist-item:first").parent();
    $(".audioplayer").remove(), cleanActiveClasses(), e.find(".playlist-item").eq(a.index()).addClass("playlist-item-play");
    var t = a.find(".ms-author").text(),
        i = a.find(".ms-song").text(),
        l = a.find(".ms-image img").attr("src"),
        s = a.find(".ms-save-song").data("mp3");
    $(".main-player").append('<audio preload="auto" class="play-song"><source src="' + s + '" /></audio>'), $(".main-player").find("audio").audioPlayer(), "undefined" != l && $(".mplayer-image").append('<img data-src="' + l + '" alt="" />'), $(".main-player .mplayer-title u").append(t), $(".main-player .mplayer-title i").append(i), $(window).width() <= "1250" && $(".player-wrap").addClass("miniplayer")
}

function loadPlayList() {
    var a = $(".favlink").attr("href");
    $.ajax({
        url: a,
        type: "get",
        cache: !0,
        success: function(a) {
            $($(a).find(".main-news")).each(function() {
                var a = $(this).find(".main-news-title").attr("href"),
                    e = $(this).find(".main-news-author").text(),
                    t = $(this).find(".main-news-song").text(),
                    i = $(this).find(".ms-save-song").data("mp3"),
                    l = $(this).find(".ms-image img").attr("src");
                $(".tabs-pl .plstab").append('<a href="' + a + '" class="playlist-item"><u class="ms-author">' + e + '</u><i class="ms-song">' + t + '</i><span class="ms-save-song" data-mp3="' + i + '"></span><span class="ms-image" style="display: none;"></span></a>')
            })
        },
        complete: function(a) {
            $(".tabs-pl .tt-panel").mCustomScrollbar()
        }
    })
}

function addTempPlayList(a, e) {
    cleanTempPlayList(), $(a.find(e)).each(function() {
        var a = $(this).find(".ms-link").data("link"),
            e = $(this).find(".ms-author").text(),
            t = $(this).find(".ms-song").text(),
            i = $(this).find(".ms-save-song").data("mp3"),
            l = $(this).find(".ms-image img").attr("src");
        $(".tabs-pl .plstemp").append('<a href="' + a + '" class="playlist-item"><u class="ms-author">' + e + '</u><i class="ms-song">' + t + '</i><span class="ms-save-song" data-mp3="' + i + '"></span><span class="ms-image" style="display: none;"></span></a>')
    })
}

function showPlay() {
    $(".main-player .audioplayer").removeClass("audioplayer-stopped").addClass("audioplayer-playing");
    $('#js-player-wrap').removeClass('player--hide');
}

function playNext() {
    if ($(".playlist-item-play").length) var a = $(".playlist-item-play"),
        e = $(".playlist-item-play").parent();
    else a = $(".playlist-item:first"), e = $(".playlist-item:first").parent();
    if (0 == playRandom) {
        createPlaySong(t = a.next(".playlist-item").length ? a.next(".playlist-item") : e.find(".playlist-item:first")), t.addClass("playlist-item-play"), $(".main-player").find("audio").get(0).play(), showPlay()
    } else {
        var t, i = e.find(".playlist-item").length,
            l = Math.floor(1 + Math.random() * (i + 1 - 1));
        createPlaySong(t = e.find(".playlist-item").eq(l - 1)), t.addClass("playlist-item-play"), $(".main-player").find("audio").get(0).play(), showPlay()
    }
}

function playPrev() {
    if ($(".playlist-item-play").length) var a = $(".playlist-item-play"),
        e = $(".playlist-item-play").parent();
    else a = $(".playlist-item:first"), e = $(".playlist-item:first").parent();
    var t = a.prev(".playlist-item").length ? a.prev(".playlist-item") : e.find(".playlist-item:last");
    createPlaySong(t), t.addClass("playlist-item-play"), $(".main-player").find("audio").get(0).play(), showPlay()
}

function showStatusWin(a) {
    $("body").append('<div class="status-win">' + a + "</div>"), $(".status-win").fadeIn(400).delay(2e3).fadeOut(400, function() {
        $(".status-win").remove()
    })
}
playRepeat = !1, playRandom = !1,
    function(a, e, t, i) {
        var l = "ontouchstart" in e,
            s = l ? "touchstart" : "mousedown",
            n = l ? "touchmove" : "mousemove",
            o = l ? "touchcancel" : "mouseup",
            p = function(a) {
                var e = a / 3600,
                    t = Math.floor(e),
                    i = a % 3600 / 60,
                    l = Math.floor(i),
                    s = Math.ceil(a % 3600 % 60);
                return s > 59 && (s = 0, l = Math.ceil(i)), l > 59 && (l = 0, t = Math.ceil(e)), (0 == t ? "" : t > 0 && t.toString().length < 2 ? "0" + t + ":" : t + ":") + (l.toString().length < 2 ? "0" + l : l) + ":" + (s.toString().length < 2 ? "0" + s : s)
            },
            d = function(a) {
                var e = t.createElement("audio");
                return !(!e.canPlayType || !e.canPlayType("audio/" + a.split(".").pop().toLowerCase() + ";").replace(/no/, ""))
            };
        a.fn.audioPlayer = function(e) {
            e = a.extend({
                classPrefix: "audioplayer",
                strPlay: "Play",
                strPause: "Pause",
                strVolume: "Volume"
            }, e);
            var t = {},
                i = {
                    playPause: "playpause",
                    playing: "playing",
                    stopped: "stopped",
                    time: "time",
                    timeCurrent: "time-current",
                    timeDuration: "time-duration",
                    bar: "bar",
                    barLoaded: "bar-loaded",
                    barPlayed: "bar-played",
                    volume: "volume",
                    volumeButton: "volume-button",
                    volumeAdjust: "volume-adjust",
                    noVolume: "novolume",
                    muted: "muted",
                    mini: "mini"
                };
            for (var r in i) t[r] = e.classPrefix + "-" + i[r];
            return this.each(function() {
                if ("audio" != a(this).prop("tagName").toLowerCase()) return !1;
                var i = a(this),
                    r = i.attr("src"),
                    m = "" === (m = i.get(0).getAttribute("autoplay")) || "autoplay" === m,
                    u = "" === (u = i.get(0).getAttribute("loop")) || "loop" === u,
                    y = !1;
                void 0 === r ? i.find("source").each(function() {
                    if (void 0 !== (r = a(this).attr("src")) && d(r)) return y = !0, !1
                }) : d(r) && (y = !0);
                var c = a('<div class="' + e.classPrefix + '">' + (y ? a("<div>").append(i.eq(0).clone()).html() : '<embed src="' + r + '" width="0" height="0" volume="100" autostart="' + m.toString() + '" loop="' + u.toString() + '" />') + '<div class="mplayer-image"></div><div class="mplayer-nav"><div class="mplayer-prev"></div><div class="mplayer-pp ' + t.playPause + '" title="' + e.strPlay + '" rel="' + e.strPlay + '"></div><div class="mplayer-next"></div></div>'),
                    f = (f = y ? c.find("audio") : c.find("embed")).get(0);
                if (y) {
                    c.find("audio").css({
                        width: 0,
                        height: 0,
                        visibility: "hidden"
                    }), c.append('<div class="mplayer-center"><div class="mplayer-title"><u></u><i></i></div><div class="mplayer-line"><div class="mplayer-line-left ' + t.time + " " + t.timeCurrent + '"></div><div class="' + t.bar + '"><div class="' + t.barLoaded + '"></div><div class="' + t.barPlayed + '"></div></div><div class="mplayer-line-right ' + t.time + " " + t.timeDuration + '"></div></div></div><div class="mplayer-right"><div class="' + t.volume + '"><div class="' + t.volumeButton + '" title="' + e.strVolume + '" title="Громкость"><a href="#"></a>Громкость <i>100%</i></div><div class="' + t.volumeAdjust + '"><div><div></div></div></div></div></div>');
                    var v = c.find("." + t.bar),
                        h = c.find("." + t.barPlayed),
                        $ = c.find("." + t.barLoaded),
                        g = c.find("." + t.timeCurrent),
                        b = c.find("." + t.timeDuration),
                        C = c.find("." + t.volumeButton),
                        w = c.find("." + t.volumeAdjust + " > div"),
                        P = 0,
                        x = function(a) {
                            theRealEvent = l ? a.originalEvent.touches[0] : a, f.currentTime = Math.round(f.duration * (theRealEvent.pageX - v.offset().left) / v.width())
                        },
                        k = function(a) {
                            theRealEvent = l ? a.originalEvent.touches[0] : a, f.volume = Math.abs((theRealEvent.pageX - (w.offset().left + w.width())) / w.width()), f.volume = Math.abs(1 - f.volume)
                        },
                        S = f.volume,
                        L = f.volume = .111;
                    Math.round(1e3 * f.volume) / 1e3 == L ? f.volume = S : c.addClass(t.noVolume), b.html("&hellip;"), g.html(p(0)), f.addEventListener("loadeddata", function() {
                        var e;
                        e = setInterval(function() {
                            if (f.buffered.length < 1) return !0;
                            $.width(f.buffered.end(0) / f.duration * 100 + "%"), Math.floor(f.buffered.end(0)) >= Math.floor(f.duration) && clearInterval(e)
                        }, 100), b.html(a.isNumeric(f.duration) ? p(f.duration) : "&hellip;"), w.find("div").width(100 * f.volume + "%"), P = f.volume
                    }), f.addEventListener("timeupdate", function() {
                        g.html(p(f.currentTime)), h.width(f.currentTime / f.duration * 100 + "%")
                    }), a("body").on("click", w, function() {
                        w.find("div").width(100 * f.volume + "%"), f.volume > 0 && c.hasClass(t.muted) && c.removeClass(t.muted), f.volume <= 0 && !c.hasClass(t.muted) && c.addClass(t.muted), c.find(".audioplayer-volume-button i").empty().append((100 * f.volume).toFixed(0) + "%")
                    }), f.addEventListener("ended", function() {
                        c.removeClass(t.playing).addClass(t.stopped), a("div").removeClass("play-active"), 0 == playRepeat ? playNext() : (a(".main-player").find("audio").get(0).play(), a(".main-player .audioplayer").removeClass("audioplayer-stopped").addClass("audioplayer-playing"))
                    }), v.on(s, function(a) {
                        x(a), v.on(n, function(a) {
                            x(a)
                        })
                    }).on(o, function() {
                        v.unbind(n)
                    }), C.on("click", function() {
                        return c.hasClass(t.muted) ? (c.removeClass(t.muted), f.volume = P) : (c.addClass(t.muted), P = f.volume, f.volume = 0), !1
                    }), w.on(s, function(a) {
                        k(a), w.on(n, function(a) {
                            k(a)
                        })
                    }).on(o, function() {
                        w.unbind(n)
                    })
                } else c.addClass(t.mini);
                c.addClass(m ? t.playing : t.stopped), c.find("." + t.playPause).on("click", function() {
                    a(this).parent().parent().attr("class");
                    return c.hasClass(t.playing) ? (a(this).attr("title", e.strPlay).find("a").html(e.strPlay), c.removeClass(t.playing).addClass(t.stopped), y ? f.pause() : f.Stop()) : (a(this).attr("title", e.strPause).find("a").html(e.strPause), c.addClass(t.playing).removeClass(t.stopped), y ? f.play() : f.Play()), !1
                }), i.replaceWith(c)
            }), this
        }
    }(jQuery, window, document), $(document).ready(function() {
    addTempPlayList($(".stm-block-content"), $(".main-news")), $(".playlist-item:first").addClass("playlist-item-play"), createPlaySong($(".main-news:first")), loadPlayList(), $("body").on("click", ".mplayer-next", function() {
        playNext()
    }), $("body").on("click", ".mplayer-prev", function() {
        playPrev()
    }), $("body").on("click", ".mplayer-menu-rep", function() {
        playRandom = !1, playRepeat = !playRepeat, $(".mplayer-menu-rnd").removeClass("mplayer-menu-active"), $(this).toggleClass("mplayer-menu-active")
    }), $("body").on("click", ".mplayer-menu-rnd", function() {
        playRepeat = !1, playRandom = !playRandom, $(".mplayer-menu-rep").removeClass("mplayer-menu-active"), $(this).toggleClass("mplayer-menu-active")
    }), $("body").on("click", ".playlist-item", function() {
        cleanActiveClasses();
        var a = $(this);
        return createPlaySong(a), cleanActiveClasses(), a.addClass("playlist-item-play"), $(".main-player").find("audio").get(0).play(), showPlay(), $(".mobplaeyr .audioplayer").addClass("audioplayer-opacity"), !1
    }), $("body").on("click", ".slider-item-image", function() {
        return cleanActiveClasses(), addTempPlayList($(".sld1 .swiper-wrapper"), $(".slider-item")), createPlaySong($(this).parent()), $(".main-player").find("audio").get(0).play(), showPlay(), !1
    }), $("body").on("click", ".main-news-play", function() {
        addTempPlayList($(".ajax-content"), $(".main-news")), createPlaySong($(this).closest('.main-news')), $(".main-player").find("audio").get(0).play(), $("div").removeClass("play-active"), $(this).closest('.main-news').toggleClass("play-active"), showPlay()
    }), $("body").on("click", ".full-news-listen a", function() {
        return createPlaySong($(this)), $(".main-player").find("audio").get(0).play(), showPlay(), !1
    }), $("body").on("click", ".main-news-pause", function() {
        $(".main-player").find("audio").get(0).pause(), $("div").removeClass("play-active")
    }), $("body").on("click", ".mplayer-menu-playlist, .player-popup-close", function() {
        $(".player-popup")._toggleClass('type--drop-open'), $(".audioplayer, .mobplaeyr-close").toggleClass("audioplayer-opacity")
    }), $("body").on("click", ".mob-playr-open, .mobplaeyr-close", function() {
        $(".player-wrap").toggleClass("mobplaeyr")
    }), $("body").on("click", ".mplayer-menu-zoom1", function() {
        $(this).toggleClass("mplayer-menu-active"), $(".player-wrap").toggleClass("miniplayer")
    }), $("body").on("click", ".fav-js", function() {
        if ($(".plstab .playlist-item-play").length) var a = $(".playlist-item-play .ms-save-song").data("mp3");
        $(".plstab").empty();
        var e = $(".favlink").attr("href");
        $.ajax({
            url: e,
            type: "get",
            cache: !0,
            success: function(a) {
                $($(a).find(".main-news")).each(function() {
                    var a = $(this).find(".main-news-title").attr("href"),
                        e = $(this).find(".main-news-author").text(),
                        t = $(this).find(".main-news-song").text(),
                        i = $(this).find(".ms-save-song").data("mp3");
                    $(".tabs-pl .plstab").append('<a href="' + a + '" class="playlist-item"><u class="ms-author">' + e + '</u><i class="ms-song">' + t + '</i><span class="ms-save-song" data-mp3="' + i + '"></span></a>')
                })
            },
            complete: function(e) {
                void 0 !== a && $(".plstab").find('.ms-save-song[data-mp3="' + a + '"]').parent().addClass("playlist-item-play")
            }
        })
    }), $("body").on("click", ".singer-play", function() {
        $(".plstemp").empty();
        var a = $(this).parent().find(".singer-track a").attr("href");
        "undefined" != a && $.ajax({
            url: a,
            type: "get",
            cache: !0,
            success: function(a) {
                $(".loading-popup").addClass("loading-content"), $($(a).find('div[data-cat="song"]')).each(function() {
                    var a = $(this).find(".main-news-title").attr("href"),
                        e = $(this).find(".main-news-author").text(),
                        t = $(this).find(".main-news-song").text(),
                        i = $(this).find(".ms-save-song").data("mp3");
                    $(".tabs-pl .plstemp").append('<a href="' + a + '" class="playlist-item"><u class="ms-author">' + e + '</u><i class="ms-song">' + t + '</i><span class="ms-save-song" data-mp3="' + i + '"></span></a>')
                })
            },
            complete: function(a) {
                createPlaySong($(".plstemp .playlist-item:first")), $(".main-player").find("audio").get(0).play(), cleanActiveClasses(), $(".playlist-item:first").addClass("playlist-item-play"), showPlay(), $(".loading-popup").removeClass("loading-content"), showStatusWin("Треки загружены в плейлист")
            }
        })
    }), $("body").on("click", ".albums-play", function() {
        $(".plstemp").empty();
        var a = $(this).parent().find(".albums-title a").attr("href");
        "undefined" != a && $.ajax({
            url: a,
            type: "get",
            cache: !0,
            success: function(a) {
                $(".loading-popup").addClass("loading-content"), $($(a).find('div[data-cat="song"]')).each(function() {
                    var a = $(this).find(".main-news-title").attr("href"),
                        e = $(this).find(".main-news-author").text(),
                        t = $(this).find(".main-news-song").text(),
                        i = $(this).find(".ms-save-song").data("mp3");
                    $(".tabs-pl .plstemp").append('<a href="' + a + '" class="playlist-item"><u class="ms-author">' + e + '</u><i class="ms-song">' + t + '</i><span class="ms-save-song" data-mp3="' + i + '"></span></a>')
                })
            },
            complete: function(a) {
                createPlaySong($(".plstemp .playlist-item:first")), $(".main-player").find("audio").get(0).play(), cleanActiveClasses(), $(".playlist-item:first").addClass("playlist-item-play"), showPlay(), $(".loading-popup").removeClass("loading-content"), showStatusWin("Треки загружены в плейлист")
            }
        })
    })
});