/******************************
	いくくるウェブ基本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.tajiri.lg.jp", "i") );
		regs.push( new RegExp("^http://www.town.tajiri.osaka.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).prepend('<img src="/material/template/img_page/icon_blank.gif" width="18" height="15" alt="外部サイトへ" />&nbsp;');
		}
	});
});







/**********
	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();
});


/**********
	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%に戻す
	});
});




/**********
	category
**********/
$(document).ready(function(){
	var reqUrl = location.pathname;
	var reqFstCat = reqUrl.split('/')[1];

	$("#gnav > 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|\.png)/,"_on"+'$1') );
		}
	});
   
});




/**********
	style
**********/
$(document).ready(function() {

	function offRollAndOn(_id) {
		// (off rollover) and on
		$('.jStyle').each(function(idx){
			
			var j2 = $(this);
			var id2 = j2.attr("id");
			var img2 = j2.find("img");
			
			// all event remove
			img2.unbind();
			
			if(_id == id2) {
				img2.attr("src", img2.attr("src").replace("_off.", "_on."));
			}
			else {
				img2.attr("src", img2.attr("src").replace("_on.", "_off."));
				
				// rollover
				img2.hover(function () {
					$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
				},function () {
					$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
				});
			}
		});
		
	}

	// cursor pointer
	$('.jStyle').attr("href", "#");

	// click(css change)
	$('.jStyle').click(function(){
		var j = $(this);
		var img = j.find("img");
		var id = j.attr("id");
		
		offRollAndOn(id);
		
		// css change
		$('#styleSize').attr("href", '/material/template/css/' + id + '.css');
		
		// cookie set
		$.cookie("styleSize", id, {path:'/'});
		
		return false;
	});
	
	// cookie get
	offRollAndOn($.cookie("styleSize"));
	
	// css change
	$('#styleSize').attr("href", '/material/template/css/' + $.cookie("styleSize") + '.css');
	
	
});






/**********
	space
**********/
$(document).ready(function(){
	/*連続でファイルダウンロード一覧があった場合、スペースを短くする*/
	var pageLinkFlg = false;
	$("#contents > *").each(function(idx){
		if( $(this).hasClass('pageLink') ) {
			// 前回のクラスが「pageLink」だったら
			if(pageLinkFlg) {
				$(this).addClass("top10");
			}
			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をとる*/
$(".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());
	});
});



