$(document).ready(function() {
  
  /* Hide/show sign in box */
  $('.b-sign-in').click(function() {
    var expandable = $('.sign-in-drop');
    expandable.stop(true);
    if (expandable.is(".down")) {
      $(this).removeClass("current");
      expandable.removeClass("down").animate({
        height: 0
      },
      200);
    } else {
      $(this).addClass("current");
      var toHeight = 210;
      if(expandable.is('.sign-in-drop-with-errors')) toHeight = 226;
      expandable.show().addClass("down").animate({
        height: toHeight
      }, 200, function() {
        $(this).find(':input:visible:first').select().focus();
      });
    }
    return false;
  });
  $('.sign-in-drop-with-errors').closest('.login-container').find('.b-sign-in').click();
  
  /* Handle logout */
  $('a.logout').click(function() {
    $.ajax({
      url: '/customer_sessions',
      type: 'POST',
      data: '_method=DELETE',
      async: false,
      success: function() {
        window.location.href = '/';
      },
      error: function() {
        window.location.href = '/';
      }
    });
    return false;
  });
});