/*------------------------------------------------------------
  TextSizeController Ver 1.1
  [http://www.trustworks.biz/]
--------------------------------------------------------------
  [2008/04/26] クッキー有効範囲のPath設定を追加
------------------------------------------------------------*/
var TextSizeController = {
	
	/* プロパティ
	============================================================*/
	//使用テキストサイズ単位
	tsUnit:"%",
	//標準テキストサイズ
	normalSize:100,
	//変更単位サイズ
	unitSize:40,
	unitSizeMinus:20,
	//クッキー名
	cookieName:"tsc",
	//クッキー有効期限（日数）
	cookieExpDay:1,
	//クッキー無効時のアラートメッセージ
	cookieOffMsg:"Cookieを有効にして下さい。",
	//クッキー有効範囲
	cookiePath:"/",

	//現在の文字サイズ
	currentSize:"normal",

	/* bodyオブジェクト
	============================================================*/
	bodyObj:function(){
		var obj = document.getElementById('MainCont');
		if(obj == null){
			obj = document.getElementById('MainCont-Index');
		}

		return obj;
	},

	btnSmall:function(){
		return document.getElementById('btnSmall');
	},
	btnNormal:function(){
		return document.getElementById('btnNormal');
	},
	btnLarge:function(){
		return document.getElementById('btnLarge');
	},
	
	/* クッキー取得
	============================================================*/
	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);
		(key=="+")?latestSize+=this.unitSize:latestSize-=this.unitSizeMinus;
		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);
	},
	
	/* ボタン画像入れ替え 
	============================================================*/
	btnChange:function(size){
		switch(size){
			case 'large':
				document.getElementById('btnLarge').setAttribute("src", '/common/images/fsz_btn03_on.jpg');
				this.removeMouseEvent(document.getElementById('btnLarge'));
				document.getElementById('btnNormal').setAttribute("src", '/common/images/fsz_btn02_off.jpg');
				this.addMouseEvent(document.getElementById('btnNormal'));
				document.getElementById('btnSmall').setAttribute("src", '/common/images/fsz_btn01_off.jpg');
				this.addMouseEvent(document.getElementById('btnSmall'));
				break;
			case 'small':
				document.getElementById('btnLarge').setAttribute("src", '/common/images/fsz_btn03_off.jpg');
				this.addMouseEvent(document.getElementById('btnLarge'));
				document.getElementById('btnNormal').setAttribute("src", '/common/images/fsz_btn02_off.jpg');
				this.addMouseEvent(document.getElementById('btnNormal'));
				document.getElementById('btnSmall').setAttribute("src", '/common/images/fsz_btn01_on.jpg');
				this.removeMouseEvent(document.getElementById('btnSmall'));
				break;
			case 'normal':
				document.getElementById('btnLarge').setAttribute("src", '/common/images/fsz_btn03_off.jpg');
				this.addMouseEvent(document.getElementById('btnLarge'));
				document.getElementById('btnNormal').setAttribute("src", '/common/images/fsz_btn02_on.jpg');
				this.removeMouseEvent(document.getElementById('btnNormal'));
				document.getElementById('btnSmall').setAttribute("src", '/common/images/fsz_btn01_off.jpg');
				this.addMouseEvent(document.getElementById('btnSmall'));
				break;
			default:
				break;
		}
	},

	addMouseEvent:function(target){
		target.onmouseover = function() {
			this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
		}
		target.onmouseout = function() {
			this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
		}
	},
	
	removeMouseEvent:function(target){
		target.onmouseover = null;
		target.onmouseout = null;
	},

	/* 初期設定
	============================================================*/
	init:function(){
		var bodyStyle = this.bodyObj().style;
		//クッキーが存在する
		if(this.getCookie()){
			bodyStyle.fontSize = this.getCookie() + this.tsUnit;
			switch(bodyStyle.fontSize){
				case '140%':
					TextSizeController.btnChange('large');
					break;
				case '100%':
					TextSizeController.btnChange('normal');
					break;
				case '80%':
					TextSizeController.btnChange('small');
					break;
				default:
					break;
			}
		}
		//クッキーが存在しない
		else{
			bodyStyle.fontSize = this.normalSize + this.tsUnit;
		}
	}
};


/* イベント設定
============================================================*/
function setEventTSC(){
	var anchorObj = document.getElementsByTagName('a');
	var self = this;
	for(var i=0;i<anchorObj.length;i++){
		if(anchorObj[i].className.match("tsPlus")){
			anchorObj[i].onclick = function(){
				if(self.currentSize != 'large'){
					TextSizeController.sizeReset();
					TextSizeController.sizeControl('+');
				}
				self.currentSize = 'large';
				TextSizeController.btnChange(self.currentSize);
				return false;
			};
		}else if(anchorObj[i].className.match("tsMinus")){
			anchorObj[i].onclick = function(){
				if(self.currentSize != 'small'){
					TextSizeController.sizeReset();
					TextSizeController.sizeControl('-');
				}
				self.currentSize = 'small';
				TextSizeController.btnChange(self.currentSize);
				return false;
			};
		}else if(anchorObj[i].className.match("tsReset")){
			anchorObj[i].onclick = function(){
				TextSizeController.sizeReset();
				self.currentSize = 'normal';
				TextSizeController.btnChange(self.currentSize);
				return false;
			};
		}
	}
	//初期設定実行
	TextSizeController.init();
}


/* イベント設定実行
============================================================*/
if(window.addEventListener){
	window.addEventListener("load",setEventTSC,false);
}else if(window.attachEvent){
	window.attachEvent("onload",setEventTSC);
}

