﻿

/** MONITOR **/



var currentSong;
var nextSong;
var positionTimer;


//Set monitor checked if cookie is set




function updateMonitor() {

    try {

        if (room) {
            $.ajax({
                url: '/Room/getCurrentSong',
                dataType: 'json',
                data: { room: room },
                traditional: true,
                type: 'POST',
                success: updateCurrentSongCallBack  // get data which are already deserialized from JSON    
            });

        }
    } catch (e) {
    }

}



function updateCurrentSongCallBack(song) {
    if (song && (!currentSong || currentSong.MbId != song.MbId)) {
        currentSong = song;


        if ($("#monitorLocally").is(':checked')) 
            playSpotify(song);

        var notificationText = 'Playing : ' + song.Artist + (song.Title ? " - " + song.Title : "")
            + ', added by: ' + song.PlayedBy.UserName;

        showNotification(notificationText);

        if (typeof(updatePlaylist) != "undefined" )
            updatePlaylist();

    }

    if (song) {
        $('#activeRoom').html(room);
        $('#activeRoom').attr("href", "/" + room);

        $('#monitor').removeClass("disabled");
        $('#monitor input').show();
        document.getElementById('currentSong').innerHTML = "<span title='Added by " + song.PlayedBy.UserName + "'><a href='/Artists/" + song.Artist + "'>" + song.Artist + "</a>" + (song.Title ? " " + song.Title : "") + "</span> ";
        document.getElementById('coverArt').src = '/Content/Images/Artists/' + (song.Artist.split(' ').join('_')) + '-40x40.png';

        if (song.Played) {

            clearInterval(positionTimer);
            positionTimer = setInterval(function () {

                var played = eval(song.Played.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
                var diff = new Date().getTime() - played.getTime();

                song.position = new Date(diff);
                song.position.setTime(diff + 1000); // average one second lag

                /*var percentPlayed = song.position.getTime() / song.Length.TotalMilliseconds;
                $(".playlist .past a.artist:first-child").css("opacity", percentPlayed);
                */

                if(song.position)
                    document.getElementById('currentPosition').innerHTML = (song.position.getMinutes()) + ':' + addLeadingZero(song.position.getSeconds());

            }, 1000);
        }

        $("#currentVote").show();

        if (typeof (updateHistory) != "undefined")
            updateHistory();

    } else {
        currentSong = null;
        document.getElementById('currentSong').innerHTML = "";
        $('#monitor').addClass("disabled");
        $("#currentVote").hide();
        $('#monitor input').hide();
    }

    if (!!song) {
        var canSkip = (song.PlayedBy.UserName.startsWith(user) || song.PlayedBy.UserName.startsWith('MAESTRO'));
        //MyApp.updateJumpList(canSkip);
    } else {
        //MyApp.clearJumpList();
    }
}




/* INIT */
$(document).ready(function () {
    socket.on('onSongStarted', updateCurrentSongCallBack);
    socket.on('onVolumeChange', function (data) {
        $('#volume').val(data.volume);
    });
    
    updateMonitor();



    //If the user checks the monitor checkbox refresh the playlist and play the song in spotify
    $("#monitorLocally").click(function () {
        if ($("#monitorLocally").is(":checked"))
            $.cookie('monitor', true, { expires: 7, path: '/' });
        else
            $.cookie('monitor', null);

        if ($("#monitorLocally").is(":checked")) {
            playSpotify(currentSong);
        }
    });

    if ($.cookie('monitor'))
        $("#monitorLocally").attr('checked', 'checked')

});

