$(function()
  {
      $("#edit form").livequery(
	  function()
	  {
	      $(this).ajaxForm(
		  { dataType: 'json',
		    success:
		    function(data, str, set)
		    {
			if (data.success)
			{
			    window.location = data.url;
			}
		    }
		  });
	  });

      $("#edit button.preview").livequery(
	  'click',
	  function()
	  {
	      $(this).parents("#edit form").ajaxSubmit(
		  { url: $(this).attr('url'),
		    success:
		    function(data, str, set)
		    {
			$("#preview").fadeOutIn(data);
		    }
		  });
	  });

      $("form.code").livequery(
	  function()
	  {
	      $(this).ajaxForm(
		  { success:
		    function(data, str, set)
		    {
			$("div.code-highlight").html(data);
		    }
		  })

	      $(this).submit();
	  });

      $("form button.submit").livequery(
	  'click',
	  function ()
	  {
	      $(this).parents("form").submit();
	  });

      $(".formattedcode .rparen").livequery(
	  function()
	  {
	      $(this).hover(
		  function ()
		  {
		      $(this).parents(".formattedcode").find("#" + $(this).attr("id") + ".lparen").addClass("paren-active");
		      $(this).addClass("paren-active");
		  },
		  function ()
		  {
		      $(this).parents(".formattedcode").find("#" + $(this).attr("id") + ".lparen").removeClass("paren-active");
		      $(this).removeClass("paren-active");
		  });
	  });

      $(".formattedcode .lparen").livequery(
	  function()
	  {
	      $(this).hover(
		  function ()
		  {
		      
		      $(this).parents(".formattedcode").find("#" + $(this).attr("id") + ".rparen").addClass("paren-active");
		      $(this).addClass("paren-active");
		  },
		  function ()
		  {
		      $(this).parents(".formattedcode").find("#" + $(this).attr("id") + ".rparen").removeClass("paren-active");
		      $(this).removeClass("paren-active");
		  });
	  });

      $("p.code").livequery(
	  function()
	  {
	      var codediv = this;

	      $.get("/karsten/highlight",
		    { code: $(codediv).html() },
		    function (data)
		    {
			$(codediv).after(data).remove();
		    });
	  });
  });