/* --------------------------------------------------------------------------------------

 * fontsize.js

 * version 1.02

 * Required common.js ver1.20 later

-------------------------------------------------------------------------------------- */

var fontSettings = {

	defaultSize: 'M',

	imgPath: '/common_v2/img/',

	parentTagName: 'ul',

	childTagName: 'li',

	childTagClassName: ['sizeS', 'sizeM', 'sizeL'],

	prefixTag: '<img src="/common_v2/img/txt_fontsize.gif" width="55" height="21" alt="文字サイズ" class="fontsizeTxt">',

	sawpImage: true,

	sizeInfo: [

		{

			name: 'S',

			percent: 65,

			alt: '小',

			imgOffSrc: 'btn_fontsize_s.gif',

			imgOnSrc:  'btn_fontsize_s_on.gif',

			width: 22,

			height: 21

		},

		{

			name: 'M',

			percent: 76,

			alt: '中',

			imgOffSrc: 'btn_fontsize_m.gif',

			imgOnSrc:  'btn_fontsize_m_on.gif',

			width: 22,

			height: 21

		},

		{

			name: 'L',

			percent: 100,

			alt: '大',

			imgOffSrc: 'btn_fontsize_l.gif',

			imgOnSrc:  'btn_fontsize_l_on.gif',

			width: 22,

			height: 21

		}

	]

};



/* start */

function fncFontSize(id){

	new FontSize(

		id,

		fontSettings

	);

}



var FontSize = function(id) {this.initialize.apply(this, arguments)}

FontSize.prototype = {

	cookieName: 'fontsize',

	cookieExpireDay: 7,



	initialize: function (id, settings) {

		this.id = id;

		this.targets = [];

		this.settings = settings;

		this.preloadObject();

	},



	/* preload images */

	preloadObject: function () {

		var targetsImgs = new Array();

		var targetsOnImgs = new Array();

		for (i = 0; i < this.settings.sizeInfo.length; i++){

			var sizeInfo = this.settings.sizeInfo[i];

			var img = new Image();

			img.src = this.settings.imgPath + sizeInfo.imgOffSrc;

			targetsImgs.push(img);

			var imgOn = new Image();

			imgOn.src = this.settings.imgPath + sizeInfo.imgOnSrc;

			targetsOnImgs.push(imgOn);

		}

		this.setObject();

	},



	/* set tags */

	setObject: function () {

		/* eat a cookie */

		this.setSize = comGetCookie(this.cookieName);

		if (!this.setSize) {

			this.setSize = this.settings.defaultSize;

		}

		/* set tags */

		var tags = '<div id="' + this.id + '">';

		if (this.settings.prefixTag) {

			tags += this.settings.prefixTag;

		}

		if (this.settings.parentTagName) {

			tags += '<' + this.settings.parentTagName + '>';

		}

		for (i = 0; i < this.settings.sizeInfo.length; i++) {

			var sizeInfo = this.settings.sizeInfo[i];

			if (this.settings.childTagName) {

				tags += '<' + this.settings.childTagName + ' class="' + this.settings.childTagClassName[i] + '">';

			}

			tags +=

				'<img id="' + this.id + sizeInfo.name +

				'" src="' + this.settings.imgPath + (this.setSize == sizeInfo.name ? sizeInfo.imgOnSrc : sizeInfo.imgOffSrc) +

				'" alt="' + sizeInfo.alt +

				'" width="' + sizeInfo.width +

				'" height="' + sizeInfo.height +

				'" />';

			if (this.settings.childTagName) {

				tags += '</' + this.settings.childTagName + '>';

			}

		}

		if (this.settings.parentTagName) {

			tags += '</' + this.settings.parentTagName + '>';

		}

		tags += '</div>';

		window.document.write(tags);



		for (i = 0; i < this.settings.sizeInfo.length; i++) {

			var sizeInfo = this.settings.sizeInfo[i];

			/* set event to size-button */

			var elem = document.getElementById(this.id + sizeInfo.name);

			elem.size = sizeInfo.name;

			elem.style.cursor = 'pointer';

			this.targets.push(elem);

			this.addEvent(elem);

			if (this.settings.sawpImage && typeof VCOMN.swapImageInit == 'function') {

				VCOMN.swapImageInit(elem, true);

			}

		};

		this.setFontSize(this.setSize);

	},



	/* add mouseEvent */

	addEvent: function (elem) {

		elem.onclick = (function(_this, _elem) {

			return function() {

				_this.onClick(_elem);

			};

		})(this, elem);

	},



	onClick: function (elem) {

		if (!elem) return;

		this.setFontSize(elem.size);

	},



	setFontSize: function (name) {

		var percent = 0;

		for (i = 0; i < this.settings.sizeInfo.length; i++) {

			var sizeInfo = this.settings.sizeInfo[i];

			if (sizeInfo.name == name) {

				percent = sizeInfo.percent;

				this.targets[i].src = this.settings.imgPath + sizeInfo.imgOnSrc;

				if (this.settings.sawpImage && this.targets[i].onmouseover) {

					this.targets[i]._onmouseover = this.targets[i].onmouseover;

					this.targets[i].onmouseover = null;

					this.targets[i]._onmouseout = this.targets[i].onmouseout;

					this.targets[i].onmouseout = null;

				}

			} else {

				this.targets[i].src = this.settings.imgPath + sizeInfo.imgOffSrc;

				if (this.settings.sawpImage && this.targets[i]._onmouseover) {

					this.targets[i].onmouseover = this.targets[i]._onmouseover;

					this.targets[i]._onmouseover = null;

					this.targets[i].onmouseout = this.targets[i]._onmouseout;

					this.targets[i]._onmouseover = null;

				}

			}

		}

		if (percent != 0) {

			document.body.style.fontSize = percent + '%';

			/* bake a cookie */

			comSetCookie(this.cookieName, name, this.cookieExpireDay, null, '/');

		}

	}

};




