// JavaScript Document

// ■□ 01-01.IEのロールオーバーで起こる背景画像再読み込み問題対応
// CSSのa:hoverなどで背景画像の切り替えを行っている際にIEで起こる問題の対応策。
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// 画像ロールオーバー(RollOver)
// Standards Compliant Rollover Script
// Author : Daniel Nolan
// http://www.bleedingego.co.uk/webdev.php
// imgタグに.buttonsを指定すると、ファイル名の末尾に
// _onとついた画像に自動ロールオーバー。
jQuery(document).ready(function($) {
	var postfix = '_on';
	$('a img.buttons').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.'))
		           + postfix
		           + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(
			function() {
				img.attr('src', src_on);
			},
			function() {
				img.attr('src', src);
			}
		);
	});
});



// jQueryを利用したタブメニュー
// http://weboook.blog22.fc2.com/blog-entry-72.html
// http://jsajax.com/jQueryUITabsArticle930.aspx
$(function() {
	 $('#rotate > ul').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
});

// ■□ 02-01.ページトップへするするスクロール(ReturnToTop)
// ページの先頭、引数を入れた場合は引数のIDの場所へなめらか動作でスクロールする関数。
// 使用例）
// <a href="#Header" onclick="ReturnToTop(); return false;">ページTOPへ</a>
// <a href="#Main" onclick="ReturnToTop('Main'); return false;">ページ本文へ</a>
function ReturnToTop(pos,now) {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = y4 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	if (!pos) {
		//ページトップへ
		window.scrollTo(Math.floor(x / 1.25), Math.floor(y / 1.25));
		if (x > 0 || y > 0) {
			window.setTimeout("ReturnToTop()", 25);
		}
	} else {
		//引数に入れたid名の場所へ(y軸限定)
		if (window.opera) {
			//でもOpera9未満は除外
			var usrAge = navigator.userAgent;
			var posStart = usrAge.indexOf("Opera",0);
			var posEnd = usrAge.indexOf(" ",posStart+6);
			if (posEnd <= 0) posEnd = usrAge.length;
			var AgeVer = usrAge.substring(posStart+6,posEnd);
			if (AgeVer < 9) {
				window.open("#"+pos,"_self")
				return false;
			}
		}
		var isMSIE = /*@cc_on!@*/false;
		var targetEle = document.getElementById(pos);

		while( targetEle ){
			y4 += targetEle.offsetTop;
			targetEle = targetEle.offsetParent;
			//IEの補正：上記計算で無視されてしまう各親要素のborder幅を加算
			if ((targetEle) && (isMSIE)) {
				y4 += (parseInt(getElementStyle(targetEle,"borderTopWidth","border-top-width")) || 0);
			}
		}

		var y5 = y4-y;
		y += Math.floor(y5 / 5);
		window.scrollTo(x, y);
		if ((!now || now != y)&&(y > y4-5 || y < y4-5)) {
			window.setTimeout('ReturnToTop("'+pos+'","'+y+'")', 25);
		}
	}
}
function getElementStyle(targetElm,IEStyleProp,CSSStyleProp) {
	var elem = targetElm;
	if (elem.currentStyle) {
		return elem.currentStyle[IEStyleProp];
	} else if (window.getComputedStyle) {
		var compStyle = window.getComputedStyle(elem,"");
		return compStyle.getPropertyValue(CSSStyleProp);
	}
}


// ■□ jQuery で target="_blank"　を使わず別ウィンドウ
/*
$(document).ready(function(){
　　$("a._blank").click(function(){
　　　　window.open(this.href,'_blank');
　　　　　　return false;
　　　　});
});
*/

/*==================================================================================
jQueryを使ったアイコン付加プラグイン
http://www.nk0206.com/life/2009/03/jquerypdf.html
=====================================================================================*/
jQuery(document).ready(function($) {
	// 外部リンク
	$("#main a[@href^=http]:not(:has(img))").each(function(){addTargetBlank(this)});
	// アイコン表示
	$("#main a[href$='pdf']").each(function(){addIconPdf(this)});
	$("#main a[href$='xls']").each(function(){addIconXls(this)});
	$("#main a[href$='doc']").each(function(){addIconDoc(this)});
	$("#main a[href$='zip']").each(function(){addIconZip(this)});
	$("#main a[href$='exe']").each(function(){addIconExe(this)});
	$("#main a[href$='feed']").each(function(){addIconFeed(this)});
	$("#main a[href ^='mailto:']").each(function(){addIconMailTo(this)});
});

/* 外部リンク */
function addTargetBlank(target){
	var self_url = location.hostname;
	var href = $(target).attr("href");
	if(href && href.indexOf("http://") != -1 && href.indexOf("http://www.371839.jp/") == -1 && href.indexOf(self_url) == -1){
		//$(target).addClass("extlink").attr("target","_blank");
		$(target).not(".noIcon").after("<span class='external'>&nbsp;</span>").attr("target","_blank");
	}
}

/* アイコン表示 */
function addIconPdf(target){
	$(target).not(".noIcon").after("<span class='pdf'>&nbsp;</span>").attr("target","_blank");
}
function addIconXls(target){
	$(target).not(".noIcon").after("<span class='xls'>&nbsp;</span>").attr("target","_blank");
}
function addIconDoc(target){
	$(target).not(".noIcon").after("<span class='doc'>&nbsp;</span>").attr("target","_blank");
}
function addIconZip(target){
	$(target).not(".noIcon").after("<span class='zip'>&nbsp;</span>");
}
function addIconExe(target){
	$(target).not(".noIcon").after("<span class='exe'>&nbsp;</span>");
}
function addIconFeed(target){
	$(target).not(".noIcon").after("<span class='feed'>&nbsp;</span>");
}
function addIconMailTo(target){
	$(target).not(".noIcon").after("<span class='mailto'>&nbsp;</span>");
}


// jQueryを利用した外部リンクアイコン表示
jQuery(document).ready(function($) {
	// 外部リンク
	$("ul.level02 > li > a").each(function(){addTargetBlank(this)});
});
jQuery(document).ready(function($) {
	// 外部リンク
	$("ul.level02 > li > ul > li > a").each(function(){addTargetBlank(this)});
});

/* 外部リンク */
function addTargetBlank(target){
	var self_url = location.hostname;
	var href = $(target).attr("href");
	if(href && href.indexOf("http://") != -1 && href.indexOf(self_url) == -1){
		$(target).prepend('<img src="/common/img/p_icon_window.gif" alt="別ウィンドウで表示します" align="absmiddle" />');
	}
}

