document.observe('dom:loaded', function() {
	var url = './mail_send.php';
	var current_mode = window.location.hash || 'i';

	var input = $('mail_form_input');
	var loading = $('mail_form_loading');
	var mf_confirm = $('mail_form_confirm');
	var thanks = $('mail_form_thanks');
	var contents = {};

	var keys = [];
	var requireds = [];
	var emails = [];
	function prepareInput(elm) {
		elm = $(elm);
		if (
			(! elm.tagName.match(/textarea/i))
			&& (! (elm.tagName.match(/input/i) && (! elm.type || elm.type.match(/text/i))))
		) {
			return;
		}
		if (! elm.name) {
			return;
		}

		if (elm.hasClassName('is_required')) {
			requireds.push(elm.name);
		}
		if (elm.hasClassName('is_email')) {
			emails.push(elm.name);
		}
		keys.push(elm.name);
	}
	$A($('mail_form').elements).each(prepareInput);


	keys.each(function(k) {
		$$('#mail_form_confirm .' + k).each(function(elm) {
			elm = $(elm);
			elm.observe('focus', function(ev) {
				ev.stop();
				elm.blur();
			});
		});
	});

	function sethash(value) {
		return;
		if (window.location.hash != value) {
			current_mode = window.location.hash = value;
		}
		else {
			current_mode = value;
		}
	}

	function fillinform() {
		$H(contents).each(function(e) {
			$$('#mail_form .' + e.key).each(function(elm) {
				elm = $(elm);
				var v = e.value;
				v = v.escapeHTML().gsub("\n", '<br />');
				var inner = elm.select('.value');
				if (inner && inner.length) {
					inner[0].innerHTML = v;
				}
				else {
					if (elm.tagName.match(/input|textarea/i)) {
						elm.value = e.value;
					}
					else {
						elm.innerHTML = v;
					}
				}
			});
		});
	}

	$('mail_preview').observe('click', function(ev) {
		ev.stop();

		var messages = [];
		keys.each(function(key) {
			var elm = $(key);
			var v = elm.value;
			var label = $$('label[for="' + key + '"]')[0];
			if (
				label
				&& (requireds.indexOf(key) != -1)
				&& (v.blank())
			) {
				var name = label.innerHTML;
				messages.push(name + 'は必ず入力してください。');

				elm.addClassName('input_error');
				label.addClassName('input_error');
			}
			else if (
				label
				&& (emails.indexOf(key) != -1)
				&& (! v.match(/.+@.+\..+/))
			) {
				var name = label.innerHTML;
				messages.push(name + 'が間違っています。');

				elm.addClassName('input_error');
				label.addClassName('input_error');
			}
			else {
				contents[key] = v;

				elm.removeClassName('input_error');
				if (label) {
					label.removeClassName('input_error');
				}
			}
		});

		if (messages.length) {
			alert(messages.join("\n"));
			return;
		}

		fillinform();

		new Effect.Parallel([
			new Effect.Fade(input),
			new Effect.Appear(loading)
		], {
			afterFinish: function() {
				setTimeout(function() {
					new Effect.Parallel([
						new Effect.Fade(loading),
						new Effect.Appear(mf_confirm)
					]);
					sethash('s');
				}, 200);
			}
		});
	});

	$('mail_edit').observe('click', function() {
		new Effect.Parallel([
			new Effect.Fade(mf_confirm),
			new Effect.Appear(loading)
		], {
			afterFinish: function() {
				setTimeout(function() {
					new Effect.Parallel([
						new Effect.Fade(loading),
						new Effect.Appear(input)
					]);
					sethash('i');
				}, 200);
			}
		});
	});

	$('mail_send').observe('click', function() {
		new Effect.Parallel([
			new Effect.Fade(mf_confirm),
			new Effect.Appear(loading)
		], {
			afterFinish: function() {
				new Ajax.Request(url, {
					method: 'post',
					parameters: contents,
					onComplete: function(transport) {
						new Effect.Parallel([
							new Effect.Fade(loading),
							new Effect.Appear(thanks)
						]);
						sethash('s');
					}
				});
			}
		});
	});

	if ($('mail_new')) {
		$('mail_new').observe('click', function() {
			keys.each(function(key) {
				$(key).value = '';
			});
			contents = {};

			new Effect.Parallel([
				new Effect.Fade(thanks),
				new Effect.Appear(loading)
			], {
				afterFinish: function() {
					setTimeout(function() {
						new Effect.Parallel([
							new Effect.Fade(loading),
							new Effect.Appear(input)
						]);
						sethash('i');
					}, 200);
				}
			});
		});
	}

	/*
	setInterval(function() {
		var next_mode = window.location.hash || 'i';
		if (current_mode != next_mode) {
			if (next_mode == 'i') {
				var enabled = null;
				[input, loading, mf_confirm, thanks].each(function(elm) {
					if (elm.style.display != 'none') {
						enabled = elm;
					}
				});
				new Effect.Parallel([
					new Effect.Fade(enabled),
					new Effect.Appear(loading)
				], {
					afterFinish: function() {
						setTimeout(function() {
							new Effect.Parallel([
								new Effect.Fade(loading),
								new Effect.Appear(input)
							]);
							sethash('i');
						}, 200);
					}
				});
			}
		}
	}, 300);
	*/
});
