         timeouter = new RetryTimeouter()
         playerInstance = new Player playerElement, preloadElement, localStorage, timeouter, playbackType
         songQueue = new SongQueue localStorage
+        preloader = new Preloader songQueue, playbackType, PRELOAD_COUNT
+        songQueue.on "queueChange", preloader.updatePreload
 
         PlayerView playerContainer, playerInstance, songQueue
 


        updateAll: (@allSongs) =>
                @trigger "updateAll", @allSongs

        updateVisible: (newVisibleSongs) ->
                if @visibleSongs == newVisibleSongs

                shuffled[insert_position + swap_position] = shuffled[insert_position]
                shuffled[insert_position] = swappable
        return shuffled


class Preloader
        constructor: (@queue, @playbackType, @preloadCount) ->
                @preloaded = {}
                @nextPreloads = []
                @ongoingPreloads = {}

        _preloadSong: =>
                if @nextPreloads.length == 0
                        return
                if _.keys(@ongoingPreloads).length != 0
                        return
                song = @nextPreloads.shift()
                encodedPath = encodeURIComponent song.filename
                preloadSource = "preload/#{encodedPath}?type=#{@playbackType.request}"
                @ongoingPreloads[song.filename] = new Date()
                request = $.ajax preloadSource,
                        type: "GET"
                        timeout: PRELOAD_WAIT_TIMEOUT
                preloadCallback = =>
                        @preloaded[song.filename] = new Date()
                        delete @ongoingPreloads[song.filename]
                        if @nextPreloads.length == 0
                                return
                        nextSong = @nextPreloads.shift()
                        @_preloadSong nextSong
                request.error preloadCallback
                request.success preloadCallback

        updatePreload: =>
                allSongs = @queue.playbackQueue()
                preloadSongs = allSongs[0..].reverse()
                @nextPreloads = []
                totalPreloads = 0
                for song in preloadSongs
                        if totalPreloads == @preloadCount
                                break
                        if song.filename in @preloaded
                                continue
                        totalPreloads++
                        @nextPreloads.push song
                @_preloadSong()


class SongQueue
