jQuery(function($) {
	// FIXHEIGHT
	$(".fixHeight").fixHeight();


	// HOVER
	$(".hover,#hnav a img,.bt_newslist img,.img_map img").hover(
		function(){   // マウスオーバー
		$(this).fadeTo(1, 0.6);
		},
		function(){   // マウスアウト
		$(this).fadeTo(1, 1);
		}
	);



	// ROLLOVER
	var postfix = '_o';
	$('.rollover a img,#gnav a img,.bt_formedical a img,.catmenu a img,#sidebar a img,.bt_access a img,.bt_detail a img,.bt_inq img,.box_corse .taright img,.box_corse_end .taright img').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);
			}
		);
	});




	// CHANGE FONT-SIZE  put title attr. on <li>   Require jquery.cookie.js
	$("body").css("font-size",$.cookie('fsize'));
	
	if('92.5%'==$.cookie('fsize')){
			$('#bt_l').addClass('on');
			$('#bt_m').removeClass('on');
	}
	
	$("#font_changer li").hover(
	  function(){
		$(this).css('cursor','pointer');
	  }
	);
	
	$("#font_changer li").click(function(){
		var s=$(this).attr('title');
		switch (s) {
			case "標準":size='80%';
			$('#bt_m').addClass('on');
			$('#bt_l').removeClass('on');
			break;
			case "拡大":size='92.5%';
			$('#bt_l').addClass('on');
			$('#bt_m').removeClass('on');
			break;
			default:size='80%';
		};
		$("body").css("font-size",size);
		$.cookie("fsize",size,{expires:30,path:'/'});
	});


	// TAB
	$("#tab li").click(function() {
		if(! ($(this).hasClass("select")) ){
			var num = $("#tab li").index(this);
			$(".tab_contents").hide();
			/*fadeIn使用時VISTA&IE7で文字にジャギーが出るバグの対処*/
			var ua = navigator.userAgent;
			if (ua.match(/Win(dows )?NT 6\.0/) && navigator.appVersion.indexOf("MSIE 7") != -1) {
				// Windows VistaでIE7の時
				$(".tab_contents").eq(num).fadeIn('fast',function(){ this.style.removeAttribute("filter"); });
			}else{
				$(".tab_contents").eq(num).fadeIn('fast');
			};
			/*ここまで*/
			$("#tab li").removeClass('select');
			$(this).addClass('select')
		}
		/*location.href="#tab";*/
	});
	$("#tab li").hover(function(){
		if(! ($(this).hasClass("select")) ){
			$(this).css("cursor","pointer");
		};
	}, function(){
		$(this).css("cursor","default");
	});

	
});



// FIXHEIGHT

/*
 * fixHeight - jQuery Plugin
 * http://www.starryworks.co.jp/blog/tips/javascript/fixheightjs.html
 *
 * Author Koji Kimura @ STARRYWORKS inc.
 * http://www.starryworks.co.jp/
 * 
 * Licensed under the MIT License
 *
 */


(function($){
	
	var isInitialized = false;
	var parents = [];
	var textHeight = 0;
	var $fontSizeDiv;
	
	$.fn.fixHeight = function() {
		this.each(function(){
			var childrenGroups = getChildren( this );
			
			$.each( childrenGroups, function(){
				
				var $children = $(this);
				if ( !$children.filter(":visible").length ) return;
				
				var row = [];
				var top = 0;
				$children.each(function(){
					if ( top != $(this).position().top ) {
						$(row).sameHeight();
						row = [];
						top = $(this).position().top;
					}
					row.push(this);
				});
				if ( row.length ) $(row).sameHeight();
			});
			
			
		});
		init();
		return this;
	}
	
	$.checkFixHeight = function( i_force ) {
		if ( $fontSizeDiv.height() == textHeight && i_force !== true ) return;
		textHeight = $fontSizeDiv.height();
		$(parents).fixHeight();
	}
	
	$.fn.sameHeight = function() {
		var maxHeight = 0;
		this.css("height","auto");
		this.each(function(){
			if ( $(this).height() > maxHeight ) maxHeight = $(this).height();
		});
		return this.height(maxHeight);
	}
	
	function getChildren( i_parent ) {
		var $parent = $( i_parent );
		
		if ( $parent.data("fixHeightChildrenGroups") ) return $parent.data("fixHeightChildrenGroups");
		var childrenGroups = [];
		
		var $children = $parent.find(".fixHeightChild");
		if ( $children.length ) childrenGroups.push( $children );
		
		var $groupedChildren = $parent.find("*[class*='fixHeightChild']:not(.fixHeightChild)");
		if ( $groupedChildren.length ) {
			var classNames = {};
			$groupedChildren.each(function(){
				var a = $(this).attr("class").split(" ");
				var i;
				var l = a.length;
				var c;
				for ( i=0; i<l; i++ ) {
					c = a[i].match(/fixHeightChild[a-z0-9_-]+/i);
					if ( !c ) continue;
					c = c.toString();
					if ( c ) classNames[c] = c;
				}
			});
			for ( var c in classNames ) childrenGroups.push( $parent.find("."+c) );
		}
		
		if ( !childrenGroups.length ) {
			$children = $parent.children();
			if ( $children.length ) childrenGroups.push( $children );
		}
		
		$parent.data("fixHeightChildrenGroups", childrenGroups );
		parents.push( $parent );
		
		return childrenGroups;
	}
	
	
	function init() {
		if ( isInitialized ) return;
		isInitialized = true;
		$fontSizeDiv = $(document).append('<div style="position:absolute;left:-9999px;top:-9999px;">s</div>');
		setInterval($.checkFixHeight,1000);
		$(window).resize($.checkFixHeight);
		$.checkFixHeight();
		$(window).load( function(){ $.checkFixHeight(true); } );
	}
	
})(jQuery);


