var ContactForm = Class.create({
	serviceUrl: "/index.php",
	initialize: function(el) {
		el.observe("click", this.cbContactBtnClick.bind(this));
		this.contactForm = $E("div", {id: "form_contact"}).appendChildElements([
			$E("div").addClassName("window_title").update("Contact me").setOpacity(1.0),
			this.windowBody = $E("div").addClassName("window_content").appendChildElements([
				this.eltMsgDiv = $E("div").hide(),
				this.eltForm = $E("form", {method: "post", action: "", onsubmit: "javascript: return false;"}).appendChildElements([
					$E("div").appendChildElement($E("label").appendChildElements([$E("span").update("Name"), this.input_name = $E("input", {type: "text"})])),
					$E("div").appendChildElement($E("label").appendChildElements([$E("span").update("e-mail"), this.input_email = $E("input", {type: "text"})])),
					$E("div").appendChildElement($E("label").appendChildElements([$E("span").update("Subject"), this.input_subject = $E("input", {type: "text"})])),
					$E("div").appendChildElement($E("label").appendChildElements([$E("span").update("Message"), this.input_message = $E("textarea", {name: "msg"})])),
					$E("div").appendChildElements([
						$E("button").update("Send").observe("click", this.cbSendMessageBtnClick.bind(this)),
						$E("button").update("Clear").observe("click", this.cbClearFormBtnClick.bind(this)),
						$E("button").update("Close").observe("click", this.cbCloseMessageBtnClick.bind(this))
					]).addClassName("buttons")
				])
			]).setOpacity(1.0)
		]).addClassName("window_frame").setOpacity(0.85).hide();
		document.body.appendChild(this.contactForm);
	},
	cbContactBtnClick: function(event) {
		this.contactForm.show();
		this.eltForm.show();
		this.eltForm.enable();
		this.eltMsgDiv.hide();
		this.input_name.focus();
	},
	cbSendMessageBtnClick: function(event) {
		this.eltForm.disable();
		this.connection = new Ajax.Request(this.serviceUrl, {
			onSuccess: this.cbSendSuccess.bind(this),
			onFailure: this.cbSendFailure.bind(this),
			parameters: $H({
				operation: "sendmessage",
				name: $F(this.input_name),
				email: $F(this.input_email),
				subject: $F(this.input_subject),
				message: $F(this.input_message)
			}).toQueryString()
		});
	},
	cbClearFormBtnClick: function(event) {
		this.eltForm.reset();
	},
	cbCloseMessageBtnClick: function(event) {
		this.eltForm.reset();
		this.eltForm.enable();
		this.contactForm.hide();
	},
	cbSendSuccess: function(conn) {
		this.displayMessage("Your message has been sent!", "msgsendsuccess");
		this.cbCloseMessageBtnClick.bind(this).delay(5);
	},
	cbSendFailure: function(conn) {
		this.eltForm.enable();
		this.displayMessage("Sending the message failed.", "msgsendfail");
	},
	displayMessage: function(message, msgclass) {
		$w(this.eltMsgDiv.className).each(function (i) {this.eltMsgDiv.removeClassName(i);}.bind(this));
		this.eltMsgDiv.addClassName(msgclass).update(message).show();
	}
});

try {
	document.observe("dom:loaded", function() {
		window.contactForm = new ContactForm($("btn_contactme"));
	});
} catch (e) {
	window.status = 'Exception: ' + e.message;
	console.error(e);
}
