
// 無衝突モードにする
jQuery.noConflict();
/*=====================初期設定エリア===================*/
//フェード時間設定
var fadetime = 1000;

//待機時間
var waittime = 5000;

//　画像ファイル名
var cf_image = new Array(
'scene01.jpg'
);

//　URLの指定
//　※リンクを付けない場合は、スペースなしの空にする
var cf_url = new Array(
''
);

/*======================================================*/

// 交互にリストにクラスを設定するのスクリプト
// evenCellとoddCellの装飾はCSSで設定してください
jQuery(function() {
	jQuery('.sitemapBox:odd').addClass('evenBox');
	jQuery('.sitemapBox:even').addClass('oddBox');
	jQuery('.sitemapBox:last').addClass('lastBox');
});

jQuery(function() {
	jQuery('body#jaSite .sitemapBox:even').before('<p class="bodyRetop"><a href="#pageTop" class="rollover"><img src="/common/img/btn/btn_body_mid_top.png" alt="このページの先頭へ" /></a></p>');
	jQuery('body#enSite .sitemapBox:even').before('<p class="bodyRetop"><a href="#pageTop" class="rollover"><img src="/common/img/btn/btn_body_mid_en_top.png" alt="Return to Top" /></a></p>');
	jQuery('.bodyRetop:first').addClass('fisrt');
});

var overKey = "_on";
var activeKey = "_on";


/*
 * ロード時の自動ロールオーバー
 */
jQuery(function() {
	var classname = ".rollover";
	jQuery(classname).hover(
		function() {
			swapImg(jQuery(this).find("img, input:image"), overKey);
		},
		function() {
			swapImg(jQuery(this).find("img, input:image"), "");
		}
	);
	jQuery(classname).focus(function() {
			swapImg(jQuery(this).find("img, input:image"), overKey);
	});
	jQuery(classname).blur(function() {
			swapImg(jQuery(this).find("img, input:image"), "");
	});

    classname = ".rolloverarea";
	jQuery(classname).hover(
		function() {
			swapMap(jQuery(this).find("area"), overKey);
		},
		function() {
			swapMap(jQuery(this).find("area"), "");
		}
	);
	jQuery(classname).focus(function() {
			swapMap(jQuery(this).find("area"), overKey);
	});
	jQuery(classname).blur(function() {
			swapMap(jQuery(this).find("area"), "");
	});
});


/*
 * ロード時の自動プリロード
 */
jQuery(function() {
	var images = jQuery('img.preload, input.preload:image');
	for (var i = 0; i < images.length; i++) {
		var path = images[i].src;
		var suffix = path.substr(path.lastIndexOf("."));
		var basename = getBasename(path, overKey, activeKey);
		var img = document.createElement('img');
		if( img != null )
			img.src = basename + overKey + suffix;
	}
});

var navInitializer = function() {
	jQuery('img, input:image, map area').each(function(i) {
		if (jQuery(this).attr("id")) {
			var path = jQuery(this).attr("src");
			var suffix = path.substr(path.lastIndexOf("."));
			var basename = getBasename(path, overKey, activeKey);
			var img = document.createElement('img');
			if( img != null )
				img.src = basename + overKey + suffix;
			var classname = "." + jQuery(this).attr("id");
			
			// 画像のIDと同じclassの要素が親要素にあった場合は、activeにする。
			// IDと同じclass名の要素がなく、かつactiveKeyをもつsrcの画像の場合は、ノーマルにする
			if (jQuery(this).parents(classname).length > 0) {
				jQuery(this).attr("src", basename + activeKey + suffix);
			} else if (jQuery(this).attr("src").indexOf(activeKey) > 0) {
				jQuery(this).attr("src", basename + suffix);
			}
		}
	});
};

/*
 * ロード時の自動ナビゲーション要素変化
 */
jQuery(navInitializer);

function swapImg(img, key) {
	var path = img.attr("src");
	var basename = getBasename(path, overKey, activeKey);
	var suffix = path.substr(path.lastIndexOf("."));
	
	
	path = img.attr("id") && jQuery.find("."+img.attr("id")).length > 0
		 ? basename + activeKey + suffix
		 : basename + key + suffix;

	img.attr("src", path);
}

function swapMap(elm, key) {
    var img = jQuery(elm).parents(".rolloverarea").prev("img");
    swapImg(img, key);
}

/*
 * ベース名を取り出す
 */
function getBasename(src, key, activeKey) {
	var path = src.substr(0, src.lastIndexOf('.'));
	if (path.lastIndexOf(key) == (path.length - key.length)) {
		path = path.substr(0, path.length - key.length);
	}
	if (path.lastIndexOf(activeKey) == (path.length - activeKey.length)) {
		path = path.substr(0, path.length - activeKey.length);
	}
	return path;
}

// 別ウィンドウのスクリプト
jQuery(function() {	
	//新規別ウィンドウ開く
	jQuery('a[@href^="http://"], a[@href^="https://"] ,a[@href*="izura/"] ,a[@href*="ps-s/"] ,a[@href*="all/gaiyou/"] ,a[@href*="siryo/"] ,a[@href*="2009sy/"]').not('a[@href^="http://www.ibaraki.ac.jp/"] ,a[@href$=".pdf"]').addClass('otherWin').click(function(){
		window.open(this.href, '_blank');
		return false;
	});
	//新規別ウィンドウ開く
	jQuery('a[@href^="http://www.ibaraki.ac.jp/all/gaiyou/"] ,a[@href^="http://www.ibaraki.ac.jp/ps-s/"]').not('a[@href$=".pdf"]').addClass('otherWin').click(function(){
		window.open(this.href, '_blank');
		return false;
	});
	//pdf,excel,word、別ウィンドウで開く
	jQuery('a[@href$=".pdf"]').addClass('pdfLink').click(function(){
		window.open(this.href, '_blank');
		return false;
	});
});