(function(){
   var easing = 0.25;
   var interval = 20;
   var d = document;
   var targetX = 0;
   var targetY = 0;
   var targetHash = '';
   var scrolling = false;
   var splitHref = location.href.split('#');
   var currentHref_WOHash = splitHref[0];
   var incomingHash = splitHref[1];
   var prevX = null;
   var prevY = null;

   // ドキュメント読み込み完了時にinit()を実行する
   addEvent(window, 'load', init);

   // ドキュメント読み込み完了時の処理
   function init(){
     // ページ内リンクにイベントを設定する
     setOnClickHandler();
     // 外部からページ内リンク付きで呼び出された場合
     if(incomingHash){
       if(window.attachEvent && !window.opera){
         // IEの場合はちょっと待ってからスクロール
         setTimeout(function(){scrollTo(0,0);setScroll('#'+incomingHash);},50);
       }else{
         // IE以外はそのままGO
         scrollTo(0, 0);
         setScroll('#'+incomingHash);
       }
     }
   }

   // イベントを追加する関数
   function addEvent(eventTarget, eventName, func){
     if(eventTarget.addEventListener){
       // モダンブラウザ
       eventTarget.addEventListener(eventName, func, false);
     }else if(window.attachEvent){
       // IE
       eventTarget.attachEvent('on'+eventName, function(){func.apply(eventTarget);});
     }
   }
   
   function setOnClickHandler(){
     var links = d.links;
     for(var i=0; i<links.length; i++){
       // ページ内リンクならスクロールさせる
       var link = links[i];
       var splitLinkHref = link.href.split('#');
       if(currentHref_WOHash == splitLinkHref[0] && d.getElementById(splitLinkHref[1])){
         addEvent(link, 'click', startScroll);
       }
     }
   }

   function startScroll(event){
     // リンクのデフォルト動作を殺す
     if(event){ // モダンブラウザ
       event.preventDefault();
       //alert('modern');
     }else if(window.event){ // IE
       window.event.returnValue = false;
       //alert('ie');
     }
     // thisは呼び出し元になってる
     setScroll(this.hash);
   }

   function setScroll(hash){
     // ハッシュからターゲット要素の座標をゲットする
     var targetEle = d.getElementById(hash.substr(1));
     if(!targetEle)return;
     //alert(scrollSize.height);
     // スクロール先座標をセットする
     var ele = targetEle;
     var x = 0;
     var y = 0;
     while(ele){
       x += ele.offsetLeft;
       y += ele.offsetTop;
       ele = ele.offsetParent;
     }
     var maxScroll = getScrollMaxXY();
     targetX = Math.min(x, maxScroll.x);
     targetY = Math.min(y, maxScroll.y);
     targetHash = hash;
     // スクロール停止中ならスクロール開始
     if(!scrolling){
       scrolling = true;
       scroll();
     }
   }

   function scroll(){
     var currentX = d.documentElement.scrollLeft||d.body.scrollLeft;
     var currentY = d.documentElement.scrollTop||d.body.scrollTop;
     var vx = (targetX - currentX) * easing;
     var vy = (targetY - currentY) * easing;
     var nextX = currentX + vx;
     var nextY = currentY + vy;
     if((Math.abs(vx) < 1 && Math.abs(vy) < 1)
       || (prevX === currentX && prevY === currentY)){
       // 目標座標付近に到達していたら終了
       scrollTo(targetX, targetY);
       scrolling = false;
       location.hash = targetHash;
       prevX = prevY = null;
       return;
     }else{
       // 繰り返し
       scrollTo(parseInt(nextX), parseInt(nextY));
       prevX = currentX;
       prevY = currentY;
       setTimeout(function(){scroll()},interval);
     }
   }
   
   function getDocumentSize(){
     return {width:Math.max(document.body.scrollWidth, document.documentElement.scrollWidth), height:Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)};
   }

   function getWindowSize(){
     var result = {};
     if(window.innerWidth){
       var box = d.createElement('div');
       with(box.style){
         position = 'absolute';
         top = '0px';
         left = '0px';
         width = '100%';
         height = '100%';
         margin = '0px';
         padding = '0px';
         border = 'none';
         visibility = 'hidden';
       }
       d.body.appendChild(box);
       var width = box.offsetWidth;
       var height = box.offsetHeight;
       d.body.removeChild(box);
       result = {width:width, height:height};
     }else{
       result = {width:d.documentElement.clientWidth || d.body.clientWidth, height:d.documentElement.clientHeight || d.body.clientHeight};
     }
     return result;
   }
   
   function getScrollMaxXY() {
     if(window.scrollMaxX && window.scrollMaxY){
       return {x:window.scrollMaxX, y:window.scrollMaxY};
     }
     var documentSize = getDocumentSize();
     var windowSize = getWindowSize();
     return {x:documentSize.width - windowSize.width, y:documentSize.height - windowSize.height};
   }
   
 }());


