﻿var contentSwitchFontSize = {
	
	conf : {
		fontSize     : ["115%","100%","85%"],
		switchId     : ["switchFontSizeBig", "switchFontSizeMedium", "switchFontSizeSmall"],
		defoSizeId   : "switchFontSizeMedium",
		targetAreaId : "center_bar",             //フォントサイズを適用するエリアのID
		targetAreaId2: "mailer1",              //フォントサイズを適用するエリアのID
		targetAreaId3: "mailer2",              //フォントサイズを適用するエリアのID
		cookieName   : "FontSizeChange",      //クッキーの名前
		cookieLimit  : 30,                     //有効期限(日にち)
		switchWriteArea : "font-change",          //指定したIDのエリアの一番最後にスイッチのHTMLが書き込まれる
		switchHTML      : '<ul><li id="switchFontSizeSmall" style="text-indent:-9999px;"><span>小</span></li><li id="switchFontSizeMedium" style="text-indent:-9999px;"><span>中</span></li><li id="switchFontSizeBig" style="text-indent:-9999px;"><span>大</span></li></ul>'
	},
	main : function(){
		contentSwitchFontSize.setHTML();
		contentSwitchFontSize.defo();
		var i, j, switchItem = contentSwitchFontSize.conf.switchId;
		for(i=0;i<switchItem.length;i++){
			document.getElementById(switchItem[i]).onclick = contentSwitchFontSize.action;
		}
	},
	
	setHTML : function(){
		var fontsizeSwitch = document.createElement('div'); 
		fontsizeSwitch.id  = "fontsizeControl";
		fontsizeSwitch.innerHTML = contentSwitchFontSize.conf.switchHTML; 
		document.getElementById(contentSwitchFontSize.conf.switchWriteArea).appendChild(fontsizeSwitch);
	},
	
	defo : function(){
		var i;
		var switchId = contentSwitchFontSize.conf.switchId;
		var targetAreaId = contentSwitchFontSize.conf.targetAreaId;
		var targetAreaId2 = contentSwitchFontSize.conf.targetAreaId2;
		var targetAreaId3 = contentSwitchFontSize.conf.targetAreaId3;
		var fontSize = contentSwitchFontSize.conf.fontSize;
		cookieValue = this.getCookie() || contentSwitchFontSize.conf.defoSizeId;
		for(i = 0; i < switchId.length; i++){
			if(cookieValue == switchId[i]){
				document.getElementById(targetAreaId).style.fontSize = fontSize[i];
if( document.getElementById(targetAreaId2) )
{
				document.getElementById(targetAreaId2).contentWindow.document.body.style.fontSize = fontSize[i];
}
if( document.getElementById(targetAreaId3) )
{
				document.getElementById(targetAreaId3).contentWindow.document.body.style.fontSize = fontSize[i];
}
			}
		}
		document.getElementById(cookieValue).className ="active";
	},
	
	action : function(){
		var i;
		var switchId = contentSwitchFontSize.conf.switchId;
		var targetAreaId = contentSwitchFontSize.conf.targetAreaId
		var targetAreaId2 = contentSwitchFontSize.conf.targetAreaId2
		var targetAreaId3 = contentSwitchFontSize.conf.targetAreaId3
		var fontSize = contentSwitchFontSize.conf.fontSize
		for(i=0;i<switchId.length;i++){
			var switchItem = document.getElementById(switchId[i]);
			switchItem.className="";
			if(this.id == switchId[i]){
				document.getElementById(targetAreaId).style.fontSize = fontSize[i];
if( document.getElementById(targetAreaId2) )
{
				document.getElementById(targetAreaId2).contentWindow.document.body.style.fontSize = fontSize[i];
}
if( document.getElementById(targetAreaId3) )
{
				document.getElementById(targetAreaId3).contentWindow.document.body.style.fontSize = fontSize[i];
}
			}
		}
		this.className ="active";
		
		contentSwitchFontSize.setCookie(this.id);
	},
	
	setCookie: function(data) {
		var today = new Date();
		today.setTime(today.getTime() + (1000 * 60 * 60 * 24 * Number(this.conf.cookieLimit)));
		document.cookie = this.conf.cookieName + '=' + encodeURIComponent(data) + '; path=/; expires=' + today.toGMTString();
	},
	
	getCookie: function(m) {
		return (m = ('; ' + document.cookie + ';').match('; ' + this.conf.cookieName + '=(.*?);')) ? decodeURIComponent(m[1]) : null;
	},
	addEvent : function(){
Event.observe(window, "load", this.main, false);
	}
}
contentSwitchFontSize.addEvent();
