/******************************
	いくくるウェブ基本JS（下記のことが出来ます）
	・外部リンクに外部アイコンをつける
	・スムーズスクロール
	・ロールオーバー
	・半透明のロールオーバー
	・現在のカテゴリ選択
	・スタイルキャッチャー（文字サイズ、背景色）
	・余計なフォントタグの除去
******************************/


/**********
	リンクが外部であったら自動で別窓表示
**********/
$(document).ready(function() {
	$('#contents .linkCom a[href*=http],#contents .linkListMod a[href*=http]').each(function() {
		var href = $(this).attr("href");
		
		var regs = [];
		regs.push( new RegExp("/www.city.ritto.shiga.jp/", "i") );
		regs.push( new RegExp("/city.ritto.shiga.jp", "i") );
		regs.push( new RegExp("^http://210.189.168.17", "i") );
		
		//_blank
		var myDomain = false;
		
		for(var i=0; i < regs.length; i++) {
			if(  href.match(regs[i])  ) {
				myDomain = true;
				break;
			}
		}

		if(myDomain) {
			$(this).click(function(){
				window.open(this.href,'_self');
				return false;
			});
		}
		else {
			$(this).click(function(){
				window.open(this.href,'_blank');
				return false;
			});
			//$(this).prepend('（外部リンク）');
			//$(this).append('&nbsp;<img height="14" width="15" alt="外部サイトへ" src="/material/template/img_page/icon_blank.gif">');
		}
	});



});





/**********
	target="_blank"を使わず別窓表示
**********/
$(document).ready( function () {
	$('.JSblank').each(function(){
		$(this).prepend('（外部リンク）');
		//$(this).append('&nbsp;<img height="14" width="15" alt="外部サイトへ" src="./img_page/icon_blank.gif">');
		$(this).click(function(){
			window.open(this.href,'_blank');
			return false;
		});
	});
});



/**********
	target="_blank"を使わず別窓表示（外部リンクという文字なし）※主に画像リンクに使用
**********/
$(document).ready( function () {
	$('.JSblank2').each(function(){
		//$(this).append('&nbsp;<img height="14" width="15" alt="外部サイトへ" src="./img_page/icon_blank.gif">');
		$(this).click(function(){
			window.open(this.href,'_blank');
			return false;
		});
	});
});






