var fontSizeSwitcher = {
	str: '',
	config: {
		length: 3,
		id: ['fontSizeSwitcherSmall', 'fontSizeSwitcherMedium', 'fontSizeSwitcherLarge'],
		img: ['site_fontsize_small', 'site_fontsize_medium', 'site_fontsize_large'],
		label: ['小', '中', '大'],
		size: ['77%', '100%', '122%'],
		cookieName: 'RMKFontSizeSwitcher',
		cookieDate: '90'
	},

	cookie: {
		set: function(n, v) {
			var t = new Date();
			t.setTime(t.getTime() + (1000 * 60 * 60 * 24 * fontSizeSwitcher.config.cookieDate));
			document.cookie = n + '=' + encodeURIComponent(v) + '; path=/; expires=' + t.toGMTString();
		},
		get: function(n, m) {
			return (m = ('; ' + document.cookie + ';').match('; ' + n + '=(.*?);')) ? decodeURIComponent(m[1]) : '';
		}
	},

	changeFontSize: function(i) {
		var config = this.config;
		var items  = document.getElementById('fontSizeSwitcher').childNodes;
		for(var j = 0; j < items.length; j++) {
			if(i == j) {
				items[j].setAttribute('class', 'current');
			}
			else {
				items[j].removeAttribute('class');
			}
		}

		if(document.getElementById('main')) {
			document.getElementById('main').style.fontSize = config.size[i];
		}
		else if(document.getElementById('siteInformation')) {
			document.getElementById('siteInformation').style.fontSize = config.size[i];
		}
		else if(document.getElementById('content')) {
			document.getElementById('content').style.fontSize = config.size[i];
		}
		else {
			document.body.style.fontSize = config.size[i];
		}

		RMT.createRollOver('img');

		for(var j = 0; j < config.length; j++) {
			var img = document.getElementById(config.id[j]).getElementsByTagName('img')[0];

			if(i == j) {
				img.src = '/shared/img/button/' + config.img[j] + '_on.gif';
			}
			else {
				img.src = '/shared/img/button/' + config.img[j] + '_off.gif';
			}
		}

		// set cookie
		this.cookie.set(config.cookieName, i);
	},

	start: function() {
		var config = this.config;
		var i      = this.cookie.get(config.cookieName, 's')

		for(var j = 0; j < config.length; j++) {
			this.str += '<li id="' + config.id[j] + '">';
			this.str += '<a href="javascript:void(0)" onclick="fontSizeSwitcher.changeFontSize(' + j + ')">';
			this.str += '<img src="/shared/img/button/' + config.img[j] + '_off.gif" alt="' + config.label[j] + '" />';
			this.str += '</a></li>';
		}

		document.write('<dl id="fontSizeController"><dt>文字の大きさ</dt><dd><ul id="fontSizeSwitcher">' + this.str + '</ul></dd></dl>');

		window.onload = function() {
			// get cookie
			if(i != '') {
				fontSizeSwitcher.changeFontSize(i);
			}
			else {
				fontSizeSwitcher.changeFontSize(1);
			}
		}
	}
}

fontSizeSwitcher.start();