var ready = function(fnc){
	if ( document.addEventListener ) {
		DOMContentLoaded = function() {
			document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
			fnc();
		};
	} else if ( document.attachEvent ) {
		DOMContentLoaded = function() {
			// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
			if ( document.readyState === "complete" ) {
				document.detachEvent( "onreadystatechange", DOMContentLoaded );
				fnc();
			}
		};
	}
	this.readyBound = false;
	this.isReady = false;
	this.doScrollCheck = function() {
		if (this.isReady) {
			return;
		}
		try {
			// If IE is used, use the trick by Diego Perini
			// http://javascript.nwbox.com/IEContentLoaded/
			document.documentElement.doScroll("left");
		} catch(e) {
			setTimeout( this.doScrollCheck, 1 );
			return;
		}
		// and execute any waiting functions
		//fnc();
	}
	this.bindReady = function() {
		if (this.readyBound){
			return;
		}
		this.readyBound = true;
		// Catch cases where $(document).ready() is called after the
		// browser event has already occurred.
		if ( document.readyState === "complete" ) {
			// Handle it asynchronously to allow scripts the opportunity to delay ready
			return setTimeout( fnc, 1 );
		}
		// Mozilla, Opera and webkit nightlies currently support this event
		if ( document.addEventListener ) {
			// Use the handy event callback
			document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
			// A fallback to window.onload, that will always work
			//window.addEventListener( "load", fnc, false );
		// If IE event model is used
		} else if ( document.attachEvent ) {
			// ensure firing before onload,
			// maybe late but safe also for iframes
			document.attachEvent("onreadystatechange", DOMContentLoaded);
			// A fallback to window.onload, that will always work
			//window.attachEvent( "onload", fnc );
			// If IE and not a frame
			// continually check to see if the document is ready
			var toplevel = false;
			try {
				toplevel = window.frameElement == null;
			} catch(e) {}
			if ( document.documentElement.doScroll && toplevel ) {
				this.doScrollCheck();
			}
		}
	}
	this.bindReady();
}
var yomotsuSwitchFontSize = {
	
	conf : {
		fontSize    : ["125%","100%","80%"],
		switchId    : ["switchFontSizeBig", "switchFontSizeMedium", "switchFontSizeSmall"],
		defoSizeId  : "switchFontSizeMedium",
		targetAreaId: "top",              //フォントサイズを適用するエリアのID
		cookieName  : "yomotsuFontSize",      //クッキーの名前
		cookieLimit : 30,                     //有効期限(日にち)
		switchWriteArea : "fontchanger",          //指定したIDのエリアの一番最後にスイッチのHTMLが書き込まれる
		switchHTML      : '<ul id="fontSizeList"><li id="fontsizetext">文字サイズを変更</li><li id="switchFontSizeSmall">小</li><li id="switchFontSizeMedium">中</li><li id="switchFontSizeBig">大</li></ul>'

// switchHTMLの参考用デフォルト値
//
//  <ul>
//  <li id="switchFontSizeBig">大</li>
//  <li id="switchFontSizeMedium">中</li>
//  <li id="switchFontSizeSmall">小</li>
//  </ul>

	},
	
	main : function(){
		yomotsuSwitchFontSize.setHTML();
		yomotsuSwitchFontSize.defo();
		var i, j, switchItem = yomotsuSwitchFontSize.conf.switchId;
		
		for(i=0;i<switchItem.length;i++){
			document.getElementById(switchItem[i]).onclick = yomotsuSwitchFontSize.action;
		}
	},
	
	setHTML : function(){
		var fontsizeSwitch = document.createElement('div'); 
		fontsizeSwitch.id  = "fontsizeControl";
		fontsizeSwitch.innerHTML = yomotsuSwitchFontSize.conf.switchHTML; 
		
		document.getElementById(yomotsuSwitchFontSize.conf.switchWriteArea).appendChild(fontsizeSwitch);
	},
	
	defo : function(){
		var i;
		var switchId = yomotsuSwitchFontSize.conf.switchId;
		var targetAreaId = yomotsuSwitchFontSize.conf.targetAreaId;
		var fontSize = yomotsuSwitchFontSize.conf.fontSize;
		
		cookieValue = this.getCookie() || yomotsuSwitchFontSize.conf.defoSizeId;
		for(i = 0; i < switchId.length; i++){
			if(cookieValue == switchId[i]){
				document.getElementById(targetAreaId).style.fontSize = fontSize[i];
			}
		}
		document.getElementById(cookieValue).className ="active";
	},
	
	action : function(){
		var i;
		var switchId = yomotsuSwitchFontSize.conf.switchId;
		var targetAreaId = yomotsuSwitchFontSize.conf.targetAreaId
		var fontSize = yomotsuSwitchFontSize.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];
			}
		}
		this.className ="active";
		
		yomotsuSwitchFontSize.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(){
		if(window.addEventListener) {
			window.addEventListener("load", this.main, false);
		}
		else if(window.attachEvent) {
			window.attachEvent("onload", this.main);
		}
	}
	
}

//yomotsuSwitchFontSize.addEvent();
ready(function(){
	yomotsuSwitchFontSize.main();
});
