

(function () {
    window.uitools.addToolButton('righttoolbuttons', 'randomAlbumPlay' /* icon */ , function () {

        app.db.getQueryResultAsync('SELECT id FROM albums ORDER BY RANDOM() LIMIT 1').then(function(results) {
            if (!results.eof) {
                var id = results.fields.getValue(0);
                app.getObject('album', {
                    id: parseInt(id)
                }).then(function(album) {
                    if(album) {
                        album.getTracklist().whenLoaded().then(function(tracks) {
                            app.player.addTracksAsync(tracks, {
                                withClear: true,
                                startPlayback: true
                            });
                        });
                    }
                });
            }
        });
    }, 'Play random album');
})();
