(function(context,$) {

  "use strict";
  context.JK = context.JK || {};
  context.JK.LaunchAppDialog = function(app) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var $dialog = null;
    var $contentHolder = null;
    var $templateUnsupportedLaunch = null;
    var $templateAttemptLaunch = null;
    var $templateLaunchSuccessful = null;
    var $templateLaunchUnsuccessful = null;
    var $gear_modal = false;

    function renderAttemptLaunch() {
      var $template = $(context._.template($templateAttemptLaunch.html(), buildOptions(), { variable: 'data' }));
      $template.find('.btn-cancel').click(handleBack);
      $contentHolder.empty().append($template);


      var options = {
        callback:function(success) {
          if(success) {
            renderLaunchSuccess();
          }
          else {
            renderLaunchUnsuccessful();
          }
        },
        fallback:function() {
          renderUnsupportedLaunch();
        }
      };

      $template.find('.btn-launch-app').customProtocol(options);
    }

    function renderLaunchSuccess() {
      var $template = $(context._.template($templateLaunchSuccessful.html(), buildOptions(), { variable: 'data' }));
      $template.find('.btn-done').click(handleBack);
      $template.find('.not-actually-launched').click(function() {
        $dialog.find('.dont-see-it').show();
        return false;
      });
      $contentHolder.empty().append($template);
    }

    function renderLaunchUnsuccessful() {
      var $template = $(context._.template($templateLaunchUnsuccessful.html(), buildOptions(), { variable: 'data' }));
      $template.find('.btn-done').click(handleBack);
      $contentHolder.empty().append($template);
    }

    function renderUnsupportedLaunch() {
      var $template = $(context._.template($templateUnsupportedLaunch.html(), buildOptions(), { variable: 'data' }));
      $template.find('.btn-cancel').click(handleBack);
      $contentHolder.empty().append($template);
    }

    function handleBack() {
      app.layout.closeDialog('launch-app-dialog');
      return false;
    }

    function buildOptions() {
      var osSpecificSystemTray = 'taskbar';
      if($.browser.mac) {
        osSpecificSystemTray = 'dock or status menu';
      }

      var osSpecificSystemTrayLink = '';

      if($.browser.win) {
        osSpecificSystemTrayLink = 'http://windows.microsoft.com/en-us/windows/taskbar-overview#1TC=windows-7';
      }
      else if($.browser.mac) {
        osSpecificSystemTrayLink = 'http://support.apple.com/kb/ht3737';
      }

      var message_prefix = $gear_modal ? "To configure your audio gear" : "To create or find and join a session";

      return { osSpecificSystemTray: osSpecificSystemTray, osSpecificSystemTrayLink: osSpecificSystemTrayLink, launchUrl: 'jamkazam:', messagePrefix: message_prefix};
    }

    function reset() {
      renderAttemptLaunch();
    }

    function beforeShow(args) {
      if (args) {
        $gear_modal = (args.d1 == "gear");
      }
      else {
        $gear_modal = false;
      }
      reset();
    }

    function initialize() {
      var dialogBindings = {
        'beforeShow' : beforeShow
      };

      app.bindDialog('launch-app-dialog', dialogBindings);

      $dialog                     = $('#launch-app-dialog');
      $contentHolder              = $dialog.find('.dialog-inner');
      $templateAttemptLaunch      = $('#template-attempt-launch');
      $templateUnsupportedLaunch  = $('#template-unsupported-launch');
      $templateLaunchSuccessful   = $('#template-launch-successful');
      $templateLaunchUnsuccessful = $('#template-launch-unsuccessful');
    }

    this.initialize = initialize;
  }

  return this;
})(window,jQuery);
