$(document).ready(function() {
  $("#login, #register, #forgot").click(function(event){
    $("#login, #register, #forgot").not(this).addClass("link");
    $("#login, #register, #forgot").not(this).removeClass("bolded");
    $(this).removeClass("link");
    $(this).addClass("bolded");
    
    var target;
    if($(this).is("#login")) {
      $("#pw1l, #pw1c").animate({height: 'show', opacity: 'show'});
      $("#pw2l, #pw2c").animate({height: 'hide', opacity: 'hide'});
      target = "login";
    }
    else if($(this).is("#register")) {
      $("#pw1l, #pw1c").animate({height: 'show', opacity: 'show'});
      $("#pw2l, #pw2c").animate({height: 'show', opacity: 'show'});
      target = "register";
    }
    else if($(this).is("#forgot")) {
      $("#pw1l, #pw1c").animate({height: 'hide', opacity: 'hide'});
      $("#pw2l, #pw2c").animate({height: 'hide', opacity: 'hide'});
      target = "forgot";
    }
    
    $("#userformaction").attr("value", target);
  });
  
  $("#userform").submit(function() {
    $("#errormsg").animate({height: 'hide', opacity: 'hide'});
    $("#loading").animate({opacity: 'show'});
    $("#usersubmit").attr("value", "Senden...");
    $("#usersubmit").attr("disabled", true);
    
    $.post("./?my&a=" + $("#userformaction").attr("value"), $(this).serialize(), function(response) {
      $("#loading").animate({opacity: 'hide'});
      $("#usersubmit").attr("value", "Senden");
      $("#usersubmit").attr("disabled", false);
      
      var status = $("status", response).text();
      var message = $("message", response).text();
      
      if(status == "success") {
        $("#formarea").animate({height: 'hide', opacity: 'hide'});
        $("#successmsg").html(message);
        $("#successmsg").animate({height: 'show', opacity: 'show'});
      }
      else {
        $("#captcha").attr("src", "captcha/captcha.php?t=my&r=" + Math.round(Math.random()*999999));
        $("#userform input[name=captcha]").attr("value", "");
        if(message.length > 0) {
          $("#errormsg").text(message);
          $("#errormsg").animate({height: 'show', opacity: 'show'});
        }
      }
    });
    return false;
  });
  
  // deep link
  if(document.location.href.match("my#register$")) {
    $("#register").click();
  }
  if(document.location.href.match("my#forgot$")) {
    $("#forgot").click();
  }
});
