if (!gg) {
  var gg = {};
}

(function() {
  gg.Reg = {};

  var hoverOpacity = true;

  gg.Reg.initMembershipRequest = function() {
    $('#request_membership_link').click(gg.Reg.showMembershipRequestForm);
    $('#membership #learn_more').click(gg.Reg.showLearnMore);
    $('#membership form').submit(gg.Reg.submitMembershipRequestForm);

    $('#back_to_non_member').click(gg.Reg.showRequestMembership);
  };

  gg.Reg.showBackground = function(index, subsite) {
    if (!subsite) var subsite = gg.Site.currentSubsite.key || 'default';

    var backgroundFilePrefix = "/images/" + subsite + "/register/background_large_";

    var img = new Image();
    img.src = fl.Init.ImageUri(backgroundFilePrefix + index + ".jpg");
    $(img).css('marginLeft', $('#background img').css('marginLeft'));

    $(img).load(function() {
      $('#background img').attr('src', img.src);
      $('#background img').fadeIn();
      $(window).trigger('resize');
    });
  };

  gg.Reg.initBackground = function() {
    $(window).resize(backgroundResizeHandler);
    $(window).trigger('resize');
  };

  gg.Reg.initForgotPassword = function() {
    $('#forgot_password').click(gg.Reg.showForgotPasswordForm);
    $('#forgot_password_form').submit(gg.Reg.submitForgotPasswordForm);

    $('#back_to_login').click(gg.Reg.showLoginForm);
  };

  gg.Reg.hoverFormOpacity = function(forms) {
    var form_elements = $(forms.join());

    for(var i = 0; i < forms.length; i++) {
      $(forms[i]).hover(function() {
        if (hoverOpacity) {
          // Fade out other forms
          form_elements.not('#' + $(this).attr('id')).fadeTo("fast", 0.52);

          $(this).fadeTo("fast", 1.0);
          $(this).find('input:visible:first').focus();
        }
      });
    }
  };

  gg.Reg.showForgotPasswordForm = function() {
    $('#login_form').hide();
    $('#forgot_password_form').show();
    $('#forgot_password_form').find('input:first').focus();
    equalizeHeights();

    return false;
  };

  gg.Reg.submitForgotPasswordForm = function() {
    $.ajax({
        url: $(this).attr('action'),
        type: $(this).attr('method'),
        data: $(this).serialize(),
        error: function() {
          alert("There was a problem requesting a password.  Please try again.");
        },
        success: function(response) {
          $('#forgot_password_form').replaceWith(response);
          $('#forgot_password_form').submit(gg.Reg.submitForgotPasswordForm);
          $('#forgot_password_form').show();
          equalizeHeights();
        }
    });

    return false;
  };

  gg.Reg.showLoginForm = function() {
    $('#forgot_password_form').hide();
    $('#learn_more_content').hide();
    $('#login_form').show();
    $('#login_form').find('input:first').focus();
    equalizeHeights();

    return false;
  };

  gg.Reg.showRequestMembership = function() {
    $('#membership #request').show();
    $('#membership #request_form').hide();

    if ($('#learn_more_content').filter(':visible').length > 0) {
      gg.Reg.showLoginForm();

      // Re-enable opacity change on hover ... and trigger fading
      hoverOpacity = true;
      $('#membership').trigger('mouseover');
    }

    equalizeHeights();

    return false;
  };

  gg.Reg.showMembershipRequestForm = function() {
    $('#membership #request').hide();
    $('#membership #request_form').show();
    $('#membership #request_form').find('input:first').focus();
    equalizeHeights();

 
  };

  gg.Reg.showLearnMore = function() {
    gg.Reg.showMembershipRequestForm();
    $('#learn_more_content').show();

    // Disable opacity change on hover ... and bring learn more info to full
    hoverOpacity = false;
    $('#login_container').css('opacity', 1.0);

    $('#forgot_password_form').hide();
    $('#login_form').hide();

    return false;
  };

  gg.Reg.submitMembershipRequestForm = function() {
    $.ajax({
        url: $(this).attr('action'),
        type: $(this).attr('method'),
        data: $(this).serialize(),
        error: function() {
          alert("There was a problem requesting a membership.  Please try again.");
        },
        success: function(response) {
          $('#membership #request_form').html(response);
          $('#back_to_non_member').click(gg.Reg.showRequestMembership);
          $('#membership form').submit(gg.Reg.submitMembershipRequestForm);
          $('#membership form').show();
          equalizeHeights();

          
        }
    });

    return false;
  };

  // Private functions
  // -----------------
  var backgroundResizeHandler = function() {
    // If the inital image isn't cached, it'll report 0 dimensions.
    if ($('#background img').height() == 0 || $('#background img').width() == 0) {
      setTimeout(function() { $(window).trigger('resize'); }, 100);
      return;
    }

    if ($('#background').hasClass('horizontal')) {
      if ($('#background img').height() < $('#background').height()) {
        $('#background').addClass('vertical').removeClass('horizontal');
        $('#background img').css('marginLeft', -1 * parseInt($('#background img').width() / 2));
      }
    } else {
      if ($('#background img').width() < $('#background').width()) {
        $('#background').addClass('horizontal').removeClass('vertical');
        $('#background img').css('marginLeft', 0);
      }
    }
  };

  var equalizeHeights = function() {
    var login = $('#login_container');
    var membership = $('#membership');
    var emSize = parseInt($('body').css('font-size'));
    var height = "auto";

    if (login.height() > membership.height()) {
      height = (login.height() / emSize) + "em";
    } else if (membership.height() > login.height()) {
      height = (membership.height() / emSize) + "em";
    }

    login.height(height);
    membership.height(height);

    if (height != 'auto')
      $('#form_container .container').height((login.outerHeight() / emSize) + "em");
    else
      $('#form_container .container').height('21em');
  };
})();