(function (context, $) {

  "use strict";

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

  var welcomeRoot;
  var rest = context.JK.Rest();
  var logger = context.JK.logger;

  function initialize() {

    welcomeRoot = $('.landing-content .wrapper .welcome')

    $('#signup').click(function (e) {
      context.JK.app.layout.showDialog('signup-dialog');
      e.preventDefault();
      return false;
    });

    $('#signin.signin').click(function (e) {
      if (context.JK.currentUserId) {
        rest.getUserDetail({id:context.JK.currentUserId})
          .done(function () {
            e.preventDefault();
            var redirectTo = $.QueryString['redirect-to'];
            if(redirectTo) {
              logger.debug("redirectTo:" + redirectTo);
              window.location = redirectTo;
            }
            else {
              logger.debug("default post-login path");
              window.location = '/client';
            }
          })
          .fail(function () {
            context.JK.app.layout.showDialog('signin-dialog');
          })
      }
      else {
        // don't bother checking if no context.JK.currentUserId
        context.JK.app.layout.showDialog('signin-dialog');
      }
      e.preventDefault();
      return false;
    });

    $('.carousel').carousel({
      hAlign: 'center',
      vAlign: 'bottom',
      vMargin: .5,
      reflection: true,
      reflectionColor: '0,0,0',
      directionNav: false,
      frontWidth: 424,
      frontHeight: 263,
      carouselWidth: 750,
      carouselHeight: 440,
      slidesPerScroll: 3,
      backOpacity: 2
    });
    $('.carousel').show()

    context.JK.TickDuration('.feed-entry.music-session-history-entry .inprogress .tick-duration');

    if ($.QueryString['showVideo']) {
      // pop a video automatically
      var slides = $('.slideItem');
      $.each(slides, function (i, slide) {
        slide = $(slide)
        if (slide.attr('data-video-url') == $.QueryString['showVideo']) {
          slide.click();
        }
      });
    }

    $('.like-link').click(function() {
      var like_site = $(this).data('site');
      // removed because we are juggling 20 events max in GA
      //JK.GA.trackJKSocial(JK.GA.Categories.jkLike, like_site, JK.clientType());
      window.open("/endorse/0/"+like_site, '_blank');
    });
  }

  context.JK.WelcomePage = initialize;


})(window, jQuery);