/**********
	スムーズスクロール
**********/
$(document).ready(function() {
	$.fn.smoothScroll = function(config) {
		var target = this;
		
		config = jQuery.extend({
				hoge: "Default value 1",
				hoge2: "Default value 2"
			},config);

		target.each(function(){
			var href = $(this).attr("href");
			
			
			if(href=='#') {
				$(this).click(function() {
					return false;
				});
				return true;
			}
			
			var hrefAry = href.match(/^([^#]*)#([^#]+)$/,function(whole,$1){ return $1 });
			
			if(hrefAry) {
				var j = $( "#" + hrefAry[2] );
				if(j.size() > 0) {
					
					jQuery.data($(this).get(0), "pos", j.offset().top);
					
					$(this).click(function() {
						
						$('html').animate({scrollTop: jQuery.data($(this).get(0), "pos")}, 400, "linear", function(){
							location.hash = hrefAry[2];
						});
						return false;
					});
				}
			}
		});
	};
});

$(document).ready(function() {
	$('a[href*=#]').filter(function(){
		if($(this).parent().parent().parent().is("#tab") == true) {
			return false;
		}
		return true;
	}).smoothScroll();
});




/**********
	カテゴリ画像の現在地を自動で_onにする
**********/
$(document).ready(function(){
	var reqUrl = location.pathname;
	var reqFstCat = reqUrl.split('/')[1];

	$("#headerNav > li > a").each(function(idx){
		var lnkUrl = $(this).attr("href");
		var fstCat = lnkUrl.split('/')[1];
		if(reqFstCat == fstCat) {
			var jImg = $($(this).find("img").get(0));
			jImg.attr("src", jImg.attr("src").replace(/_off(\.gif|\.jpg)/,"_on"+'$1') );
		}
	});
   
});





/**********
	_offのファイル名の画像をロールオーバー
**********/
$(document).ready(function(){
	$("img[src*='_off.']").each(function(){
		
		$("<img>").attr("src", $(this).attr("src").replace("_off.", "_on."));
	
		$(this).hover(function () {
			$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
		},function () {
			$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
		});
	});
});




/**********
	画像のいらないロールオーバー（透明度変更）
**********/
$(document).ready(function(){
	$("img.JSroll").hover(function(){
		$(this).fadeTo(100, 0.7); // マウスオーバーで透明度を60%にする
	},function(){
		$(this).fadeTo(100, 1.0); // マウスアウトで透明度を100%に戻す
	});
});




/**********
	文字サイズ変更（標準 or 大きくする）
**********/
$.fn.styleCatcherUpNormal = function(config) {
	config = $.extend({
		btnCls: "scSize",
		cssID: "scSize",
		prAry: ['0.css', '1.css', '2.css', '3.css'],
		path: '/material/template/css/',
		def: 0
	},config);
	
	var btnCls = config.btnCls;
	var cssID = config.cssID;
	var prAry = config.prAry;//parameter array
	var pth = config.pth;
	var def = config.def;

	//parameter number
	var prNum;
	if($.cookie(cssID)) {
		prNum = Number($.cookie(cssID));
	}
	else {
		prNum = Number(def);
	}
	
	//_idはon画像、後はoffのロールオーバー
	function offRollAndOn(cl_) {
		$('.' + btnCls).each(function(idx){
			
			var j = $(this);
			var cl = j.attr("class");
			var img = j.find("img");
			
			img.unbind();
			
			if(cl.indexOf(cl_) != -1) {
				img.attr("src", img.attr("src").replace("_off.", "_on."));
				$(this).css("cursor", "auto");
			}
			else {
				img.attr("src", img.attr("src").replace("_on.", "_off."));
				$(this).css("cursor", "pointer").css("cursor", "hand");
				
				/*
				img.hover(function () {
					$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
				},function () {
					$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
				});
				*/
			}
		});
		
	}
	
	$('.' + btnCls).css("cursor", "pointer").css("cursor", "hand");

	$('.' + btnCls).click(function(){
		var j = $(this);
		var img = j.find("img");
		var cl = j.attr("class");
		
		if(cl.indexOf("up") != -1) {
			prNum++;
			if(prAry.length-1 <= prNum) {
				prNum = prAry.length-1;
				offRollAndOn("up");
			}
			else {
				offRollAndOn("dummy");//どちらも押せる状態の画像にする
			}
			
		}
		else if(cl.indexOf("normal") != -1) {
			prNum = 0;
			offRollAndOn("normal");
		}
		
		$('#' + cssID).attr("href", pth + prAry[prNum]);
		
		//prNumを保存
		$.cookie(cssID, prNum, {path:'/'});
		
		return false;
	});
	

	if(prAry.length-1 <= prNum) {
		prNum = prAry.length-1;
		offRollAndOn("up");
	}
	else if(0 >= prNum) {
		prNum = 0;
		offRollAndOn("normal");
	}
	else {
		offRollAndOn("dummy");//unbindを防ぐため
	}

	$('#' + cssID).attr("href", pth + prAry[prNum]);
};


$(document).ready(function() {
	$("body").append('<link rel="stylesheet" type="text/css" id="scSize" />');
	$.fn.styleCatcherUpNormal({
		btnCls: "scSize",
		cssID: "scSize",
		prAry: ['size_default.css', 'size_up.css', 'size_up2.css', 'size_up3.css'],
		pth: '/material/template/css/',
		def: 0
	});
});

/*
//HTML側の実装方法
<dl class="ls">
	<dd class="it3"><a class="scSize up">文字を大きくする</a></dd>
	<dd class="it4"><a class="scSize normal">文字を標準にする</a></dd>
</dl>
*/




/**********
	スタイルキャッチャー
**********/
$.fn.styleCatcher = function(config) {
	config = $.extend({
		btnCls: "scSize",
		cssID: "scSize"
	},config);
	
	var btnCls = config.btnCls;
	var cssID = config.cssID;
	
	function offRollAndOn(_id) {
		$('.' + btnCls).each(function(idx){
			var j2 = $(this);
			var id2 = j2.attr("id");
			var img2 = j2.find("img");
			
			img2.unbind();
			
			if(_id == id2) {
				img2.attr("src", img2.attr("src").replace("_off.", "_on."));
			}
			else {
				img2.attr("src", img2.attr("src").replace("_on.", "_off."));
				
				img2.hover(function () {
					$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
				},function () {
					$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
				});
			}
		});
		
	}

	$('.' + btnCls).attr("href", "#");

	$('.' + btnCls).click(function(){
		var j = $(this);
		var img = j.find("img");
		var id = j.attr("id");
		
		offRollAndOn(id);
		
		$('#' + cssID).attr("href", '/material/template/css/' + id + '.css');
		
		$.cookie(cssID, id, {path:'/'});
		
		return false;
	});
	
	var fileName;
	if($.cookie(cssID)) {
		fileName = $.cookie(cssID);
	}
	else {
		fileName = $($('.' + btnCls).get(0)).attr("id");
	}
	
	offRollAndOn(fileName);
	$('#' + cssID).attr("href", '/material/template/css/' + fileName + '.css');
};

//HTML側の記述
$(document).ready(function() {
/*
	$("body").append('<link href="/material/template/css/size_small.css" rel="stylesheet" type="text/css" id="scSize" />');
	$.fn.styleCatcher({
		btnCls: "scSize",
		cssID: "scSize"
	});
*/
	$("body").append('<link href="/material/template/css/color_normal.css" rel="stylesheet" type="text/css" id="scColor" />');
	$.fn.styleCatcher({
		btnCls: "scColor",
		cssID: "scColor"
	});
});



/**********
	XPのIE7と他のOSにメイリオのフォント指定
**********/
$(document).ready(function() {
	function getUserAgent() {
		var obj = {};
		obj['userAgent'] = navigator.userAgent;
		obj['os'] = null;
		obj['osVersion'] = null;
		obj['browser'] = null;
		obj['browserVersion'] = null;
		
		if(obj['userAgent'].match(/Win/)) {
			obj['os']='win';
		}

		if(obj['userAgent'].match(/Win(dows)?[ ]*(NT[ ]*5\.1|XP)/)) {
			obj['osVersion']='xp';
		}
		
		if(obj['userAgent'].match(/MSIE/)) {
			obj['browser']='ie';
			var ary = obj['userAgent'].match(/MSIE[ ]*([0-9]+)/);
			if(ary != null) {
				obj['browserVersion']=Number(ary[1]);
			}
		}
		
		return obj;
	}
	
	var userAgent = getUserAgent();
	if(  (userAgent['osVersion'] == 'xp' && userAgent['browserVersion'] == '7') || userAgent['osVersion'] != 'xp'  ) {
		$("body").append('<style type="text/css">body {font-family: "ヒラギノ角ゴ Pro W3", "Hiragino Kaku Gothic Pro", "メイリオ", Meiryo, Osaka, "ＭＳ Ｐゴシック", "MS PGothic", sans-serif !important;} </style>');

	}
});



/**********
	○段組のliの高さを揃える
**********/
	$.fn.eqGroupHeight = function(turn_) {
		var target = this;
		target.each(function(idx_){
			//揃えたい分確保した後、高さ揃える…ループ
			var ary = new Array();
			
			//一旦、高さをリセット
			$(this).find("> *").css('height', 'auto');
			
			$(this).find("> *").each(function(idx2_){
				ary.push($(this));
				if(ary.length >= turn_) {
					var max = 0;
					for (var i=0; i < ary.length; i++){
						max = Math.max(ary[i].outerHeight(), max);
					}
					for (var j=0; j < ary.length; j++){
						ary[j].height(max);
					}
					ary[0].addClass("firstLeftCom");//最初の要素にだけClassを振る
					ary = new Array();
				}
			});
			//未だ配列に残っている分を高さ揃える
			if(ary.length > 0) {
				var max = 0;
				for (var i=0; i < ary.length; i++){
					max = Math.max(ary[i].outerHeight(), max);
				}
				for (var j=0; j < ary.length; j++){
					ary[j].height(max);
				}
				ary[0].addClass("firstLeftCom");//最初の要素にだけClassを振る
			}
		});
	};
	$(document).ready(function() {
		
		function setFontSizeListener(func_) {
			var s = $("<span />")
				.text(" ")
				.hide()
				.appendTo("body");
			s.data("size", s.css("font-size"));
			s.data("timer", setInterval(function(){
				if(s.css("font-size") != s.data("size")) {
					s.data("size", s.css("font-size"));
					func_();
				}
			}, 1000));
			
		};
		
		function setGroupHeight() {
			//2列の目次テンプレート
			$(".linkListMod").eqGroupHeight(2);
		}
		setGroupHeight();
		setFontSizeListener(setGroupHeight);

		
	});




/**********
	横並び画像
**********/
$(document).ready(function() {
	$(".c2lMod").each(function() {
		var L = $(this).find(".L");
		var R = $(this).find(".R");
		
		var LImg = L.find("img");
		var RImg = R.find("img");
		
		L.width(LImg.width());
		R.width(RImg.width());
	});
});




/**********
	カレンダーの休日に色づけ
**********/
$(document).ready(function() {
	$('#shicho tr').each(function() {
		var tr = $(this);
		var day = tr.find("> *:eq(1)").text();

		if( day.indexOf('日') > -1 || day.indexOf('祝') > -1 ) {
			tr.addClass("sun");
		}
		else if( day.indexOf('土') > -1 ) {
			tr.addClass("sat");
		}
	});
});



/**********
	スマートフォンへのリンク
**********/
$(document).ready(function() {
	if ((navigator.userAgent.indexOf('iPhone') > 0 && navigator.userAgent.indexOf('iPad') == -1) || 
		navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0) {
		if(location.pathname.match(/^\/i\//) == null) {
			$("body").prepend('<p id="smartphone"><a href="/i' + location.pathname + '"><img src="/material/template/img_top/smartphone.gif" width="900" height="62" alt="スマートフォン専用サイトへ" /></a></p>');
		}
	}
});






/**********
	tableのalign属性削除
**********/
$(document).ready(function() {
	$("table").removeAttr("align");
});


