//onload start
$(function() {
	//タブ
	$.yuga.tab();
	//奇数・偶数
	$.yuga.stripe();

	//pdf・word・excelへのアイコン付与 画像のパスは適時変更
	$("a[href$='.pdf']").not('#subBanner a').after('<img src="http://www.jichi.ac.jp/hospital/top/resident/common/img/icon_pdf.gif" alt="PDFファイルへのリンクです" class="externalbeforeIcon" />');
	$("a[href$='.doc'],a[href$='.docx']").after('<img src="http://www.jichi.ac.jp/hospital/top/resident/common/img/icon_doc.gif" alt="Wordファイルへのリンクです" class="externalbeforeIcon" />');
	$("a[href$='.xls'],a[href$='.xlsx']").after('<img src="http://www.jichi.ac.jp/hospital/top/resident/common/img/icon_xls.gif" alt="Excelファイルへのリンクです" class="externalbeforeIcon" />');
	$("a[href$='.pdf'],a[href$='.doc'],a[href$='.docx'],a[href$='.xls'],a[href$='.xlsx']").click(function(){window.open(this.href, '_blank');return false;});



	//ロールオーバー

$('.imgover').opOver();

	//hover時にclass付与
	$(".navi-body dt span,.accordion > dt,.blocklink").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});

	//blocklink エリアの指定
	clickArea("dl", "blocklink");
	clickArea("dt", "blocklink");
	clickArea("tr", "blocklink");
	clickArea("td", "blocklink");
	clickArea("li", "blocklink");
	clickArea("div", "blocklink");

	// 画像キャプション生成
	$("img.capContain").each(function(){
		var icT = $(this).attr("title").split("｜").join("<br />");
		var icW = $(this).width();
		$(this).removeAttr("title").after('<span class="imgCap">'+ icT +'</span>');
		$(this).next("span.imgCap").width(icW).css("display","block");
	});

});//onload end



