$(function()
{
  $('a.anfrageformular').live('click', function(event)
  {
    $('body').append('<div id="dialog-form" />');
  
    $('#dialog-form').unbind().dialog(
    {
      autoOpen: false,
	  modal: true,
	  width: 500
    });
  
	var $dialog = $('#dialog-form');
    
	event.preventDefault();
	
	$.ajax({
        url: '/ajax/anfrageformular/',
        success: function(html)
        {
		  $dialog.html(html);
		  $dialog.dialog('open');
		  
		  $dialog.find('form').live('submit', function(event)
		  {
		    event.preventDefault();
			
			$.ajax({
              type     : "post",
              data     : $dialog.find('form').serialize(),
              url      : this.action,
              success  : function(html)
              {
			    var $value = $(html).find('div.wpcf7-mail-sent-ok');

				if ($value.text() == '')
				{
                  $dialog.html(html);
				  $dialog.find('span.wpcf7-not-valid-tip-no-ajax').append('<span class="icon"></span>');
				}
				else
				{
				  $dialog.dialog('close');
				}
              }
            });
          });
        }
	});
  });
});

