var TextSizeController = {
	
	/* プロパティ
	============================================================*/
	//使用テキストサイズ単位
	tsUnit:"px",
	//標準テキストサイズ
	normalSize:13,
	//変更単位サイズ
	unitSize:2,
	//クッキー名
	cookieName:"tsc",
	//クッキー有効期限（日数）
	cookieExpDay:10,
	//クッキー無効時のアラートメッセージ
	cookieOffMsg:"こちらのサイトの文字サイズをお調整になったら、保存するにはCookieの設定を有効にすることが必要です。",
	//クッキー有効範囲
	cookiePath:"/",
	
	/* bodyオブジェクト
	============================================================*/
	bodyObj:function(){
		return document.getElementsByTagName('body')[0];
	},
	
	
	/* クッキー取得
	============================================================*/
	getCookie:function(){
		var cd = document.cookie.split("; ");
		for(var i=0;i<cd.length;i++){
			if(cd[i].indexOf(this.cookieName + "=")>=0){
				var latestSetting = cd[i].split("=")[1];
				return latestSetting;
			}
		}
		return false;
	},
	
	
	/* クッキー書込み
	============================================================*/
	writeCookie:function(value){
		var ce = new Date();
		ce.setTime(ce.getTime() + (this.cookieExpDay*24*60*60*1000));
		document.cookie = this.cookieName + "=" + value + "; expires=" + ce.toGMTString() + "; path=" + this.cookiePath;
	},
	
	
	/* クッキー使用確認
	============================================================*/
	checkCookie:function(){
		if(!navigator.cookieEnabled){
			alert(this.cookieOffMsg);
			return false;
		}
		return true;
	},
	
	
	/* サイズ変更
	============================================================*/
	sizeControl:function(key){
		//クッキー使用確認実行
		this.checkCookie();
		var latestSize = parseInt(this.bodyObj().style.fontSize);
		var maxSize = this.normalSize + this.unitSize*2;
		var minSize = this.normalSize - this.unitSize*2;
		if (minSize<=latestSize && latestSize<=maxSize){ 
			//(key=="+")?latestSize+=this.unitSize:latestSize-=this.unitSize;
			if(minSize !=latestSize && latestSize != maxSize) {
				(key=="+")?latestSize+=this.unitSize:latestSize-=this.unitSize;
			} else if(minSize ==latestSize) {
				(key=="+")?latestSize+=this.unitSize:latestSize=latestSize;
			} else if(maxSize ==latestSize) {
				(key=="-")?latestSize-=this.unitSize:latestSize=latestSize;
			}
			this.bodyObj().style.fontSize = latestSize + this.tsUnit;
			
			//クッキー書込み実行
			this.writeCookie(latestSize);
			return false;
		}
	},
	
	
	/* サイズリセット
	============================================================*/
	sizeReset:function(){
		//クッキー使用確認実行
		this.checkCookie();
		this.bodyObj().style.fontSize = this.normalSize + this.tsUnit;
		//クッキー書込み実行
		this.writeCookie(this.normalSize);
	},
	
	
	/* 初期設定
	============================================================*/
	init:function(){
		var bodyStyle = this.bodyObj().style;
		//クッキーが存在する
		if(this.getCookie()){
			bodyStyle.fontSize = this.getCookie() + this.tsUnit;
		}
		//クッキーが存在しない
		else{
			bodyStyle.fontSize = this.normalSize + this.tsUnit;
		}
	}
};

/* イベント設定
============================================================*/
function setEventTSC(){
	var anchorObj = document.getElementsByTagName('a');
	for(var i=0;i<anchorObj.length;i++){
		if(anchorObj[i].className.match("tsPlus")){
			anchorObj[i].onclick = function(){
				TextSizeController.sizeControl('+');
				return false;
			};
		}else if(anchorObj[i].className.match("tsMinus")){
			anchorObj[i].onclick = function(){
				TextSizeController.sizeControl('-');
				return false;
			};
		}else if(anchorObj[i].className.match("tsReset")){
			anchorObj[i].onclick = function(){
				TextSizeController.sizeReset();
				return false;
			};
		}
	}
	//初期設定実行
	TextSizeController.init();
}


/* イベント設定実行
============================================================*/
if(window.addEventListener){
	window.addEventListener("load",setEventTSC,false);
}else if(window.attachEvent){
	window.attachEvent("onload",setEventTSC);
}

/*------------------------------------------------------------
  TextSizeController Ver 1.1
  [http://www.trustworks.biz/]
------------------------------------------------------------*/