(function($) {
/*
 * yuga.js 0.7.1
 *
 * Copyright (c) 2009 Kyosuke Nakamura (kyosuke.jp)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
$.yuga = {
Uri: function(path){
	var self = this;
	this.originalPath = path;
	this.absolutePath = (function(){
		var e = document.createElement('span');
		e.innerHTML = '<a href="' + path + '" />';
		return e.firstChild.href;
	})();
	var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
	var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
	for (var field in fields) {
		this[field] = r[fields[field]];
	}
	this.querys = {};
	if(this.query){
		$.each(self.query.split('&'), function(){
			var a = this.split('=');
			if (a.length == 2) self.querys[a[0]] = a[1];
		});
	}
},
tab: function(options) {
	var c = $.extend({
		tabNavSelector:'.tabNav',
		activeTabClass:'active'
	}, options);
	$(c.tabNavSelector).each(function(){
		var tabNavList = $(this).find('a[href^=#], area[href^=#]');
		var tabBodyList;
		tabNavList.each(function(){
			this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
			var selecter = '#'+this.hrefdata.fragment;
			if (tabBodyList) {
				tabBodyList = tabBodyList.add(selecter);
			} else {
				tabBodyList = $(selecter);
			}
			$(this).unbind('click');

			$(this).click(function(){
				if(tabNavList.children("img").length){
					$(this).children("img").addClass("on");
					if(tabNavList.children("img").attr("src").indexOf("_on") != -1){
						$(this).children("img").each(function() {
							var tabimgdot4 = this.src.lastIndexOf('_on.');
							var tabimgonlydot4 = this.src.lastIndexOf('.');
							var tabimgsrc4 = this.src.substr(0, tabimgdot4) + this.src.substr(tabimgonlydot4, 4);
							this.src = tabimgsrc4;
						});
					}else{
						$(this).children("img").each(function() {
							var tabimgdot = this.src.lastIndexOf('.');
							var tabimgsrc = this.src.substr(0, tabimgdot) + '_on' + this.src.substr(tabimgdot, 4);
							this.src = tabimgsrc;
							alert(tabimgsrc);
						});
						tabNavList.not($(this)).children("img").not("img.on").click(function(){
							var tabimgdot2 = this.src.lastIndexOf('.');
							var tabimgsrc2 = this.src.substr(0, tabimgdot2) + '_on' + this.src.substr(tabimgdot2, 4);
							this.src = tabimgsrc2;
							alert(tabimgsrc2);
							tabNavList.not($(this)).children("img.on").each(function() {
								if($(this).attr("src").indexOf("_on") != -1){
									var tabimgdot3 = this.src.lastIndexOf('_on.');
									var tabimgonlydot3 = this.src.lastIndexOf('.');
									var tabimgsrc3 = this.src.substr(0, tabimgdot3) + this.src.substr(tabimgonlydot3, 4);
									this.src = tabimgsrc3;
								}else{

								}
							});
						});

					}

					tabNavList.not($(this)).children("img.on").removeClass("on");

				}else{
					tabNavList.removeClass(c.activeTabClass);
					$(this).addClass(c.activeTabClass);
				}
				tabBodyList.css({"width":"auto","height":"auto","position":"absolute","top":"-9999px","left":"-9999px"});
				$(selecter).fadeOut(0).css({"width":"auto","height":"auto","position":"static","top":"auto","left":"auto"}).fadeIn(700);
				return false;
			});
		});
		tabBodyList.css({"width":"auto","height":"auto","position":"absolute","top":"-9999px","left":"-9999px"});
		if($(c.tabNavSelector).length){
			var thisWinURL = location.hash;
			var containTabs = $(c.tabNavSelector).html();
			if(thisWinURL.length == 0){
				tabNavList.filter(':first').trigger('click');
			}else if(containTabs.indexOf(thisWinURL) != -1){
				tabNavList.filter("a[href='"+thisWinURL+"']").trigger('click').focus();
			}else{
				tabNavList.filter(':first').trigger('click');
			}
		}
	});
},
stripe: function(options) {
	var c = $.extend({
		oddClass:'odd',
		evenClass:'even'
	}, options);
	$('#contents ul,#contents ol').each(function(){
		$(this).children('li:odd').addClass(c.evenClass);
		$(this).children('li:even').addClass(c.oddClass);
	});
	$('table, tbody').each(function(){
		$(this).children('tr:odd').addClass(c.evenClass);
		$(this).children('tr:even').addClass(c.oddClass);
	});
}

};
})(jQuery);

// Rollover ----------------------------------------

(function($) {

$.fn.opOver = function(op,oa,durationp,durationa){

var c = {
op:op? op:1.0,
oa:oa? oa:0.6,
durationp:durationp? durationp:'fast',
durationa:durationa? durationa:'fast'
};


$(this).each(function(){
$(this).css({
opacity: c.op,
filter: "alpha(opacity="+c.op*100+")"
}).hover(function(){
$(this).fadeTo(c.durationp,c.oa);
},function(){
$(this).fadeTo(c.durationa,c.op);
})
});
},

$.fn.wink = function(durationp,op,oa){

var c = {
durationp:durationp? durationp:'slow',
op:op? op:1.0,
oa:oa? oa:0.2
};

$(this).each(function(){
$(this).css({
opacity: c.op,
filter: "alpha(opacity="+c.op*100+")"
}).hover(function(){
$(this).css({
opacity: c.oa,
filter: "alpha(opacity="+c.oa*100+")"
});
$(this).fadeTo(c.durationp,c.op);
},function(){
$(this).css({
opacity: c.op,
filter: "alpha(opacity="+c.op*100+")"
});
})
});
}

})(jQuery);

// MenuActive ----------------------------------------
function NaviActive(id) {
	var target_navi = "#"+id;
	$(target_navi).children('a').addClass("on");
}



// blocklink ----------------------------------------

function clickArea(tagName, className) {
	$("."+className+" a").css("outline","0");
	var els = document.getElementsByTagName(tagName);
	if (els) {
		for (var i = 0; i < els.length; i++){
			if (els[i].className.indexOf(className)>=0) {
				els[i].onmouseover=function() {
					this.className+=" blockhover";
					window.status = this.getElementsByTagName("a")[0].href;
					return true;
				}
				els[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" blockhover\\b"), "");
					window.status = "";
					return true;
				}
				els[i].onclick = function () {
					var elm = this.getElementsByTagName("a")[0];
					if($(elm).attr('href').indexOf("http://example.com/example/") != -1){
						location.href = elm.href;
					}else{
						if($(elm).attr('href').indexOf("http") != -1){
							window.open(elm.href,'_blank');
						}else{
							location.href = elm.href;
						}
					}
				}
			}
		}
	}
}


$.changeLetterSize = {
	handlers : [],
	interval : 1000,
	currentSize: 0
};
(function($) {
	var self = $.changeLetterSize;
	var ins = $('<ins>M</ins>').css({display: 'block',visibility: 'hidden',position: 'absolute',padding: '0',top: '0'});
	var isChanged = function() {
		ins.appendTo('#contents');
		var size = ins[0].offsetHeight;
		ins.remove();
		if (self.currentSize == size) return false;
			self.currentSize = size;
			return true;
		};
		$(isChanged);
		var observer = function() {
			if (!isChanged()) return;
			$.each(self.handlers, function(i, handler) {
				handler();
			});
		};
	self.addHandler = function(func) {
		self.handlers.push(func);
			if (self.handlers.length == 1) {
				setInterval(observer, self.interval);
			}
	};
})(jQuery);

(function($) {
	var sets = [];
	var flatHeights = function(set) {
		var maxHeight = 0;
		set.each(function(){
		var height = this.offsetHeight;
		if (height > maxHeight) maxHeight = height;
		});
	set.css('height', maxHeight + 'px');
	};
	$.fn.flatHeights = function() {
		if (this.length > 1) {
			flatHeights(this);
			sets.push(this);
		}
	return this;
	};
	$.changeLetterSize.addHandler(function() {
		$.each(sets, function() {
			this.height('auto');
			flatHeights(this);
		});
	});
})(jQuery);

function flatColumns(flatColum,columNum){
	$(flatColum).parent().each(function(){
		var sets = [], temp = [];
		if(columNum == 2){
			$(this).children().each(function(i){
				temp.push(this);
				if ((i+1)%2 == 0 && i!=0){
					sets.push(temp);
					temp = [];
				}
			});
			if (temp.length) sets.push(temp);
		}else if(columNum == 3){
			$(this).children().each(function(i) {
				temp.push(this);
				if (i % 3 == 2) {
					sets.push(temp);
					temp = [];
				}
			});
			if (temp.length) sets.push(temp);
		}
		$.each(sets, function() {
			$(this).flatHeights();
		});
	});
}

