function init_more_entries_events() {

	entry_count=0;

	$("#more_entries_link").click(function(event) {

		entry_count+=25;
		event.preventDefault();

		var total_entry_count = $("#gb_count").text().replace(/\./, "");
		var user = $(this).attr('href').replace(/^#/, "");

		$.ajax({

			type: "GET",
			url: "/cgi-bin/allmystery.pl?",
			data: 'am=guestbook&profile=' + user + '&next=' + entry_count,
			dataType: "html",

			beforeSend: function() {
				$("#more_entries_link").hide();
				$("#more_entries_loading_img").fadeIn("fast");
			},

 			success: function(data) {
				$("#gb_posts")
				.append(data);
			},

			complete: function(data) {

				if (entry_count > total_entry_count-25) $("#more_entries_link").remove();

				intTime = 0;

				$('#gb_posts li.hidden').each(function(i){
					var _this = this;
					window.setTimeout(function(){$(_this).fadeIn('slow').removeClass("hidden");}, intTime);
					intTime += 110;
				});

				window.setTimeout(function() {
					$("#more_entries_link").show();
					$("#more_entries_loading_img").hide();}, 2300);

			}

		});

	});

}

function init_delete_gb_entry_events() {

	$("a.delete_gb_entry").live('click', function(event){
		event.preventDefault();
		delete_url = $(this).attr('href');
		_self = $(this);

		if (_self.text() == "löschen") {
			$("a.delete_gb_entry").text("löschen");
			_self.html("<b>wirklich löschen?</b>");
			return false;
		}

		$.ajax({
			type: "GET",
			url: delete_url,
			dataType: "html",
			beforeSend: function() {
				$(_self).parent().parent().parent().slideUp(function() {
					$(_self).parent().parent().parent().remove();
				});
			}
		});
	});

}

function init_post_gb_entry_events() {

	$("#show_gb_post_wrap").click(function (event) {
		event.preventDefault();
		$("#gb_post_wrap").toggle();
		$("textarea#gb_post_textarea").focus();
	});

	$("#gb_post_submit").removeAttr('disabled');

	$("#gb_post_submit").click(function() {

		profile = $("input#profile").val();
 		key = $("input#key").val();
		text= $("textarea#gb_post_textarea").val();

		if (!text) {
			$("textarea#gb_post_textarea").focus();
			return false;
		}

		$.ajax({
			type: "POST",
			url: '/cgi-bin/allmystery.pl',
			dataType: "html",
			data: 'am=guestbook&action=1&' + 'profile=' + profile + '&key=' + key + '&text=' + escape(text),

			beforeSend: function() {
				$("#gb_post_submit").attr("disabled", "true");
				$("#gb_post_submit").attr("value", "Einen Moment...");
			},

			success: function(datas) {
				$("#gb_posts")
				.prepend(datas);
			},

			complete: function(data) {
				$("#gb_posts li")
				.slideDown();

			setTimeout(function() {
				$("#gb_post_submit").removeAttr('disabled');
         		$("#gb_post_submit").attr("value", "Eintragen");}, 1500);

				$("textarea#gb_post_textarea").val("");
			}

		});

		return false;

	});

}

$(document).ready(function() {

  init_more_entries_events();
  init_delete_gb_entry_events();
  init_post_gb_entry_events();
  //resize_images(100);

});