var  Comments = function () {
	
	var that = this;
	
	// Captcha-Code
	this.scOK = false;
	
	// Validierung des Kommentars
	this.validNewCommentForm = function () {
		
		var errors = '';
		
		if (jQuery.trim(jQuery("#commentUsername").val()) == '') {
			errors = "Keinen Namen eingegeben.<br />";
		}
		if (jQuery.trim(jQuery("#commentHeadline").val()) == '') {
			errors += "Keine Überschrift eingegeben.<br />";
		}
		if (jQuery.trim(jQuery("#commentInhalt").val()) == '') {
			errors += "Keinen Kommentar eingegeben.<br />";
		}
		if (jQuery.trim(jQuery('input[name="commentSecurityCode"]').val()) == '') {
			errors += "Den Sicherheitscode nicht eingegeben.<br />";
		}

		return this.showErrors(errors);
	};
	
	// Erstell Formular zum Verstoß-Melden
	this.createOffenceForm = function (linkObj) {
	
		var commentID = getCommentID(linkObj);
		
		jQuery('#someComment_'+commentID+' .commentKasten').after(jQuery('.offenceBlock').show());
		
		return false;
	};
	
	// "Verstoß melden" abbrechen
	this.cancelOffence = function (linkObj) {
		
		jQuery('#allComments').after(jQuery('.offenceBlock').hide());
		
		return false;
	};
	
	// Verstoß-Form-Validierung
	this.validNewOffenceForm = function (linkObj) {
		
		var sc = linkObj.siblings('input[name="commentSecurityCode"]').val();
		
		jQuery('#offenceFormError').html('');
		
		jQuery.ajax({
			url: commentWeb + 'rem.phtml',
			async: false,
			data: {sc: sc},
			dataType: 'html',
			success: function (http_xml) {
					
					if (http_xml != 1) {
						that.scOK = false;
						jQuery('#offenceFormError').html('Der eingegebene Sicherheistcode ist falsch.');
					} else {
						that.scOK = true;
					}
			}
		});
		
		return that.scOK;
	};
	
	// Validierungsfehler für einen neuen Kommentar anzeigen
	this.showErrors = function (errors) {
		if (errors !== '') {
			jQuery("#commentFormError").html(errors);
			return false;
		} else {
			return true;
		}
	};
	
	// On click "Kommentar schreiben" als Antwort
	this.writeAnswer = function (linkObj) {
		
		jQuery('#Kommentar').show();

		this.toKommentar();
		
		var commentID = getCommentID(linkObj);
		
		var ueberschrift = jQuery('#commentTitle_'+commentID).text();
		
		jQuery('#answerToComment').show();
		jQuery('.commentCaptionAnswer').first().text('Antwort auf "'+ ueberschrift +'"');
		jQuery('#zumKommentar').val(commentID);
		jQuery('#commentHeadline').val('AW: ' + ueberschrift);
	};
	
	// Zur Kommentar-Form springen
	this.toKommentar = function (elem) {
		
		
		if (!elem) elem = 'Kommentar'; 
		
		jQuery('#'+elem).show();
		
		var kommentFormPosition = jQuery('#'+elem).position();
		
		window.scrollTo(0,kommentFormPosition.top);
	};
	
	// On click "Kommentar schreiben" allgemein
	this.writeComment = function () {
		
		jQuery('#Kommentar').show();
		
		this.toKommentar();
		
		jQuery('#commentHeadline').val(jQuery('#commentCaptionComment').text());
		jQuery('#zumKommentar').val('0');
		jQuery('.commentCaptionAnswer').first().empty();
		jQuery('#answerToComment').hide();
	};
	
	// Liefert Comment-ID für ein Element im Kommentar-Div
	function getCommentID (linkObj) {
		return linkObj.siblings('.thisCommentID').val();
	};
};


jQuery(function(){
	
	jQuery('.comment_forms').append('<input type="hidden" name="ds" value="km" />');
	
	var theComments = new Comments();
	
	if (ankerZiel != false) {
		
		theComments.toKommentar('allComments');
		
		if (zurForm != false) theComments.toKommentar('Kommentar'); 
	}
	
	// Kommentar schreiben
	jQuery('#goToComment').click(function(){
		theComments.writeComment();
	});
	
	// Antwort auf Kommentar schreiben
	jQuery('.answerCommentLink').click(function(){
		theComments.writeAnswer(jQuery(this));
	});
	
	// Formvalidierung
	jQuery('#commentSendButton').click(function(){
		return theComments.validNewCommentForm();
	});
	
	// Verstoß melden
	jQuery('.reportOffenceButton').click(function(){
		
		return theComments.createOffenceForm(jQuery(this));
	});
	
	// Verstoß melden abbrechen
	jQuery('input[name="cancelOffence"]').click(function(){
		return theComments.cancelOffence(jQuery(this));
	});
	
	// Verstoß-Form-Validierung
	jQuery('input[name="addOffence"]').click(function(){
		return theComments.validNewOffenceForm(jQuery(this));
	});
	
	// Button-Animation
	jQuery('.verstossButtonImg').hover(
		function(){jQuery(this).attr('src', commentWeb + 'img/report-color.gif');},
		function(){jQuery(this).attr('src', commentWeb + 'img/report.gif');}
	);
	jQuery('.kommentButtonImg').hover(
		function(){jQuery(this).attr('src', commentWeb + 'img/answer-color.gif');},
		function(){jQuery(this).attr('src', commentWeb + 'img/answer.gif');}
	);
	jQuery('.commentPrev').hover(
			function(){jQuery(this).attr('src', commentWeb + 'img/prev_Mover.png');},
			function(){jQuery(this).attr('src', commentWeb + 'img/prev.png');}
	);
	jQuery('.commentNext').hover(
			function(){jQuery(this).attr('src', commentWeb + 'img/next_Mover.png');},
			function(){jQuery(this).attr('src', commentWeb + 'img/next.png');}
	);
});
