var mywedding = window.mywedding || {};
if (typeof mywedding.public == "undefined") mywedding.public = {};
if (typeof mywedding.public.MysiteBlog == "undefined") mywedding.public.MysiteBlog = {};
if (typeof mywedding.public.MysiteBlog.Comment == "undefined") mywedding.public.MysiteBlog.Comment = {};
 
mywedding.public.MysiteBlog.Comment.AddHandler = {
	disableHandler: false,
	form: null,
	formId: "c-blog-comment-form",
	buttonContainerId: "c-blog-comment-save",
	nameFieldId: "c-blog-comment-name",
	emailFieldId: "c-blog-comment-email",
	commentContainerId: "c-blog-comment-container",
	responseContainerId: "c-response-container",
	
	init:function() {
		YAHOO.util.Event.onContentReady(this.formId, this.initForm, this, true);
	},
	initForm:function(e) {
		document.getElementById(this.formId).onsubmit = function() {
			return mywedding.public.MysiteBlog.Comment.AddHandler.execute(this);
		}
		if (mywedding.cookies.get(escape("mw_session::LOGGED_IN")) != null) {
			document.getElementById(this.nameFieldId).style.display = "none";
			document.getElementById(this.emailFieldId).style.display = "none";
		}
	},
	success:function(o) {
		var response = YAHOO.lang.JSON.parse(o.responseText);
		if (typeof response.errorNotice == "string") {
			document.getElementById(this.responseContainerId).innerHTML = response.errorNotice;
			var buttonContainer = document.getElementById(this.buttonContainerId);
			if (buttonContainer.childNodes[0].nodeType == 1) {
				buttonContainer.childNodes[0].disabled = false;
				buttonContainer.childNodes[0].value = "Post Comment";
			}
		}
		else {
			document.getElementById(this.formId).style.display = "none";
			document.getElementById(this.responseContainerId).innerHTML = "<p>Thank you for posting a comment.</p>";
			if (typeof response.commentEntry == "string") {
				this.addComment(response.commentEntry);
			}
			else {
				document.getElementById(this.responseContainerId).innerHTML += "<p>Your comment will appear on our website after we have approved it.</p>";
			}
		}
	},
	failure:function(o) {
		this.disableHandler = true;
		this.form.submit();
	},
	execute:function(form) {
		if (this.disableHandler) {
			return true;
		}
		
		this.form = form;
		
		var buttonContainer = document.getElementById(this.buttonContainerId);
		if (buttonContainer.childNodes[0].nodeType == 1) {
			buttonContainer.childNodes[0].disabled = true;
			buttonContainer.childNodes[0].value = "Posting...";
		}
		
		try {
			var callback = { success:this.success, failure:this.failure, scope:this };
			YAHOO.util.Connect.setForm(form);
			YAHOO.util.Connect.asyncRequest(form.method, form.action, callback, 'format=json');
		}
		catch (e) {
			this.failure({});
		}
		return false;
	},
	addComment:function(commentEntry) {
		var container = document.getElementById(this.commentContainerId);
		var entryContainer = document.createElement("div");
		entryContainer.innerHTML = commentEntry;
		insertComment: {
			var foundH4 = false, lastClass = "c-even";
			for (var i = 0; i < container.childNodes.length; i++) {
				if (container.childNodes[i].nodeType == 1) {
					if (container.childNodes[i].tagName.toLowerCase() == "h4") {
						foundH4 = true;
					}
					else if (container.childNodes[i].tagName.toLowerCase() == "div") {
						lastClass = container.childNodes[i].className;
					}
				}
			}
			if (! foundH4) {
				var heading = document.createElement("h4");
				heading.className = "c-subtitle";
				heading.appendChild(document.createTextNode("Comments"));
				container.appendChild(heading);
			}
			entryContainer.className = lastClass == "c-odd" ? "c-even" : "c-odd";
			container.appendChild(entryContainer); 
		}
	}
}
mywedding.public.MysiteBlog.Comment.AddHandler.init();
