﻿function _attachOnload(f) {
	if (window.addEventListener)
		window.addEventListener('load', f, false);
	else if (window.attachEvent)
		window.attachEvent('onload', f);
}


// FontChanger
// REQUIRES: prototype.js, cookiemanager.js
FontChanger = Class.create();
FontChanger.prototype = {
	id: null,
	cookieManager: null,
	cookieName: 'body.style.fontSize',
	initialize: function(id) {
		this.id = id || 'fontChanger';
		this.cookieManager = new CookieManager();
		var fontSize = this.cookieManager.getCookie(this.cookieName);
		if (fontSize) document.body.style.fontSize = fontSize;
	},
	setCookieShelfLife: function(days) {
		this.cookieManager.cookieShelfLife = days;
	},
	change: function(fontSize) {
		document.body.style.fontSize = fontSize;
		this.cookieManager.setCookie(this.cookieName, fontSize);
	},
	reset: function() {
		document.body.style.fontSize = '';
		this.cookieManager.clearCookie(this.cookieName);
	},
	show: function() {
		var id = this.id;
		document.writeln([
'<div id="' + id + '">',
'<span style="cursor: pointer; font-size: 80%;" id="' + id + '-small">小</span>',
'<span style="cursor: pointer; font-size: 100%;" id="' + id + '-medium">中</span>',
'<span style="cursor: pointer; font-size: 120%;" id="' + id + '-large">大</span>',
'</div>'
		].join("\n"));
		Event.observe($(id + '-small'), 'click', this.onClickSmall.bind(this));
		Event.observe($(id + '-medium'), 'click', this.onClickMedium.bind(this));
		Event.observe($(id + '-large'), 'click', this.onClickLarge.bind(this));
	},
	onClickSmall: function(e) { this.change('80%'); },
	onClickMedium: function(e) { this.change('100%'); },
	onClickLarge: function(e) { this.change('120%'); }
};
FontChanger.start = function(id) {
	var fontChanger = new FontChanger(id);
	fontChanger.setCookieShelfLife(90);
	fontChanger.show();
};

// CommentHandler
// REQUIRES: prototype.js, cookiemanager.js
var commenter_name; // for compatibility
CommentHandler = Class.create();
CommentHandler.prototype = {
	cookieManager: null,
	comments_form: null,
	commenter_name: null,
	initialize: function() {
		this.cookieManager = new CookieManager();
		this.commenter_name = this.getCookie('commenter_name');
		commenter_name = this.commenter_name; // for compatibility

	Event.observe(window, 'load', this.individualArchivesOnLoad.bind(this));
	},
	getCookie: function(key) {
		var val = this.cookieManager.getCookie(key);
		if (val) val = window.decodeURIComponent(val);
		return val;
	},
	setCookie: function(key, val) {
		if (val) val = window.encodeURIComponent(val);
		this.cookieManager.setCookie(key, val);
	},
	rememberMe: function() {
		var f = this.comments_form;
		if (f.bakecookie.checked) {
			if (f.author != undefined)
				this.setCookie('mtcmtauth', f.author.value);
			if (f.email != undefined)
				this.setCookie('mtcmtmail', f.email.value);
			if (f.url != undefined)
				this.setCookie('mtcmthome', f.url.value);
		}
	},
	forgetMe: function() {
		var f = this.comments_form;
		if (!f.bakecookie.checked) {
			var manager = this.cookieManager;
			manager.clearCookie('mtcmtmail');
			manager.clearCookie('mtcmthome');
			manager.clearCookie('mtcmtauth');
			f.email.value = '';
			f.author.value = '';
			f.url.value = '';
		}
	},
	individualArchivesOnLoad: function() {
		if (commenter_name) {
			Element.hide('name-email');
		} else {
			Element.show('name-email');
		}


		this.comments_form = document.forms['comments-form'];
		if (this.comments_form) this.fillCommentsForm();
	},
	fillCommentsForm: function() {
		var f = this.comments_form;
		Event.observe(f, 'submit', this.rememberMe.bind(this));
		var mtcmtmail, mtcmtauth, mtcmthome;
		if (!commenter_name) {
			if (f.email != undefined && (mtcmtmail = this.getCookie("mtcmtmail")))
				f.email.value = mtcmtmail;
			if (f.author != undefined && (mtcmtauth = this.getCookie("mtcmtauth")))
				f.author.value = mtcmtauth;
		}
		if (f.url != undefined && (mtcmthome = this.getCookie("mtcmthome")))
			f.url.value = mtcmthome;
		if (f.bakecookie != undefined) {
			f.bakecookie.checked = (mtcmtauth || mtcmthome) ? true : false;
			new Form.Element.EventObserver(f.bakecookie, this.forgetMe.bind(this));
		}
	},
	writeTypeKeyGreeting: function(entry_id) {
		if (this.commenter_name) {
			document.write('<div class="tk-status" title="TypeKey認証サービス: Now Signed In"><a href="http://as-is.net/mt/mt-comments.fcgi?__mode=handle_sign_in&amp;static=1&amp;logout=1&entry_id=' + entry_id + '" rel="nofollow" accesskey="O">TypeKeyからサイン・アウトする(<span class="accesskey">O</span>)</a></div><p>' + this.commenter_name + ' さんのTypeKeyアカウントを確認しました。</p>');
		} else {
			document.write('<div class="tk-status" title="TypeKey認証サービス: Not Signed In"><a href="https://www.typekey.com/t/typekey/login?&amp;lang=ja&amp;t=b1cpHmla2My5JUkUoW7L&amp;v=1.1&amp;_return=http://as-is.net/mt/mt-comments.fcgi%3f__mode=handle_sign_in%26static=1%26entry_id=' + entry_id + '" rel="nofollow" accesskey="I">TypeKeyにサイン・インまたは登録する(<span class="accesskey">I</span>)</a></div><p>TypeKey認証サービスにサイン・インしてください。サイン・インしない場合には、投稿されたコメントは要承認コメントとして取り扱われ、承認作業が完了するまで表示されないことがあります。TypeKeyアカウントをお持ちでない場合は上記リンクから登録することができます。</p>');

		}

	}
};
CommentHandler.start = function() {
	var commentHandler = new CommentHandler();
	Event.observe(window, 'load', function () { commentHandler.individualArchivesOnLoad(); });
};
