// by using named object, we can see the labels in the stack trace (...but I should test that too!)
var playlistControl = (function () {
  var defaultAjax = {
    url: '/api/playlists/',
    type: 'post',
    dataType: 'json',
    error: function () {
      //console.log('ajax error');
      //console.log(arguments);
    }
  };
  
  return {
    remove: function (playlistId, songId, position, callback) {
      $.ajax($.extend({}, defaultAjax, {
        url: '/api/playlists/' + playlistId + '/song/' + songId + '/',
        data: { _action: 'remove', song_position: position },
        success: callback
      }));
    },
    add: function (playlistId, songId, callback) {
      $.ajax($.extend({}, defaultAjax, {
        url: '/api/playlists/' + playlistId + '/',
        data: { song_id : songId },
        success: callback
      }));
    },
    create: function (name, songId, callback) { // songId is optional
      var data = {};
      if (name) {
        data.name = name;
      }
      if (songId) {
        data.song_id = songId;
      }
      
      $.ajax($.extend({}, defaultAjax, {
        data: data,
        success: callback
      }));
    },
    rename: function (playlistId, name, callback) {
      $.ajax($.extend({}, defaultAjax, {
        url: '/api/playlists/' + playlistId + '/',
        data: { name : name },
        success: callback
      }));      
    },
    get: function (callback) {
      $.ajax($.extend({}, defaultAjax, {
        type: 'get',
        success: callback
      }));
    },
    move: function (playlistId, playlist, callback) {
      $.ajax($.extend({}, defaultAjax, {
        url: '/api/playlists/' + playlistId + '/',
        data: { playlist_json: playlist },
        success: callback
      }));
    }
  };
})();