(function(context,$) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.SessionStartDialog = function(app, session) {
    var logger = context.JK.logger;
    var sessionUtils = context.JK.SessionUtils;
    var $dialog = null;
    var dialogId = 'session-start-dialog';
    var $btnStartSession = null;

    function beforeShow(data) {
    }

    function afterShow(data) {
    }

    function afterHide() {
    }

    function showDialog() {
      return app.layout.showDialog(dialogId);
    }

    function events() {
      $btnStartSession.unbind('click');
      $btnStartSession.click(function(e) {
        context.location = '/client#/session/' + session.id;
        app.layout.closeDialog(dialogId);
      });
    }

    function initializeSessionDetails() {
      $dialog.find('#session-start-type-disp').html('Now!');
      $dialog.find('#session-name-disp').html(session.name);
      $dialog.find('#session-description-disp').html(session.description);

      if (session.music_notations && session.music_notations.length > 0) {
        $dialog.find('#session-notations-disp').html("Notations: " + session.music_notations.join(', '));
      }

      if (session.band) {
        $dialog.find('#session-band-disp').html(band.name);
      }
      else {
        $dialog.find('#session-band-disp').html('N/A');
      }

      $dialog.find('#session-language-disp').html(session.language_description);

      var invitedFriends = session.invitations;

      var sessionInvited = [];
      $.each(invitedFriends, function(index, invitation) {
        sessionInvited.push(invitation.receiver_name);
      });

      var sessionInvitedString = sessionInvited.join(', ');

      if (session.musician_access && session.approval_required) {
        if (session.open_rsvps) {
          if (invitedFriends.length == 0)
            sessionInvitedString = "Any interested JamKazam musicians that I approve";
          else
            sessionInvitedString += ", plus any interested JamKazam musicians that I approve";
        }
        else {
          if (invitedFriends.length == 0) {
            sessionInvitedString = "No open RSVPs";
          }
          else {
            sessionInvitedString += " (No open RSVPs)";
          }
        }
      }
      else if (session.musician_access && !session.approval_required) {
        if (invitedFriends.length == 0)
          sessionInvitedString = "Any interested JamKazam musicians who want to join us";
        else
          sessionInvitedString += ", plus any interested JamKazam musicians who want to join us";
      }

      $dialog.find('#session-invited-disp').html(sessionInvitedString);

      var instrumentsMe = [], instrumentsOthers = [];

      $.each(session.approved_rsvps, function(index, rsvp) {
        if (rsvp.id === context.JK.currentUserId) {
          $.each(rsvp.instrument_list, function(index, instrument) {
            instrumentsMe.push(instrument.desc);
          });
        }
        else {
          $.each(rsvp.instrument_list, function(index, instrument) {
            instrumentsOthers.push(instrument.desc);
          });
        }
      });

      $dialog.find('#session-instruments-me-disp').html(instrumentsMe.join(', '));
      $dialog.find('#session-instruments-rsvp-disp').html(instrumentsOthers.join(', '));

      $dialog.find('#session-musician-access-disp').html('Musicians: ' + session.musician_access_description);
      $dialog.find('#session-fans-access-disp').html('Fans: ' + session.fan_access_description);
      $dialog.find('#session-policy-disp').html(session.legal_policy);
    }

    function initialize() {

      var dialogBindings = {
        'beforeShow' : beforeShow,
        'afterShow' : afterShow,
        'afterHide': afterHide
      };

      app.bindDialog(dialogId, dialogBindings);

      $dialog = $('[layout-id="' + dialogId + '"]');
      $btnStartSession = $dialog.find('.btnStartSession');

      initializeSessionDetails();
      events();
    }

    this.initialize = initialize;
    this.showDialog = showDialog;
  }

  return this;
})(window,jQuery);