(function (context, $) {

  "use strict";

  context.JK = context.JK || {};

  context.JK.VideoDialog = function (app) {
    var logger = context.JK.logger;
    var rest = context.JK.Rest();
    var dialogId = '#video-dialog';

    function videoClick(e) {
      var $self = $(this);
      if (!context.jamClient || !context.jamClient.IsNativeClient()) {

        $('#video-dialog-header').html($self.data('video-header') || $self.attr('data-video-header'));
        $('#video-dialog-iframe').attr('src', $self.data('video-url') || $self.attr('data-video-url'));
        app.layout.showDialog('video-dialog');
        e.stopPropagation();
        e.preventDefault();
        return false;
      }
      else {
        var videoUrl = $.param.querystring(window.location.href, 'showVideo=' + encodeURIComponent($self.data('video-url')));
        context.jamClient.OpenSystemBrowser(videoUrl);
      }
    }

    function events() {
      $('.carousel .slides').on('click', '.slideItem', videoClick);
      $('.video-slide').on('click', videoClick);
      $('.video-item').on('click', videoClick);

      $(dialogId + '-close').click(function (e) {
        app.layout.closeDialog('video-dialog');
        $('#video-dialog-header').html('');
        $('#video-dialog-iframe').attr('src', '');
        e.stopPropagation();
        return false;
      });
    }

    function beforeShow() {

    }

    function afterHide() {

    }

    function initialize() {

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

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

      events();
    }

    this.initialize = initialize;

  }
})(window, jQuery);