(function($){

	$(function(){
		$("#fontSize").fontsizeChange();	//フォントサイズ変更ボタンを囲んでいる要素でスクリプト実行
	});

	$.fn.fontsizeChange = function(options){

		var o = $.extend({
			changeArea: "#wrapper",	//フォントサイズ変更エリア
			changeBtn: ".changeBtn",	//フォントサイズ変更ボタン
			fontSize: [70,75,95],	//フォントサイズ（HTMLと同じ並び順、幾つでもOK、単位は％）
			ovStr: "",				//ロールオーバー画像ファイル末尾追加文字列（ロールオーバー画像を使用しない場合は値を空にする）
			activeClass: "active",		//フォントサイズ変更ボタンのアクティブ時のクラス名
			cookieExpires: 7			//クッキー保存期間
		},options),
		ovStr2 = o.ovStr+".",
		btnArea = $(this),
		changeArea = $(o.changeArea),
		changeBtn = btnArea.find(o.changeBtn),
		sizeLng = o.fontSize.length,
		useImg = changeBtn.is("[src]") && o.ovStr!="";

		//現在クッキー確認関数
		function nowCookie(){
			return $.cookie("fontsize");
		}

		//画像切替関数
		function imgChange(elm1,elm2,str1,str2){
			elm1.attr("src",elm2.attr("src").replace(new RegExp("^(\.+)"+str1+"(\\.[a-z]+)$"),"$1"+str2+"$2"));
		}

		//マウスアウト関数
		function mouseOut(){
			for(var i=0;i<sizeLng;i++){
				if(nowCookie()!=o.fontSize[i]){
					imgChange(changeBtn.eq(i),changeBtn.eq(i),o.ovStr,"");
				}
			}
		}

		//フォントサイズ設定関数
		function sizeChange(){
			changeArea.css({fontSize:nowCookie()+"%"});
		}

		//クッキー設定関数
		function cookieSet(index){
			$.cookie("fontsize",o.fontSize[index],{path:'/',expires:o.cookieExpires});
		}

		//初期表示
		if(nowCookie()){
			for(var i=0;i<sizeLng;i++){
				if(nowCookie()==o.fontSize[i]){
					sizeChange();
					var elm = changeBtn.eq(i);
					if(useImg){
						imgChange(elm,elm,"",o.ovStr);
					}
					elm.addClass(o.activeClass);
					break;
				}
			}
		}
		else {
			cookieSet(0);
			sizeChange();
			var elm = changeBtn.eq(0);
			if(useImg){
				imgChange(elm,elm,"",o.ovStr);
			}
			else {
				elm.addClass(o.activeClass);
			}
		}

		if(useImg){

			//ホバーイベント＆プリロード
			changeBtn.each(function(){

				var self = $(this);

				self.hover( //ホバーイベント
				function(){
					if(!self.attr("src").match(ovStr2)){
						imgChange(self,self,"",o.ovStr);
					}
				},
				function(){
					if(self.attr("src").match(ovStr2)){
						mouseOut();
					}
				})
				.each(function(){ //プリロード
					if(!self.attr("src").match(ovStr2)){
						imgChange($("<img>"),self,"",o.ovStr);
					}
				});

			});

		}

		//クリックイベント
		changeBtn.click(function(){

			var index = changeBtn.index(this),
			self = $(this);

			cookieSet(index);
			sizeChange();

			if(useImg){
				mouseOut();
			}
			if(!self.hasClass(o.activeClass)){
				changeBtn.not(this).removeClass(o.activeClass);
				self.addClass(o.activeClass);
			}

		});

	}

})(jQuery);

