/* =======================================

	Common Javascript

 -----------------------------------------
	INDEX
 -----------------------------------------
	1.

========================================== */

/* Flatheights
========================================== */
/*
	jquery.flatheights.js
	Version: 2007-08-01
*/

/*
======================================================================
	$.changeLetterSize.addHandler(func)
	文字の大きさが変化した時に実行する処理を追加
======================================================================
*/

jQuery.changeLetterSize = {
	handlers : [],
	interval : 500,//定期処理を行う時間間隔。一応0.5秒で設定して有ります。1秒 = 1000
	currentSize: 0
};

(function($) {

	var self = $.changeLetterSize;

	/* 文字の大きさを確認するためのins要素 */
	var ins = $('<ins>M</ins>').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;
	};

	/* 文字の大きさが変わった時に、
	   setsに含まれる各要素群に対して高さ揃えを実行 */
	$.changeLetterSize.addHandler(function() {
		$.each(sets, function() {
			this.height('auto');
			flatHeights(this);
		});
	});

})(jQuery);


/*
Copyright (c) 2007, KITAMURA Akatsuki

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/


/* Flatheights Setting
========================================== */
$(function(){
	
	/* Index List
	---------------------------------------- */
		/* Index List01
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList01 ul.indexList li').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();
		});
		
		/* Index List02
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList02 ul.indexList li').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();
		});
		
		/* Index List03
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList03 ul.indexList li').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();
		});
		/* Index List04
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList04 ul.indexList li').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();
		});
		
		/* Index List05
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList05 ul.indexList li').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();
		});
		
		/* Index List06
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList06 ul.indexList li').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();
		});
		/* Index List07
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList07 ul.indexList li').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();
		});
		
		/* Index List08
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList08 ul.indexList li').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();
		});
		
		/* Index List09
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents div.indexList09 ul.indexList li').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();
		});
		
	/* Book Info
	---------------------------------------- */
	var sets = [], temp = [];
	$('#contents .monthlyLatest ul.bookInfo > li').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();
	});
	
	/* Books Introduce
	---------------------------------------- */
	var sets = [], temp = [];
	$('#contents .booksIntroduce ul li').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();
	});
	
		/* Books Intro01
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents #booksIntro01 ul li').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();
		});
		
		/* Books Intro02
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents #booksIntro02 ul li').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();
		});
		
		/* Books Intro03
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents #booksIntro03 ul li').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();
		});
		
		/* Books Intro04
		---------------------------------------- */
		var sets = [], temp = [];
		$('#contents #booksIntro04 ul li').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();
		});
	
	/* Books Others List
	---------------------------------------- */
	var sets = [], temp = [];
	$('#contents .booksOthersList ul li').each(function(i) {
			temp.push(this);
			if (i % 4 == 3) {
					sets.push(temp);
					temp = [];
			}
	});
	if (temp.length) sets.push(temp);

	$.each(sets, function() {
			$(this).flatHeights();
	});
	
	/* Books Others List
	---------------------------------------- */
	var sets = [], temp = [];
	$('#contents #careerInfo .infoSection .infoUnit').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();
	});
});


/* Alternate
========================================== */
$(function(){
	$('#contents table.bookListTable tr:odd').addClass('odd');
});

/* Smooth Scroll
========================================== */
$(function(){
	$('a[href^=#]').click(function(){
		var href = this.hash;
		if($(href).size()){
			$($.browser.safari ? 'body' : 'html').animate({scrollTop:$(href).offset().top}, 300, 'swing');
		}
		return false;
	});
});

/* get Date
========================================== */

$(function(){
	
	function getDayName(date){
		var weekName = new Array("日", "月", "火", "水", "木", "金", "土");
		return weekName[date.getDay()];
	}
	
	function getLastWeekMonday(){
		var date = new Date();
		return ( ( date.getDay() * -1 ) - 7 );
	}
	
	function getDate(d, vary){
		date = new Date(d.getFullYear(), d.getMonth() , d.getDate() + vary);
		var dateArray = new Array();
		dateArray['year'] = date.getFullYear();
		dateArray['month'] = (date.getMonth() + 1);
		dateArray['day'] = date.getDate();
		return dateArray;
	}
	
	$('span.today').each(function(){
		var date = new Date();
		var today = getDate(date, 0);
		$(this).append( today['year'] +'年' + today['month'] +'月' + today['day'] + '日' + '(' + getDayName(date) + ')' );
	});
	
	$('span.this_week').each(function(){
		var date = new Date();
		var lastWeekMonday = getLastWeekMonday();
		lastMonday = getDate(date, lastWeekMonday);
		lastSaturday = getDate(date, (lastWeekMonday + 6));
		$(this).append( '(' + lastMonday['year'] +'/' + lastMonday['month'] +'/' + lastMonday['day']
		 + '～'
		 + lastSaturday['month'] +'/' + lastSaturday['day'] + ')'
		 );
	});
	
});

/* input Text
========================================== */

var default_input = " 記事・書名の検索 ";
$(document).ready( function() {
	$(".keywordSearch label.textbox input").each(function(){
		$(this).val(default_input);
	});
});

$(function(){
	$(".keywordSearch .textbox input").focus(function(){
		if(this.value == default_input){
			$(this).toggleClass('input_color', true);
			$(this).val("");
		}
	});
	$(".keywordSearch .textbox input").blur(function(){
		if(this.value == ""){
			$(this).toggleClass('input_color', false);
			$(this).val(default_input);
		}
	});
	
	$(".keywordSearch .button input").click(function(){
 		var text_box = $(this).parent().parent().find(".textbox input");
		if(text_box.val() == default_input){
			text_box.val("");
			text_box.toggleClass('input_color', false);
		}
	});
	
});