(function(context, $) {

  "use strict";

  // http://img.youtube.com/vi/x-Oas9Sc6s0/hqdefault.jpg
  context.JK = context.JK || {};
  context.JK.FeedItemRecording = function($parentElement, options){

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

    var claimedRecordingId = $parentElement.attr('data-claimed-recording-id');
    var recordingId = $parentElement.attr('id');
    var mode = $parentElement.attr('data-mode');
    var mixInfo = null;

    var $feedItem       = $parentElement;
    var $name           = $('.name', $feedItem);
    var $description    = $('.description', $feedItem);
    var $musicians      = $('.musician-detail', $feedItem);
    var $controls       = $('.recording-controls', $feedItem);
    var $sliderBar      = $('.recording-position', $feedItem);
    var $statusBar      = $('.recording-status', $feedItem);
    var $currentTime    = $('.recording-current', $feedItem);
    var $status         = $('.status-text', $feedItem);
    var $playButton     = $('.play-button', $feedItem);
    var playing         = false;

    var toggledOpen = false;
    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.listenRecording');
      playing = true;
    }

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

    function togglePlay() {

      if(playing) {
        stopPlay();
      }
      else {
        startPlay();
      }
      return false;
    }


    function stateChange(e, data) {
      if(data.isEnd) stopPlay();
      if(data.isError) {
        $sliderBar.hide();
        $playButton.hide();
        $currentTime.hide();
        $statusBar.show();
        $status.text(data.displayText);
      }
    }

    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();
          $name.css('height', $name.data('original-height'));
          $name.dotdotdot();
        });
      }
      else {
        $description.trigger('destroy.dot');
        $description.data('original-height', $description.css('height')).css('height', 'auto');
        $name.trigger('destroy.dot');
        $name.data('original-height', $name.css('height')).css('height', 'auto');
        $musicians.show();
        $feedItem.animate({'max-height': '1000px'});
      }

      toggledOpen = !toggledOpen;

      return false;
    }

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

      $('.btn-share', $feedItem).click(function() {
        ui.launchShareDialog(claimedRecordingId, 'recording');
      });
      
      $('.btn-comment', $feedItem).click(function() {
        ui.launchCommentDialog({
          recording_id: recordingId,
          claimed_recording_id: claimedRecordingId,
          entity_type: 'recording'
        });
      });

      $('.btn-like', $feedItem).click(function() {
        ui.addRecordingLike(recordingId, claimedRecordingId, JK.currentUserId, $('.likes', $feedItem), $('.btn-like', $feedItem));
      });

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

    function initialize() {
      $('.timeago', $feedItem).timeago();
      $('.dotdotdot', $feedItem).dotdotdot();
      $controls.listenRecording({recordingId: recordingId, claimedRecordingId: claimedRecordingId, sliderSelector:'.recording-slider', sliderBarSelector: '.recording-playback', currentTimeSelector:'.recording-current'});
      context.JK.prettyPrintElements($('time.duration', $feedItem));
      context.JK.setInstrumentAssetPath($('.instrument-icon', $feedItem));

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

      events();

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

      mixInfo = recordingUtils.createMixInfo({state: options.mix_state})
      $controls.data('mix-state', mixInfo) // for recordingUtils helper methods
      $controls.data('view-context', 'feed')
      $controls.addClass(mixInfo.mixStateClass)
      $status.text(mixInfo.mixStateMsg)

      if(mixInfo.mixState == 'mixed' || mixInfo.mixState == 'stream-mix') {
        $controls.addClass('has-mix')
      }
    }

    initialize();

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