function toggleBlogComments(id) {
	if ($('#buttonShowBlogComments-' + id).hasClass('hidden')) {
		$('#blogComments-' + id).slideUp('slow');
		$('#buttonShowBlogComments-' + id).removeClass('hidden').addClass('visible').html('Kommentare anzeigen');
	} else {
		$('#blogComments-' + id).slideDown('slow');
		$('#buttonShowBlogComments-' + id).removeClass('visible').addClass('hidden').html('Kommentare zuklappen');
	}
}

var addBlogCommentId;

function openDialogAddBlogComment(blogId, text) {
	addBlogCommentId = blogId;
	$('#input-AddBlogComment').val(text);
	$('#dialog-addBlogComment').dialog('open');
}

function addBlogComment() {
	$('#dialog-addBlogComment-errorMessage').hide();

	$.getJSON('/Blog/addComment/', {
		'blogId': addBlogCommentId,
		'comment': $('#input-AddBlogComment').val()
	}, function(response) {
		if (response.result == true) {
			// reload page
			location.reload(true);
		} else {
			$('#dialog-addBlogComment-errorMessage').html(response.error).show();
		}
	});
}

function openDialogAddBlogEntry() {
	$('#input-AddBlogEntry').val('');
	$('#dialog-addBlogEntry').dialog('open');
}

function addBlogEntry() {
	$('#dialog-addBlogEntry-errorMessage').hide();

	$.getJSON('/Blog/add/', {
		'title': $('#input-title-addBlogEntry').val(),
		'message': $('#input-message-AddBlogEntry').val(),
		'visibility': $("input[name='input-visibility-AddBlogEntry']:checked").val()
	}, function(response) {
		if (response.result == true) {
			location.reload(true);
		} else {
			$('#dialog-addBlogEntry-errorMessage').html(response.error).show();
		}
	});
}

$().ready(function() {
	// add new blog entry dialog
	$("#dialog-addBlogEntry").dialog({
		bgiframe: true,
		height: 560,
		width: 650,
		modal: true,
		resizable: false,
		autoOpen: false
	});

	// add new comment dialog
	$("#dialog-addBlogComment").dialog({
		bgiframe: true,
		height: 220,
		width: 420,
		modal: true,
		resizable: false,
		autoOpen: false
	});

	$("#dialog-addBlogCommentNotLoggedIn").dialog({
		bgiframe: true,
		height: 160,
		width: 440,
		modal: true,
		resizable: false,
		autoOpen: false
	});

	$('.addTagButtonActivate').click(function() {
		$('.addTagForm[blogId=' + $(this).attr('blogId') + ']').effect('slide');
		$(this).hide();
	});

	$('.addTagForm').submit(function() {
		var tagContainer = $('span[blogId='+$(this).attr('blogId') + ']');
		var blogId = $(this).attr('blogId');
		var tag = $('.tagInput[blogId=' + blogId + ']').val();

		var tagsAvailable = !$('#no-blogTags-' + blogId).is(':visible');
		tagContainer.children('#no-blogTags-' + blogId).hide();

		$.getJSON('/Blog/addTag/', {
			'tag': tag,
			'blogId': blogId
		}, function(response) {
			for (i in response.tags) {
				var newTag = $('.tagTemplate').clone();
				newTag.removeClass('tagTemplate');
				newTag.attr('tagId', response.tags[i].id);
				newTagHref = newTag.children('a').attr('href');
				newTag.children('a').attr('href', newTagHref + response.tags[i].tag).html(response.tags[i].tag);
				newTag.children('.deleteTagButton').attr('tagId', response.tags[i].id);
				if (tagsAvailable) {
					var lastTag = $('.tags[blogId=' + blogId + ']').children('.tag').last();
					lastTag.html(lastTag.html() + ', ');
				}
				tagContainer.append(newTag);
				newTag.show();
			}
		});

		$('.tagInput[blogId=' + blogId + ']').val('');
		return false;
	});

	$('.tagInput').focus(function() {
		if ($(this).val() == 'mehrere Tags durch Komma trennen') {
			$(this).val('').removeClass('tagInfoText');
		}
	});

	$('.deleteTagButton').live('click', function() {
		var tagId = $(this).attr('tagId');
		var tag = $(this).parent('span[tagId=' + tagId + ']');
		$.getJSON('/Blog/deleteTag/', {
			'tagId': tagId
		}, function(response) {
			if (response.result == true) {
				tag.fadeOut();
			}
		});
	});
});
