/******************************
	いくくるウェブ基本JS（下記のことが出来ます）
	・外部リンクに外部アイコンをつける
	・スムーズスクロール
	・ロールオーバー
	・半透明のロールオーバー
	・現在のカテゴリ選択
	・スタイルキャッチャー（文字サイズ、背景色）
	・余計なフォントタグの除去
******************************/


/**********
	blank
**********/
$(document).ready(function() {
	$('#contents .pageLink a[href*=http]').each(function() {
		var href = $(this).attr("href");
		
		var regs = [];
		regs.push( new RegExp("^http://www.town.kumatori.lg.jp", "i") );
		
		//_blank
		var myDomain = false;
		
		for(var i=0; i < regs.length; i++) {
			if(  href.match(regs[i])  ) {
				myDomain = true;
				break;
			}
		}

		if(myDomain) {
			$(this).attr("target", "_self");
		}
		else {
			$(this).attr("target", "_blank");
			$(this).append('&nbsp;<img height="14" width="15" alt="外部サイトへ" src="/material/template/img_page/icon_blank.gif">');
		}
	});






	$('a[href*=http]').each(function() {
		var href = $(this).attr("href");
		
		var regs = [];
		regs.push( new RegExp("^http://www.town.kumatori.lg.jp", "i") );
		
		//_blank
		var myDomain = false;
		
		for(var i=0; i < regs.length; i++) {
			if(  href.match(regs[i])  ) {
				myDomain = true;
				break;
			}
		}

		if(myDomain) {
			$(this).attr("target", "_self");
		}
		else {
			$(this).attr("target", "_blank");
		}
	});








});







/**********
	smoothScroll
**********/
$(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,body').animate({scrollTop: jQuery.data($(this).get(0), "pos")}, 400);
						return false;
					});
				}
			}
		});
	};
});

$(document).ready(function() {
	$('a[href*=#]').filter(function(){
		return !$(this).parent().parent().parent().is(".cNavTabBox");
	}).smoothScroll();
});








/**********
	category
**********/
$(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') );
		}
	});
   
});








/**********
	rollover
**********/
$(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."));
		});
	});
});






/**********
	rollover2
**********/
$(document).ready(function(){
	$("img.JSroll").hover(function(){
		$(this).fadeTo(100, 0.7); // マウスオーバーで透明度を60%にする
	},function(){
		$(this).fadeTo(100, 1.0); // マウスアウトで透明度を100%に戻す
	});
});








/**********
	style
**********/
	$.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');
	};



	$(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"
		});
	});



/**********
	space
**********/
/*連続でファイルダウンロード一覧があった場合、スペースを短くする*/
$(document).ready(function(){
	/*連続でファイルダウンロード一覧があった場合、スペースを短くする*/
	var pageLinkFlg = false;
	$("#contents > *").each(function(idx){
		if( $(this).hasClass('pageLink') ) {
			// 前回のクラスが「pageLink」だったら
			if(pageLinkFlg) {
				$(this).addClass("top0");
			}
			pageLinkFlg = true;
		}
		else {
			pageLinkFlg = false;
		}
	});
});








$(document).ready(function(){
	/*pで囲まれていないテキストがあったら囲む*/
	$("div.wys").filter(function(){
		if( $($(this).find("p")).size() > 0 ) {
			return false;
		}
		if( $($(this).find("ul")).size() > 0 ) {
			return false;
		}
		if( $($(this).find("table")).size() > 0 ) {
			return false;
		}
		if( $($(this).find("div")).size() > 0 ) {
			return false;
		}
		
		return true;
	}).wrapInner("<p />");
});


/*先頭の要素はmargin-topをとる*/
$(document).ready(function(){
	$(".JSspace").each(function(idx){
		$(this).find("> *").each(function(idx2){
			if(idx2 == 0) {
				var tag = this.tagName;
				
				if(    tag.match(/^p$/i) || tag.match(/^ul$/i) || tag.match(/^div$/i) || tag.match(/^table$/i)   ) {
					$(this).addClass("top0");
				}
				return false;
			}
		});
	});
});





/**********
	font
**********/
$(document).ready(function() {
	$("p[style]").removeAttr("style");
	$("span[style]").removeAttr("style");
	$("font[size]").removeAttr("size");
	$("font[face]").removeAttr("face");
	$("font[color]").removeAttr("color");
});



/**********
	横並び画像
**********/
$(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());
	});
});

