﻿function searchSuccess() {
    $("#xjk4V").html($("#qsI").val() + " - хайлтын үр дүн");
    $('.feature_box').showFeatureText();
}

function showLyrics() {    
    $('.scroll-pane').jScrollPane();
}

function editMyPlayListSuccess() {
    showNotify("Жагсаалтыг амжилттай хадгаллаа.");
}


function playListSaved() {
    $("#svDlgCPl4").dialog("close");
    showNotify("Жагсаалтыг амжилттай хадгаллаа.");
}
function musicSavedToPlaylist() {
    $("#svDlgCPl4").dialog("close");
    showNotify("Дууг жагсаалтад амжилттай орууллаа.");
}

function playListAdded(response, status, data) {
    $("#txtPlayListName").val("");
    $("#qsI").val("");
    $("#mV4H3kNc").empty();
    var data = '<tr id="jilK5v' + response.autoId + '"><td><a href="' + response.url + '">' + response.name + '</a></td><td>' + response.trackCount + '</td><td><input class="lkij5nc" id="xsr3n' + response.autoId + '" checked bool="true" playList="' + response.autoId + '" type="checkbox" data-on="Тийм" data-off="Үгүй" /></td><td><a href="/music/playlist/edit/' + response.autoId + '"><span class="icEdit15"></span></a><a class="ji3m3vnB rmvLbt6" title="' + response.name + '" playList="' + response.autoId + '">x</a></td></tr>';
    $(data).insertAfter('#mdjH5Lmc2');
    $('#xsr3n' + response.autoId).tzCheckbox();
    $("#jilK5v" + response.autoId).effect("highlight", {}, 5000);
}

function searchSuccessJson(response, status, data) {
    var items = response;    
    $.each(items, function (index, item) {
        var title = item.artist + " - " + item.title;
        var data = '<li id="njkt'+item.autoId+'" class="pladdedm" songid="' + item.autoId + '" title="' + title + '">' + title + '<a id="" onclick="removeMeFromPlayList('+item.autoId+')"  class="rmvLbt6 rfloat"><span>x</span></a></li>';
        $(data).appendTo('#mV4H3kNc');

        $("#njkt" + item.autoId).effect("highlight", {}, 3000);

    });
    saveNewPlayList();
}

function removeMeFromPlayList(autoId) {
    $("#njkt" + autoId).remove();
    saveNewPlayList();
}

function saveNewPlayList() {
    var newPlayList = $('#mV4H3kNc');
    var ids = "";
    newPlayList.children('li').each(function (index, item) {
        var trackID = $(this).attr('songId');
        ids = ids + trackID + ",";
    });

    ids = ids.substr(0, ids.length - 1);
    $("#hAuIdPls4").val(ids);    
}


$(document).ready(function () {

    $('.sdlrBt3X').live("click", function () {
        var songId = $(this).attr('song');
        var title = $(this).attr('title');
        $('#svDlgCPl4').dialog({
            bgiframe: true,
            autoOpen: false,
            width: 490,
            modal: true,
            title: title,
            open: function (event, ui) {
                $(this).load("/music/showSendTrackLyrics?autoId=" + songId, function () {
                });
            },
            overlay: {
                backgroundColor: '#FFFFFF',
                opacity: 0.5
            },
            buttons: {
                "Хадгалах": function () {
                    $('#FkilF4m').submit();
                    $(this).dialog("close");
                },
                "Болих": function () {
                    $(this).dialog("close");
                }
            }
        });
        $('#svDlgCPl4').dialog('open');
    });

    $('.lkij5nc').live("click", function () {
        var playListId = $(this).attr('playList');
        var checked = $(this).attr("bool")

        $.ajax({
            type: "GET",
            url: "/music/changePlayListPublic/?autoId=" + playListId + "&isPublic=" + checked,
            dataType: "json",
            async: false,
            success: function (msg) {
                showNotify(msg);
            }
        });
    });
    $('.ji3m3vnB').live("click", function () {
        var playListId = $(this).attr('playList');
        var title = $(this).attr('title');

        var elem = $("#jilK5v" + playListId);

        $.confirm({
            'title': 'Зөвшөөрөл',
            'message': 'Та ' + title + ' дууны жагсаалтыг устгах гэж байна уу',
            'buttons': {
                'Тийм': {
                    'class': 'blue',
                    'action': function () {


                        $.ajax({
                            type: "GET",
                            url: "/music/deletePlayList/?autoId=" + playListId,
                            dataType: "text",
                            async: false,
                            success: function (response) {
                                if (response == "ok") {
                                    elem.slideUp();
                                }
                                else {
                                    showNotify(response);
                                }
                            }
                        });

                        //elem.remove();
                    }
                },
                'Үгүй': {
                    'class': 'gray',
                    'action': function () { } // Nothing to do in this case. You can as well omit the action property.
                }
            }
        });
    });

    $('.afkM5kg').live("click", function () {
        if (checkLogin()) {
            var songId = $(this).attr('song');
            var title = $(this).attr('title');
            $('#svDlgCPl4').dialog({
                bgiframe: true,
                autoOpen: false,
                width: 490,
                modal: true,
                title: title,
                open: function (event, ui) {
                    $(this).load("/music/showAddMusicToPlayList?songId=" + songId, function () {
                    });
                },
                overlay: {
                    backgroundColor: '#FFFFFF',
                    opacity: 0.5
                },
                buttons: {
                    "Хадгалах": function () {
                        $('#mk2BdR2').submit();
                        $(this).dialog("close");
                    },
                    "Болих": function () {
                        $(this).dialog("close");
                    }
                }
            });
            $('#svDlgCPl4').dialog('open');
        }
    });
});
