= javascript_include_tag "download_jamtrack"
= render "clients/download_jamtrack_templates"
= stylesheet_link_tag "client/downloadJamTrack"

- provide(:title, 'Download Jam Track Widget')

.content-wrapper
  h2 Jam Track State Widget

  h3 Possible States
  ul
    li synchronized
    li no_client
    li packaging
    li downloading
    li keying
    li initial
    li errored
  #widget

javascript:
  var initialized = false;
  $(document).on('JAMKAZAM_READY', function(e, data) {
    window.JK.JamServer.get$Server().on(window.JK.EVENTS.CONNECTION_UP, function() {
      if(initialized) {
        return;
      }
      initialized = true

      setTimeout(function() {
        window.downloadJamTrack = new JK.DownloadJamTrack(data.app, {id: gon.jamTrackId, jam_track_right_id: gon.jamTrackRightId, name: 'Back in Black'}, gon.size)
        downloadJamTrack.init()
        $('#widget').append(window.downloadJamTrack.root)

        if (gon.switchState == 'errored') {
          downloadJamTrack.transitionError("package-error", "The server failed to create your package.")
        }
        else if (gon.switchState) {
          downloadJamTrack.transition(downloadJamTrack.states[gon.switchState]);
        }
      }, 1)

    })
  })