var li_even = true;
var depth = 0;
var target = 'html,body';

addComment = {
	moveForm : function(commId, parentId, respondId, postId) {
		var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');

		if ( ! comm || ! respond || ! cancel || ! parent )
			return;

		t.respondId = respondId;
		postId = postId || false;

		if ( ! t.I('wp-temp-form-div') ) {
			div = document.createElement('div');
			div.id = 'wp-temp-form-div';
			div.style.display = 'none';
			respond.parentNode.insertBefore(div, respond);
		}

		comm.parentNode.insertBefore(respond, comm.nextSibling);
		if ( post && postId )
			post.value = postId;
		parent.value = parentId;
		cancel.style.display = '';

		cancel.onclick = function() {
			var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);

			if ( ! temp || ! respond )
				return;

			t.I('comment_parent').value = '0';
			temp.parentNode.insertBefore(respond, temp);
			temp.parentNode.removeChild(temp);
			this.style.display = 'none';
			this.onclick = null;
			return false;
		}

		try { t.I('comment').focus(); }
		catch(e) {}

		return false;
	},

	I : function(e) {
		return document.getElementById(e);
	}
}

function fixCommentsLayout(branch) {
	depth++;
	if(branch.children().length > 0) {
		branch.children().each(function() {
			$(this).removeClass('even odd alt thread-even thread-odd thread-alt');
			if(li_even) {
				if(depth == 1) $(this).addClass('even thread-even depth-1');
				else $(this).addClass('even depth-' + depth);
				li_even = false;
			}
			else {
				if(depth == 1) $(this).addClass('odd alt thread-odd thread-alt depth-1');
				else $(this).addClass('odd alt depth-' + depth);
				li_even = true;
			}
		fixCommentsLayout($(this).children('ol.children'));				
		});
	}
	depth--;
}

function AjaxComment() {
	$.ajax({
		url: 'http://queerbeet.org/wp-content/themes/unnamed/comments-ajax.php',
		datatype: 'html',
		data: $("#commentform").serialize(),
		type: 'POST',
		beforeSend: function() { 
			$('#commentload').show();
			$('#error').html('');
			$('#error').hide();
			$('#preview').hide();
			$('#commentform').attr('disabled', 'disabled');
		},
		complete: function(xhr, status) {
 			if (status == 'success') {
				var comment_parent = $('#comment_parent').val();

				if($('#leavecomment').length != 0 ) {
					$('#leavecomment').remove();
					$('#commentlist').before('<h4 id="comments" class="section-title"><span id="response-count">1</span> Antwort zu' + $('.entry-header a').html() + '</h4><h5 class="section-title"><span id="comment-count">1</span> Kommentar</h5>');
				}
				else {
					if($('#response-count').length != 0) {
						var response_count = parseInt($('#response-count').html());
						switch(response_count) {
						case 1:
							$('#response-count').parent().html('<span id="response-count">2</span> Antworten zu' + $('.entry-header a').html());
						break;
						default:
							$('#response-count').html(++response_count);
						break;
						}
					}
				
					if($('#comment-count').length != 0) {
						var comment_count = parseInt($('#comment-count').html());
						switch(comment_count) {
						case 1:
							$('#comment-count').parent().html('<span id="comment-count">2</span> Kommentare');
						break;
						default:
							$('#comment-count').html(++comment_count);
						break;
						}
					}
					else {
						$('#comments').after('<h5 class="section-title"><span id="comment-count">1</span> Kommentar</h5><ol id="commentlist"></ol>');
					}
				}
			
				if (comment_parent == 0) {
					$(xhr.responseText).prependTo($('#commentlist')).addClass('new-comment').css('opacity', 0.01);
				}
				else {
					$('#cancel-comment-reply-link').click();
					var parent_node = '#li-comment-' + comment_parent;
					
					if($(parent_node + ' ol.children').length == 0) {
						$('<ol class="children"></ol>').appendTo(parent_node);
					}
					$(xhr.responseText).appendTo($(parent_node + ' ol.children')).addClass('new-comment').css('opacity', 0.01);
				}
				
				fixCommentsLayout($('#commentlist'));
				li_even = true;
				
				$('.new-comment.depth-3 .reply').remove();
				
				$(target).animate({scrollTop: $('.new-comment').offset().top}, 600, function() { $('.new-comment').fadeTo(800, 1.0, function() { $('.new-comment').removeClass('new-comment'); }); });
				
				$('#comment').val('');
				$('#commentform').attr('disabled', 'disabled');
				setTimeout('$("#commentform").removeAttr("disabled")',15000);
			}
			else if (status == 'error') {
				$('#error').html(xhr.responseText).fadeIn(800);
				$("#commentform").removeAttr("disabled");
			}
			$('#preview').hide();
			$('#commentload').hide();
		}
	});
}

function AjaxPreview() {
	$.ajax({
		context: $('#preview'), 
		url: 'http://queerbeet.org/wp-content/themes/unnamed/comments-preview.php',
		datatype: 'html',
		data: $("#commentform").serialize(),
		type: 'POST',
		beforeSend: function() {
			$('#commentload').show();
			$('#error').html('');
			$('#error').hide();
			$('#preview').hide();
			$('#commentform').attr('disabled', 'disabled');
		},
		success: function(data) {
			$('#preview').html(data).fadeIn(1000);
			$("#commentform").removeAttr("disabled");
			$('#commentload').hide();
		}
	});
}

function initComment() {
	if ( $('#commentform').length != 0 ) {
		$('#commentform').submit(function() { AjaxComment(); return false; });
		$('#previewcomment').click(function() { AjaxPreview(); return false; });
		$("#commentform").before('<div id="preview"></div>');
		$("#submit").after('<span id="error"></span>');
		$("#submit").after('<span id="commentload"></span>');
		$('#commentload').hide();
		$('#preview').hide();
		$('#error').hide();
		if ($.browser.opera) target = 'html';
 	}
}
$(document).ready(function() { initComment(); } );

