(function(context, $) {

  "use strict";

  context.JK = context.JK || {};
  context.JK.FeedItemSessionTimer = null;
  context.JK.FeedItemSession = function($parentElement, options){

    var ui = new context.JK.UIHelper(JK.app);

    var $feedItem       = $parentElement;
    var $description    = $('.description', $feedItem)
    var $musicians      = $('.musician-detail', $feedItem)
    var $controls       = $('.session-controls', $feedItem);
    var $status         = $('.session-status', $feedItem);
    var playing         = false;
    var toggledOpen     = false;
    var musicSessionId  = $feedItem.attr('data-music-session');

    if(!$feedItem.is('.feed-entry')) {
      throw "$parentElement must be a .feed-entry"
    }

    function startPlay() {
      var img = $('.play-icon', $feedItem);
      img.attr('src', '/assets/content/icon_pausebutton.png');
      $controls.trigger('play.listenBroadcast');
      playing = true;
    }

    function stopPlay() {
      var img = $('.play-icon', $feedItem);
      img.attr('src', '/assets/content/icon_playbutton.png');
      $controls.trigger('pause.listenBroadcast');
      playing = false;
    }

    function togglePlay() {
      if(playing) {
        $status.text('SESSION IN PROGRESS');
        stopPlay();
      }
      else {
        startPlay();
      }
      return false;
    }

    function toggleDetails() {
      if(toggledOpen) {
        $feedItem.css('height', $feedItem.height() + 'px')
        $feedItem.animate({'height': $feedItem.data('original-max-height')}).promise().done(function() {
          $feedItem.css('height', 'auto').css('max-height', $feedItem.data('original-max-height'));

          $musicians.hide();
          $description.css('height', $description.data('original-height'));
          $description.dotdotdot();
        });
      }
      else {
        $description.trigger('destroy.dot');
        $description.data('original-height', $description.css('height')).css('height', 'auto');
        $musicians.show();
        $feedItem.animate({'max-height': '1000px'});
      }

      toggledOpen = !toggledOpen;

      return false;
    }

    function stateChange(e, data) {
      if(data.displayText) $status.text(data.displayText);

      if(data.isEnd) stopPlay();

      if(data.isSessionOver) {
        $controls.removeClass('inprogress').addClass('ended')
      }
    }

    function events() {
      $('.details', $feedItem).click(toggleDetails);
      $('.details-arrow', $feedItem).click(toggleDetails);
      $('.play-button', $feedItem).click(togglePlay);

      $('.btn-share', $feedItem).click(function() {
        ui.launchShareDialog(musicSessionId, 'session');
      });
      
      $('.btn-comment', $feedItem).click(function() {
        ui.launchCommentDialog({
          session_id: musicSessionId,
          entity_type: 'session'
        });
      });

      $('.btn-like', $feedItem).click(function() {
        ui.addSessionLike(musicSessionId, JK.currentUserId, $('.likes', $feedItem), $('.btn-like', $feedItem))
      });

      $controls.bind('statechange.listenBroadcast', stateChange);
    }

    function initialize() {
      $('.timeago', $feedItem).timeago();
      $('.dotdotdot', $feedItem).dotdotdot();
      $controls.listenBroadcast();
      context.JK.prettyPrintElements($('time.duration', $feedItem).show());
      context.JK.setInstrumentAssetPath($('.instrument-icon', $feedItem));

      $feedItem.data('original-max-height', $feedItem.css('height'));

      events();

      context.JK.bindHoverEvents($feedItem);
      //context.JK.bindProfileClickEvents($feedItem);
    }

    initialize();

    return this;
  }
})(window, jQuery);