/******************************
	いくくるウェブ基本JS（下記のことが出来ます）
	・外部リンクに外部アイコンをつける
	・スムーズスクロール
	・ロールオーバー
	・半透明のロールオーバー
	・現在のカテゴリ選択
	・スタイルキャッチャー（文字サイズ、背景色）
	・余計なフォントタグの除去
******************************/


/**********
	リンクが外部であったら自動で別窓表示
**********/
$(document).ready(function() {
	$('#contents .linkCom a[href*=http],#contents .linkListMod a[href*=http],.bannerMod a[href*=http],.list  a[href*=http]').each(function() {
		var href = $(this).attr("href");
		
		var regs = [];
		regs.push( new RegExp("/www.city.ibaraki.osaka.jp", "i") );
		regs.push( new RegExp("/city.ibaraki.osaka.jp", "i") );
		regs.push( new RegExp("^http://210.189.169.3", "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;
		});
	});
});


/**********
	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').animate({scrollTop: jQuery.data($(this).get(0), "pos")}, 400, "linear", function(){
							location.hash = hrefAry[2];
						});
						return false;
					});
				}
			}
		});
	};
});

$(document).ready(function() {
	$('a[href*=#]').filter(function(){
		return !$(this).parent().parent().parent().is("#tab");
	}).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%に戻す
	});
});











/**********
	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;
		}
	});
});
*/






/**********
	pで囲まれていないテキストがあったら囲む
**********/
/*
$(document).ready(function(){
	$("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;
			}
		});
	});
});
*/





/**********
	横並び画像
**********/
/*
$(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());
	});
});
*/










/**********
	styleCatcherUpNormal
**********/
	$.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){
				$(this).attr("href", "#");//Tabで選択できるように空リンクを挿入
				
				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>
*/


	//テスト確認用（ドメインをIPに変換）
	/*
	$(document).ready(function() {
		$("a[href*=http]").each(function() {
			$(this).attr("href", $(this).attr("href").replace("/www.city.ibaraki.osaka.jp", "/210.189.169.3"));
			$(this).attr("href", $(this).attr("href").replace("/city.ibaraki.osaka.jp", "/210.189.169.3"));
		});
	});
	*/








	$(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>');
		}

		if( userAgent['browserVersion'] == '6' ) {

			$(".wysiwygCom").filter(function(){
				if($(this).find("img.L").size() == 0) {
					return false;
				}
				return true;
			}).find("ol,ul").css({
				'display':'inline-block',
				'padding-left':'40px'
			});



		}




	});













	$.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 .list").eqGroupHeight(2);
			//トップページの人口・世帯数
			$(".cat7Mod .table").eqGroupHeight(2);
		}
		setGroupHeight();
		setFontSizeListener(setGroupHeight);

		
	});






	$(document).ready(function() {
		$("#pankuzu2 li a[href*='/ibaraki/index.html']:contains('ええやん！茨木')").attr('href', '/koho/eeyan/');
	});







