/* -------------------------------------------

	共通関数実行
	
------------------------------------------- */
$(function() {

/* FloatImage */
	$(".imgLeft, .imgRight").not(".no").FloatImage();
	$(".imageBoxLeft, .imageBoxRight").not(".no").FloatImage();

/* Rollover */
	new RolloverImages( 'rollover', 'on' );

/* SmoothScroll */
	$().SmoothScroll();

/* flatheights */
	$('.columnBlock03 > div').flatHeights();
	$('.linkBlock > div').flatHeights();
	$('#leftSide, #contents, #rightSide').flatHeights();
	$('div#contents .examineIndexBox01 .linkBlock02 .col .linkBox').flatHeights();

/* flatheights for columnBlock03 */
	/* div要素を2つずつの組に分ける */
   // var sets = [], temp = [];
//    $('.columnBlock03 > div').each(function(i) {
//        temp.push(this);
//        if (i % 2 == 1) {
//            sets.push(temp);
//            temp = [];
//        }
//    });
//    if (temp.length) sets.push(temp);
//
//    /* 各組ごとに高さ揃え */
//    $.each(sets, function() {
//        $(this).flatHeights();
//    });

});

/* -------------------------------------------

	共通関数定義
	
------------------------------------------- */

/* ---------------------
  FloatImage Setup
--------------------- */
(function ($) {
	$.fn.FloatImage = function () {
		$(this).each(function() {
				var w = $("img", this).width();
				$(this).css("width",w);
				
				/* 回り込みの際に、block要素がある場合 */
				var width_all = $(this).parent().width();
				var target = $(this).siblings(".widthFix");
				target.css("width",width_all - w -15);
		});
	}
})(jQuery);


