
(function (context, $) {

  "use strict";

  context.JK = context.JK || {};
  context.JK.IndividualJamTrackBand = function (app) {

    var rest = context.JK.Rest();
    var logger = context.JK.logger;
    var $page = null;
    var $jamTrackBandInfo = null;
    var $jamTrackNoun = null;
    var $previews = null;
    var $jamTracksButton = null;
    var $jamtrack_band = null;
    var $checkItOut = null;
    var $ctaJamTracksButton = null;

    function fetchJamTrack() {
      rest.getJamTrackWithArtistInfo({plan_code: gon.jam_track_plan_code})
        .done(function (jam_track) {
          logger.debug("jam_track", jam_track)

          $jamtrack_band.text(jam_track.original_artist)
          $jamTracksButton.attr('href', '/client?artist=' + jam_track.original_artist + '#/jamtrack/search')
          $jamTracksButton.removeClass('hidden').text("Preview all " + jam_track.band_jam_track_count + " of our " + jam_track.original_artist + " JamTracks")
          $ctaJamTracksButton.attr('href', '/client?artist=' + jam_track.original_artist + '#/jamtrack/searche')


          context._.each(jam_track.tracks, function (track) {

            if (track.track_type == 'Click') {
              return;
            }

            var $element = $('<div class="jam-track-preview-holder"></div>')

            $previews.append($element);

            new context.JK.JamTrackPreview(app, $element, jam_track, track, {master_shows_duration: false, color:'black', master_adds_line_break:true, preload_master:true})

            if(track.track_type =='Master') {
              context.JK.HelpBubbleHelper.rotateJamTrackLandingBubbles($element.find('.jam-track-preview'), $page.find('.video-wrapper'), $page.find('.cta-free-jamtrack a'), $page.find('a.browse-jamtracks'));
            }
          })

          $previews.append('<br clear = "all" />')
        })
        .fail(function () {
          app.notify({title: 'Unable to fetch JamTrack', text: "Please refresh the page or try again later."})
        })

    }
    function initialize() {

      $page = $('body')
      $jamTrackBandInfo = $page.find('.jamtrack_band_info')
      $previews = $page.find('.previews')
      $jamtrack_band = $page.find('.jamtrack_band')
      $jamTracksButton = $page.find('.browse-jamtracks')
      $ctaJamTracksButton = $page.find('.cta-free-jamtrack');

      context.JK.Tracking.adTrack(app)
      fetchJamTrack();
    }

    this.initialize = initialize;
  }
})(window, jQuery);
