/*
	強引にフォントサイズを変更させるスクリプト
	要 jQuery, CookieManager（保存機能を使うとき）
	
	Yuichi Takeuchi
	http://takeyu-web.com/
*/

var SizeChanger = new Object;

// バージョン情報
// SizeChanger.version.string() => "1.0.0"
SizeChanger.version = {
	major	: 1,
	minor	: 0,
	tiny	: 1
};
SizeChanger.version.string = function(){
	return this.major + '.' +this.minor + '.' + this.tiny;
}
	
// 変化幅
SizeChanger.magnifications = [-1, 0, 2];
	
SizeChanger.index = Math.floor(SizeChanger.magnifications.length / 2);

(function ($){
	/* 状態復元 */
	SizeChanger.load = function() {
		if(typeof(CookieManager) == 'undefined')
			return false;
		var loadedIndex = CookieManager.get('SizeChanger.index');
		if(loadedIndex == '')
			return false;
		
		this.change(parseInt(loadedIndex));
		
		return true;
	}
	
	/* 状態保存 */
	SizeChanger.save = function() {
		if(typeof(CookieManager) == 'undefined')
			return false;
		CookieManager.set('SizeChanger.index', this.index);
	}
	
	/* サイズ変更 */
	SizeChanger.change = function(newIndex) {
		if(this.index == newIndex || newIndex < 0 || newIndex + 1 > this.magnifications.length)
			return false;
		
		this.onChange(newIndex);
		
		var rex = RegExp("\\S");
		jQuery.each(jQuery.makeArray($(document.body).find('*:not(script,style):parent').contents()), function(){
			if(this.nodeType != 3) return;
			if(!this.data.match(rex)) return;
			var textNode = $(this);
			if(textNode.parent()[0].className == "_size-rewrite") return;
			textNode.wrap('<span class="_size-rewrite" />');
		});
		
		this.appendRewriter(newIndex);
		
		var oldIndex = this.index;
		this.index = newIndex;
		
		this.onChanged(oldIndex);
		
		this.save();
		
		return true;
	}
	
	/* 1段階小さく */
	SizeChanger.lower = function(){
		return this.change(this.index - 1);
	}
	
	/* 1段階大きく */
	SizeChanger.upper = function(){
		return this.change(this.index + 1);
	}
	
	/* 一番大きく */
	SizeChanger.maximum = function(){
		return this.change(this.magnifications.length - 1);
	}
	
	/* 一番小さく */
	SizeChanger.minimum = function(){
		return this.change(0);	
	}
	
	/* 上限か？ */
	SizeChanger.isMaximum = function(index){
		if(typeof(index) == 'undefined') index = this.index;
		return index >= this.magnifications.length - 1;
	}
	
	/* 下限か？ */
	SizeChanger.isMinimum = function(index){
		if(typeof(index) == 'undefined') index = this.index;
		return index <= 0;
	}
	
	/* サイズ更新用スタイルのセット */
	SizeChanger.appendRewriter = function(size){
		var style = $('#_size-rewriter');
		if(style.length == 0) {
			style = $('<link id="_size-rewriter" rel="stylesheet" type="text/css" charset="shift_jis" />');
			style.appendTo('head');
		}
		style.attr('href', '/css/sizechanger/'+ size + '.css');
	}
	
	/* コールバックの設定 上書きして使う */
	SizeChanger.onChange = function(newIndex){}
	SizeChanger.onChanged = function(oldIndex){}

})(jQuery);
