$(document).ready(function(){

	$("#b-login a").click(function(){
    $("#login").animate({opacity: "show"}, "slow");
	  return false;
	});
	
jQuery.fn.inputFieldText = function(string) {
  this.each(function() {
      $(this).addClass("fieldhint").val(string);
      $(this).focus(function(){
        if ($(this).val() == string){
          $(this).removeClass("fieldhint").val('');
        }
      });
      $(this).blur(function(){
        if ($(this).val() == '' ){
          $(this).addClass("fieldhint").val(string);
        }
      });  
  });
}
	
  $("#login input[name=regmail]").inputFieldText("E-post"); 
  $("#login input[name=regpass]").inputFieldText("Passord");
  
  $("form .hinted").focus(function () {
  	$(this).next("em").animate({opacity: "show"}, "slow");
	})
	$("form .hinted").blur(function() {
	  $(this).next("em").animate({opacity: "hide"}, "fast");
	});
	
  $("select[multiple]").asmSelect();
  if($(".asmListItem").length == 3) {
	  $(".asmSelect").attr("disabled", true);
	}
  
  $(".asmSelect").click(function() {
	  if($(".asmListItem").length == 3) {
	    $(this).attr("disabled", true);
	  }
	});
  
  $("#regcats").change(function() {
	  if($(".asmListItem").length < 3) {
      $(".asmSelect").removeAttr("disabled");
    }
	});
	
	$(".comment .editlink").click(function () {
    if ($(this).is(":contains('Rediger')")) {
      $(this).parent().next("p").replaceWith("<textarea>" + $(this).parent().next("p").text() + "</textarea>");
      $(this).text("Lagre");
    } else {
      $(this).after('<em id="ajaxloading">Lagrer...</em>');
      $(this).hide();
      var commentid = $(this).attr("href").split("=")[1];
      $.post("commentupdate.php", { id: commentid, text: $(this).parent().next("textarea").val() }, function(data){
        console.log("Result:" + data.result);
        console.log("Text:" + data.text);
        $("#ajaxloading").replaceWith("<em>Lagret</em>");
      }, "json" );
      $(this).parent().next("textarea").replaceWith('<p class="commenttext">' + $(this).parent().next("textarea").val() + "</p>");
    }
    return false;
  });

});