$(document).ready(function() {
	$(document).ready(function(){
		$("a[rel^='prettyPhoto']").prettyPhoto({
			opacity: 0.7,
			theme: 'dark_rounded'
		});
	});
	$('.entry .inside .media .description').hide();
	$('.entry .inside .media').hover(
		function () {
			$(this).find('.description').slideDown('fast');
		},
		function () {
			$(this).find('.description').slideUp('fast')
		}
		);
	var options = {
		beforeSubmit: showRequest,
		success: commentResponse,
		url: '/API/addComment',
		dataType: 'json'
	};
	$('#comment-form').ajaxForm(options);
})


function showRequest(formData, jqForm, options)
{
	for (var i=0; i < formData.length; i++) {
        if (!formData[i].value) {
            notify('Es sind nicht alle Felder ausgefüllt.');
            return false;
        }
    }
	commentActivity(true);
}


function commentActivity(active)
{
	if (active == true) {
		$('#comment-form input').attr('disabled', 'true');
		$('#comment-form textarea').attr('disabled', 'truw');
		notify('Übertrage Daten...');
	} else {
		$('#comment-form input').attr('disabled', '');
		$('#comment-form textarea').attr('disabled', '');
	}
}

function commentResponse(responseText, statusText)
{
	commentActivity(false);
	if (responseText.success == true) {
		if (responseText.data.visible == 0) {
			// $('#comment-form').before(responseText.data.data);
			$('#comment-form').clearForm();
			notify(responseText.data.data);
		} else {
			showComment(responseText.data.data);
		}
	} else {
		var error = responseText.data;
		notify("Es ist ein Fehler aufgetreten: <br/>" + error);
	}
}

function showComment(data)
{
	$('#comment-form').clearForm();
	var text = '<li><h3>' + data.name + '</h3><p>' + data.body + '<p></li>';
	if ($('#comments').length == 0) {
		$('#comment-form').before('<ul id="comments" class="text"></ul>');
	}
	$('#comments').append(text);
}

function notify(text)
{
	$("#notification").html(text);
	$('#notification').fadeIn()
	setTimeout("$('#notification').fadeOut()", 5000);
}
