﻿



function volume(value) {

    socket.emit('volume', { room: room, volume: value });
    /*
    clearTimeout(volumeTimeout);
    volumeTimeout = window.setTimeout(function () {
    $.ajax({
    url: '/Room/volume',
    data: { value: parseFloat(value) },
    dataType: 'json',
    traditional: true,
    type: 'POST'
    });
    }, 400);*/
}





function checkin(callback) {

    $.ajax({
        url: '/Room/checkin',
        data: { userName: escape(user), facebookId: facebookId, room: room },
        dataType: 'json',
        type: 'POST',
        traditional: true,
        success: function (result) {
            if (callback)
                callback(result);

            socket.emit('checkin', { user: user, room: room });
        }
    });


}

function logout(callback) {
    $.ajax({
        url: '/Room/logout',
        dataType: 'json',
        traditional: true,
        type: 'POST',
        success: function (result) {
            callback(result);

            socket.emit('checkout', { user: user, room: room });

        }
    });
}

function next() {
    $.ajax({
        url: '/Room/next',
        dataType: 'json',
        traditional: true,
        type: 'POST'
    });
}


function vote(mbId, value) {
    $.ajax({
        url: '/Room/vote',
        data: { mbId: mbId, value: parseInt(value) },
        dataType: 'json',
        traditional: true,
        type: 'POST',
        success: function (result) {
            socket.emit('vote', { user: user, mbId: mbId, value: value });
            updatePlaylist(result);  // get data which are already deserialized from JSON
            if (value == 1) {
                silentAlert('You have now blocked this track. Bummer for those who liked it ;)');
                postTrackAction({mbid:mbid}, "block");

            } else {
                silentAlert('Your vote is now registered, it will play more often. Glad you liked it!');
                postTrackAction({ mbid: mbid }, "like");
            }
        }
    });
    //event.preventDefault();

}

function logout(callback) {

    $.ajax({
        url: '/Room/logout',
        dataType: 'json',
        traditional: true,
        type: 'POST',
        success: function (data) {  // get data which are already deserialized from JSON    

            socket.emit('checkout', { room: room, user: user });
            updateMonitor();

            if (callback != null)
                callback();
        }
    });

}



function postCheckin(room) {
    FB.api('/me/maestroo:checkin' +
                            '?room=http://maestroo.com/room/openGraph?room=' + room, 'post',
                            function (response) {
                                console.log(response.error);

                                /* ignore errors for now
                                if (!response || response.error) {
                                silentAlert('Error occured when updating facebook:' + response.error.message);
                                }*/
                            });
}

function postTrackAction(track, action) {

    var trackurl;

    if (track.mbId)
        trackUrl = "http://maestroo.com/tracks/" + track.mbId;
    else
        trackUrl = "http://maestroo.com/artists/" + escape(track.artist) + "/" + escape(track.title);

    FB.api('/me/maestroo:' + action +
                            '?track=' + trackUrl, 'post',
                            function (response) {
                                if (!response || response.error) {
                                    silentAlert('Error occured when updating facebook:' + response.error.message);
                                }
                            });
}




