/*
 * Shows the background div with defined image, opacity 70%.
 * @imageURL for the image shown on background
 */
function loginWebBit_showPopupBackground(imageURL) {
  var background = jQuery("<div />").attr("id", "loginWebBitBackground").css({ "opacity": 0.7, "height": jQuery(window).height(), "background-image": "url("+imageURL+")", "background-repeat": "no-repeat", "background-attachment": "fixed", "background-position": "50% 50%" }).appendTo("body").fadeIn("fast");
}


/*
 * Hides the background div.
 */
function loginWebBit_hidePopupBackground() {
  jQuery("#loginWebBitBackground").fadeOut("fast", function() {
    jQuery(this).remove();
  });
}


/*
 * Show the Login Popup div.
 * @imageURL for the background image
 */
function loginWebBit_showPopup(imageURL) {
  //loads popup only if it is disabled
  var popupTop = Math.round(jQuery(window).height()/2 - jQuery("#loginWebBit").height()/2 + jQuery(document).scrollTop());
  if(popupTop<0) {
    popupTop = 0;
  }
  var popupLeft = Math.round(jQuery(window).width()/2 - jQuery("#loginWebBit").width()/2 + jQuery(document).scrollLeft());
  if(popupLeft<0) {
    popupLeft = 0;
  }
  loginWebBit_showPopupBackground(imageURL);
  jQuery("#loginWebBit").css({ "position":"absolute", "top":popupTop+"px", "left":popupLeft+"px" }).fadeIn("fast");
}


/*
 * Hide the Login Popup div.
 */
function loginWebBitHide() {
  loginWebBit_hidePopupBackground();
  jQuery("#loginWebBit").fadeOut("fast");
  loginWebBit_resetLoginFormBlocks();
}


/*
 * Show the forgot password form.
 */
function loginWebBit_resetLoginFormBlocks() {
  jQuery("#forgotPasswordFormBox, #forgotPasswordFormBoxSuccess, #forgotPasswordFormBoxLoading").hide();
  jQuery("#loginWebBitFormBox, #forgotPasswordFormBoxForm").show();
  jQuery("#forgotPasswordError").text("").hide();
  jQuery("#forgotPasswordEmail, #loginFormUsername, #loginFormPassword").val("");
}

/*
 * Submit the password e-mail.
 */
function loginWebBit_forgotPasswordRequestSubmit(email,componentId) {
  var errorText = jQuery("#forgotPasswordError").hide();
  if(email!="") {
    jQuery("#forgotPasswordFormBoxLoading").show();
    jQuery.ajax({
      url: 'index.php?article_id='+componentId+'&email='+email,
      type: 'POST',
      timeout: 10000,
      error: function(){
        alert('Error! Please try again.');
      },
      success: function(response) {
        jQuery("#forgotPasswordFormBoxLoading").hide();
        if(response=="email_not_found") {
          errorText
            .text("The e-mail address you entered could not be found in our database. Please check the spelling.")
            .fadeIn("normal");
        } else {
          jQuery("#forgotPasswordFormBoxForm").hide();
          jQuery("#forgotPasswordFormBoxSuccess").show();
        }
      }
    });
  } else {
    errorText
      .text("Please enter an e-mail address.")
      .fadeIn("normal");
  }
}

