// setup parser for browsers which support it
if (window.DOMParser) {
	var parser = new DOMParser();
	var xmlDoc;
}

// accordion style revelations
function initPosts() {
	$('#posts div.bulk').hide();
	$('#posts div.title h2').click(
		function() {
			$(this).parents('.post').find('div.bulk').slideToggle('normal');
		}
	);
}

// new post button replaced by proper form
function newPost() {
	$('button.newpost').click(function() {
		$(this).replaceWith($('form.postTemplate:last').clone().attr('class', 'postForm'));
		allowPostSubmit();
		$('textarea.newbody').keyup(validateBody);
	});
}

// edit post button behavior
function editPost() {
	$('button.edit').click(function() {
		var title = $(this).parents('.post').find('span.title').html();
		var body = $(this).parents('.post').find('div.body').html();
		var postid = $(this).parents('.post').find('input:eq(1)').attr('value');
		var newForm = $('form.postTemplate:last').clone();

		title = title.replace(/&/g, '&amp;');
		title = title.replace(/</g, '&lt;');
		title = title.replace(/>/g, '&gt;');
		title = title.replace(/ xmlns="http:\/\/www.w3.org\/1999\/xhtml"/g, '');

		body = body.replace(/&/g, '&amp;');
		body = body.replace(/</g, '&lt;');
		body = body.replace(/>/g, '&gt;');
		body = body.replace(/ xmlns="http:\/\/www.w3.org\/1999\/xhtml"/g, '');

		newForm.attr('class', 'postForm');
		newForm.find('label:first').after('<label for="postid"><input class="postid" type="hidden" name="id" value="' + postid + '" /></label>');
		newForm.find('input.postaction').attr('value', 'edit');
		newForm.find('input.newtitle').attr('value', title);
		newForm.find('textarea.newbody').append(body);

		$(this).parents('.post').replaceWith(newForm);
		allowPostSubmit();
		$('textarea.newbody').keyup(validateBody);
	});
}

// XML validation while we type
function validateBody() {

	var body = '<body>' + $(this).val() + '</body>';
	var errorMsg = null;

	if (body != '<body></body>') {

		if (parser != undefined) {

			try {
				xmlDoc = parser.parseFromString(body, 'text/xml');
			} catch (e) {};

		}

		if (xmlDoc.documentElement.nodeName == 'parsererror') {
			errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
		}

	}

	if (errorMsg) {
		$(this).parents('.postForm').find('p.validationMsg').text(errorMsg);
		return false;
	} else {
		$(this).parents('.postForm').find('p.validationMsg').text('no errors');
		return true;
	}

}

function allowPostSubmit() {

	$('form.postForm').submit(function() {
		var title = $(this).find('input.newtitle').val();
		var body = '<body>' + $(this).find('textarea.newbody').val() + '</body>';

		if (title == undefined || title == '') {
			return false;
		} else {

			if (parser != undefined) {
				try {
					xmlDoc = parser.parseFromString(body, 'text/xml');
				} catch (e) {};
			} else {
				alert('allowing post since no parser was found');
				return true;
			}

			if (xmlDoc.documentElement.nodeName == 'parsererror') {
				return false;
			} else {
				return true;
			}

		}
	});

}

// initialize this shit!
$(document).ready(function() {
	initPosts();
	newPost();
	editPost();
});
