(function (context, $) {
  /** Javascript for managing the (account dropdown) */


  "use strict";

  context.JK = context.JK || {};
  context.JK.UserDropdown = function (app) {
    var EVENTS = context.JK.EVENTS;
    var logger = context.JK.logger;
    var rest = new JK.Rest();
    var userMe = null;
    var invitationDialog = null;
    var nowYetShownGettingStarted = true;
    var $dropdown = null;

    function menuHoverIn() {
      $('ul.shortcuts', this).show();
    }

    function menuHoverOut() {
      $('ul.shortcuts', this).hide();
    }

    function events() {
      $dropdown.hoverIntent({
        over: function () {
          $('ul.shortcuts', this).fadeIn(100);
        },
        out: function () {
          $('ul.shortcuts', this).fadeOut(100);
        },
        timeout: 500
      });

      $dropdown.find('.real-signin').on('click', function(e) {
        e.stopPropagation()

        window.location.href ="/signin?redirect-to=" + encodeURIComponent(window.location.href)

        return false;
      })

      $dropdown.find('.invite-friends .menuheader').on('click', function (e) {
        $(this).closest('li').css('height', 'auto').find('ul').toggle();
        e.stopPropagation();
        return false;
      });

      $dropdown.find('.invite-friends .google-invite a').on('click', function (e) {
        invitationDialog.showGoogleDialog();
      });

      $dropdown.find('.invite-friends .email-invite a').on('click', function (e) {
        invitationDialog.showEmailDialog();
      });

      $dropdown.find('.invite-friends .facebook-invite a').on('click', function (e) {
        invitationDialog.showFacebookDialog(e);
      });

      $dropdown.find('.shortcuts .test-network').on('click', function(e) {
        app.layout.showDialog('network-test');
        return false;
      });

      $('#header-avatar').on('avatar_changed', function (event, newAvatarUrl) {
        updateAvatar(newAvatarUrl);
        event.preventDefault();
        return false;
      });
      $(document).on(EVENTS.USER_UPDATED, function(e, data) {
        userMe = data;
        updateHeader();
      })
    }

    function loadMe() {
      if (!context.JK.currentUserId) {
        return;
      }

      app.user()
        .done(function(r) {
          userMe = r;
          // TODO - Setting global variable for local user.
          context.JK.userMe = r;
          updateHeader();
        });
    }

    function updateHeader() {
      if (userMe.first_name  =='Anonymous' && userMe.last_name == 'Anonymous') {
        $('#user').html(userMe.email);
      }
      else {
        $('#user').html(userMe.name);
      }

      showAvatar();
    }

    // initially show avatar
    function showAvatar() {
      var photoUrl = context.JK.resolveAvatarUrl(userMe.photo_url);
      $('#header-avatar').attr('src', photoUrl);
    }

    // handle update avatar event
    function updateAvatar(avatar_url) {
      var photoUrl = context.JK.resolveAvatarUrl(avatar_url);
      var avatar = $(new Image());
      avatar.attr('src', photoUrl + '?cache_bust=' + new Date().getTime());
      avatar.attr('alt', "Avatar");
      avatar.attr('id', 'header-avatar');
      $('#header-avatar').replaceWith(avatar);
    }

    this.initialize = function (invitationDialogInstance) {
      $dropdown = $('#profile.userinfo');
      events();
      invitationDialog = invitationDialogInstance;
      loadMe();
    }
    this.loadMe = loadMe;

  }
})(window, jQuery);