// 交互にテーブルにクラスを設定するのスクリプト
// evenCellとoddCellの装飾はCSSで設定してください
jQuery(function() {
  jQuery('.table03').find('tr:odd').addClass('evenCell');
  jQuery('.table03').find('tr:even').addClass('oddCell');
});

// 繰り返しの最後にクラスを付与（背景画像を変える。マージンを削除など）
// lastの装飾は各々CSSで設定してください
jQuery(function() {
  jQuery('.lists .listSet:last').addClass('last');
});

jQuery(function() {
  jQuery('.secondLayer #mainAndSub #sub #subNavi #subNaviList li ul.subnNaviLast').find('li:last').addClass('last');
});

jQuery(function() {
  jQuery('.secondLayer #mainAndSub #sub #subNavi #subNaviList').find('li:last-child').addClass('last');
});

jQuery(function() {
  jQuery('.departmentLinks').find('li:last').addClass('last');
});

// 空Pタグ削除
jQuery(function(){
	jQuery(".pageBody p").each(function(){
		var abc = jQuery(this).html();
		if( abc == "&nbsp;"){
			jQuery(this).remove();
		}
	});
});

// ヒストリーバックのスクリプト
jQuery(function() {
  jQuery('.historyBack a').click(function(){
		history.back();
		return false;
	});
});

// 印刷のスクリプト
jQuery(function() {
  jQuery('.print a').click(function(){
		window.print(); 
		return false;
	});
});

// スムーススクロールのスピード
jQuery(function() {
	jQuery('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var target = jQuery(this.hash);
			target = target.length && target;
			if (target.length) {
				var sclpos = 15;
				var scldurat = 1200;
				var targetOffset = target.offset().top - sclpos;
				jQuery('html,body')
					.animate({scrollTop: targetOffset}, {duration: scldurat, easing: "easeOutExpo"});
				return false;
			}
		}
	});
});


// FAQ
jQuery(function(){
	var faqArray = jQuery( '.qaSetBox' );
	if( faqArray != null )
	{
		for( var i = 0; i < faqArray.length; i++ )
		{
			var item = faqArray[ i ];
			var linkItem = jQuery(item).find( "h2 a" );
			var openItem = jQuery(item).find( ".open" );
			setFAQOpenAction( linkItem, openItem );
		}
	}
});

function setFAQOpenAction( linkItem, openItem )
{
	jQuery(linkItem).click( function ()
	{
		if( jQuery( openItem ).is(":hidden") )
		{
			jQuery(linkItem).addClass( "active" );
			jQuery( openItem ).slideDown();
		}
		else
		{
			jQuery(linkItem).removeClass( "active" );
			jQuery( openItem ).slideUp();
		}
		return false;
	});
}

// TABのスクリプト
jQuery(function(){
	jQuery('.tabNav').each(function(){
		var tabs = jQuery(this).find('a[href^=#]');
		var imgs = tabs.find('img');
		var contents;
		imgs.each(function(){
			this.originalSrc =  jQuery(this).attr('src');
			this.currentSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)$/, '_cr'+"$1");
		});
		tabs.each(function(){
			var selecter = jQuery(this).attr('href');
			if (contents) {
				contents = contents.add(selecter);
			} else {
				contents = jQuery(selecter);
			}
			jQuery(this).click(function(){
				imgs.each(function(){
					jQuery(this).attr('src',this.originalSrc);
				});
				var img = jQuery(this).find('img');
				img.attr('src',img.get(0).currentSrc);
				contents.hide();
				jQuery(selecter).show();
				return false;
			});
		});
		tabs.eq(0).trigger('click');
	});
	jQuery('.tabContent h2').hide();
});

// ThickBoxのスクリプト
jQuery(function(){
	jQuery('a[href$=".jpg"]').add('a[href$=".gif"]').add('a[href$=".png"]').click(function(event){
		event.preventDefault();
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		tb_show(t,a,g);
		this.blur();
		return false;
	});

	initNavi();
});

/**
 * ナビゲーションの状態を初期化する。
 * 現在地とその親階層の要素をハイライトし、別の親を持つ下位要素を隠す。
 * 現在地の判定はパンくずリスト（topicPath）を基準とします。
 */
function initNavi() {
	var titles = new Array();
	// topicPathを配列として抽出
	extractTopicPath(jQuery("#topicPath ul li:last"), titles);

	// ナビゲーション中にあるパンくずリストと同一テキストを持つ要素をハイライト
	for (var i = 0; i < titles.length; i++) {
		var selector = "#subNaviList a:contains(" + titles[i] + ")";
		jQuery("#subNaviList a").each(function() {
			var text = trim(jQuery(this).text());
			if (text == titles[i]) {
				jQuery(this).parent("li").addClass("current");
				if (jQuery(this).parent().parent().hasClass("subnNaviLast")) {
					jQuery(this).parent().parent().parent("li").addClass("currentMarker");
				} else {
					jQuery(this).parent("li").addClass("currentMarker");
				}
			}
		});
	}

	// 別ルートの下位要素を隠す
	jQuery("#subNaviList li").each(function() {
		if (!jQuery(this).hasClass("currentMarker")) {
			jQuery(this).children("ul").hide();
		}
	});
}

/**
 * 指定された要素の兄要素を配列に格納していく。
 */
function extractTopicPath(target, titles) {
	if (target != null && target.prev().length > 0) {
		titles.push(trim(target.text()));
		extractTopicPath(target.prev(), titles);
	}
}

function trim(txt) {
	return txt.replace(/^\s+|\s+$/g, "");
}