/* ---------------------
  SmoothScroll Setup
--------------------- */
(function ($) {
	$.fn.SmoothScroll = function (options) {

		var scroller = (function () {
			var c = $.extend({
				easing: 100,
				step: 30,
				fps: 60,
				fragment: ''
			},
			options);

			c.ms = Math.floor(1000 / c.fps);
			var timerId;
			var param = {
				stepCount: 0,
				startY: 0,
				endY: 0,
				lastY: 0
			};

			function move() {
				if (param.stepCount == c.step) {
					// scroll end
					setFragment(param.fragment);

					window.scrollTo(getCurrentX(), param.endY);
				} else if (param.lastY == getCurrentY()) {
					// scroll
					param.stepCount++;
					window.scrollTo(getCurrentX(), getEasingY());
					param.lastY = getEasingY();
					timerId = setTimeout(move, c.ms);
				} else {
					// error	
					if (getCurrentY() + getViewportHeight() == getDocumentHeight()) {
						setFragment(param.fragment);
					}
				}
			}

			function setFragment(fragment) {
				location.href = location.href.split('#')[0] + fragment;
			}

			function getEasingY() {
				return Math.floor(getEasing(param.startY, param.endY, param.stepCount, c.step, c.easing));
			}

			function getEasing(start, end, stepCount, step, easing) {
				var s = stepCount / step;
				return (end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start;
			}

			function targetOffsetTop(target) {
				return target.offset().top;
			}
			return {
				set: function (options) {
					this.stop();
					if (options.startY == undefined) options.startY = getCurrentY();
					param = $.extend(param, options);
					param.lastY = param.startY;
					param.fragment = options.fragment;
					timerId = setTimeout(move, c.ms);
				},
				stop: function () {
					clearTimeout(timerId);
					param.stepCount = 0;
				}
			};
		})();

		function getCurrentY() {
			return document.body.scrollTop || document.documentElement.scrollTop;
		}

		function getCurrentX() {
			return document.body.scrollLeft || document.documentElement.scrollLeft;
		}

		function getDocumentHeight() {
			return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement.scrollHeight : document.body.scrollHeight;
		}

		function getViewportHeight() {
			return (!$.browser.safari && !$.browser.opera) ? document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight : window.innerHeight;
		}
		// init
		$('a[href^=#], area[href^=#]').not('a[href=#], area[href=#], a.no-scroll, area.no-scroll').click(function () {

			var fragment = $(this).attr('href');
			var target = $(fragment);
			if (target.length == 0) target = $('a[name=' + fragment + ']');

			if (target.length) {
				scroller.set({
					endY: ((getDocumentHeight() - target.offset().top)) < getViewportHeight() ? getDocumentHeight() - getViewportHeight() : target.offset().top,
					fragment: fragment,
					_target: target
				});

				return false;
			}
		});
	}

})(jQuery)


/* ---------------------
  Rollover Setup
--------------------- */

function RolloverImages( className, onSuffix, aSuffix ){
	this.init( className, onSuffix, aSuffix );
}

RolloverImages.prototype = {

	init : function( className, onSuffix, aSuffix ){
		if(!document.getElementById || !document.images || !className) return;

		// 
		this.targetClassName = className;
		this.onSuffix = onSuffix;
		this.aSuffix = aSuffix;
		//this.mdSuffix = mdSuffix;
		this.buttons = [];

		// 
		var _this  = this;
		var imgs   = [];
		var inputs = [];
		var rolloverObj    = [];

		var selectors = [
			"img." + this.targetClassName,
			"input." + this.targetClassName
		];

		$( selectors.join(", ") ).each(function(i){
			var el = $(this);
			rolloverObj.push(this);
		});

		$.each(rolloverObj, function(i){ _this.registButton(this); });
		this.preloadImages();
	},


	registButton : function( el ){
		var _this = this;
		var btn = new Object();
		btn.src = el.src;
		btn.filetype = btn.src.substring(btn.src.lastIndexOf('.'));
		btn.basename = btn.src.substring(0, btn.src.length-btn.filetype.length);
		btn.onsrc = btn.basename + this.onSuffix + btn.filetype;

		el.offsrc = btn.src;
		el.onsrc  = btn.onsrc;
		el.lock   = false;
		if( this.aSuffix ){
			btn.activesrc = btn.basename + this.aSuffix + btn.filetype;
			el.activesrc = btn.activesrc;
		}

		this.buttons.push( el );

		if( !this.aSuffix ){
			$(el).bind( 'mouseover', function(){ _this.swapImage( el, 'on' ); } );
			$(el).bind( 'mouseout',  function(){ _this.swapImage( el, 'off'); } );
			if(el.parentNode && el.parentNode.tagName=="A"){
				var p = el.parentNode;
				$(p).bind( 'focus', function(){ _this.swapImage( el, 'on' ); } );
				$(p).bind( 'blur',  function(){ _this.swapImage( el, 'off'); } );
			}
		}
		else {
			$(el).bind( 'mouseover', function(){ if(!el.lock) _this.swapImage( el, 'on' ); } );
			$(el).bind( 'mouseout',  function(){ if(!el.lock) _this.swapImage( el, 'off'); } );
			$(el).bind( 'mouseup',   function(){
				$(_this.buttons).each(function(i){
					_this.deactivate(this)
				});
				_this.activate( el );
			} );

			if(el.parentNode && el.parentNode.tagName=="A"){
				var p = el.parentNode;
				$(p).bind( 'focus', function(){ if(!el.lock) _this.swapImage( el, 'on' ); } );
				$(p).bind( 'blur',  function(){ if(!el.lock) _this.swapImage( el, 'off'); } );

				$(p).bind( 'keypress',  function(e){
					if( jQuery.browser.msie || (!jQuery.browser.msie && e.keyCode == 13) ){
						//Event.stop(e);
						$(_this.buttons).each(function(i){
							_this.deactivate(this)
						});
						_this.activate( el );
					}
				} );
			}

		}
	},

	preloadImages : function(){
		var ret = [];
		for( var i=0; i<this.buttons.length; i++ ){
			(new Image()).src = this.buttons[i].onsrc;
			ret[ret.length] = this.buttons[i].onsrc;
			if(this.buttons[i].activesrc){
				(new Image()).src = this.buttons[i].activesrc;
				ret[ret.length] = this.buttons[i].activesrc;
			}
		}
	},

	swapImage : function( obj, status ){
		if( !obj || !obj[status+"src"] ) return;
		if( !obj.lock )
			obj.src = obj[status+"src"];
	},

	activate : function( el ){
		this.swapImage( el, 'active' );
		this.lock( el );
	},

	deactivate : function( el ){
		this.unlock( el );
		this.swapImage( el, 'off' );
	},

	lock : function( obj ){
		if( obj.lock == 'undefined' ) return;
		if(!obj.lock) obj.lock = true;
	},

	unlock : function( obj ){
		if( obj.lock == 'undefined' ) return;
		if( obj.lock ) obj.lock = false;
	}
}


/*
======================================================================
	$.changeLetterSize.addHandler(func)
	文字の大きさが変化した時に実行する処理を追加
======================================================================
*/

jQuery.changeLetterSize = {
	handlers : [],
	interval : 200,
	currentSize: 0
};

(function($) {

	var self = $.changeLetterSize;

	/* 文字の大きさを確認するためのins要素 */
	var ins = $('<div>fontSizeWatcher</div>').css({
		display: 'block',
		visibility: 'hidden',
		position: 'absolute',
		padding: '0',
		top: '0'
	});

	/* 文字の大きさが変わったか */
	var isChanged = function() {
		ins.appendTo('body');
		var size = ins[0].offsetHeight;
		ins.remove();
		if (self.currentSize == size) return false;
		self.currentSize = size;
		return true;
	};

	/* 文書を読み込んだ時点で
	   文字の大きさを確認しておく */
	$(isChanged);

	/* 文字の大きさが変わっていたら、
	   handlers中の関数を順に実行 */
	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);

/*
======================================================================
	$(expr).flatHeights()
	$(expr)で選択した複数の要素について、それぞれ高さを
	一番高いものに揃える
======================================================================
*/

(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');
	};

	/* 要素群の高さを揃え、setsに追加 */
	jQuery.fn.flatHeights = function() {
		if (this.length > 1) {
			flatHeights(this);
			sets.push(this);
		}
		return this;
	};

	/* 高さ揃えを再実行する処理 */
	var reflatting = function() {
		$.each(sets, function() {
			this.height('auto');
			flatHeights(this);
		});
	};

	/* 文字の大きさが変わった時に高さ揃えを再実行 */
	$.changeLetterSize.addHandler(reflatting);

	/* ウィンドウの大きさが変わった時に高さ揃えを再実行 */
	$(window).resize(reflatting);

})(jQuery);

/*
======================================================================
	stripe
======================================================================
*/
$(function() {
        $(".stripe tr:even").addClass("even");
});

