// setup everything when document is ready
jQuery(document).ready(function($) {
	$('#shoutbox_form').ajaxForm({

		// target identifies the element(s) to update with the server response
		target: '#shoutbox_comments',

		// handler function for success event
		success: function(responseText, statusText) {
			$('#shoutbox_response').css('display','none');
			$('#shoutbox_response').html('<span class="shoutbox-success">Dein Kommentar wurde gespeichert</span>');
			$('#submit_shoutbox_post').attr('value', 'Submit');
			$("#shoutbox_post #author").attr('value', '');
			$("#shoutbox_post #shoutbox_comment").attr('value', '');

			$("#shoutbox_post #shoutbox_comment").focus();
			$("#shoutbox_response").show("fast");
			
		},

		// handler function for errors
		error: function(request) {
			// parse the response for WordPress error
			if (request.responseText.search(/<title>WordPress &rsaquo; Fehler<\/title>/) != -1) {
				var data = request.responseText.match(/<p>(.*)<\/p>/);
				$('#shoutbox_response').html('<span class="shoutbox-error">'+ data[1] +'</span>');
			} else {
				$('#shoutbox_response').html('<span class="shoutbox-error">Da hat was nicht geklappt</span>');
			}
			$('#submit_shoutbox_post').attr('value', 'Submit');
			$("#shoutbox_post #shoutbox_comment").attr('value', '');

			$("#shoutbox_post #shoutbox_comment").focus();
			$("#shoutbox_response").show("fast");
		},

		beforeSubmit: function(formData, jqForm, options) {
			// clear response div
			$('#shoutbox_response').empty();

			for (var i=0; i < formData.length; i++) {
				if (!formData[i].value) {
					$('#shoutbox_response').html('<span class="shoutbox-error">Bitte alle Felder ausf&uuml;llen.</span');
					return false;
				}
			}
			$('#submit_shoutbox_post').attr('value', 'Bitte warten...');
		}

	});

	refreshtime=parseInt( SpitzohrShoutboxSettings.refreshtime);
	if (refreshtime)
	timeoutID = setInterval(refresh, (refreshtime  < 5000 ) ? 5000: refreshtime );


	function refresh() {
		var shoutboxform = $('#shoutbox_form');
		if (shoutboxform[0])
			$('#shoutbox_comments').load(shoutboxform[0].action+'?refresh=1');
	}

});