/**
 * @fileOverview *************************
 * @version 0.0.5
 */
/* ----------------------------------------------------------------------------------- */


/**
 * @namespace 
 */
var JXHDLIB = {};


/** プレビュー画面に設定されるwindow名。本文印刷、スムーススクロールで使用。
	@type String @constant */
JXHDLIB.previewWindowName = "ContentPrintPreviewWindow";


// 自分がプレビュー画面の場合は、プレビュー画面用のスタイルシートを読み込み。
if (window.name == JXHDLIB.previewWindowName) {
	var linkElement = '<link type="text/css" rel="stylesheet" href="/file/common/css/print_content.css" media="screen,print" />';
	document.write(linkElement);
}


/* ----------------------------------------------------------------------------------- */

/**
 * 本文印刷処理。
 * @class 本文印刷処理。
 * @constructor
 */
JXHDLIB.PrintPreview = function () {

	/** 印刷ボタンの挿入先の要素。
	 * @type jQuery Object @constant @private */
	this.targetBlock         = $(".content-utility");

	/** プレビュー画面表示ボタンに付与されるid属性値
	 * @type String @constant @private */
	this.openPreviewBtnId    = "open-preview-btn";

	if (this.targetBlock.length) {
		this.init();
	}
}

/**
 * 初期化処理。印刷/プレビューボタンの書き出しとwindow.nameの設定。
 * @function
 */
JXHDLIB.PrintPreview.prototype.init = function () {
	var _this = this;
	_this.targetBlock.each(function () {
		var printBtnHtml = "";
		if (window.name == JXHDLIB.previewWindowName) {
				printBtnHtml =  ''
								+ '<a href="javascript:window.close();" id="close-btn">'
								+ '<img src="/image/common/com_bt017.gif" alt="閉じる" width="62" height="21" />'
								+ '</a>'
								+ '<a href="javascript:window.print();" id="print-btn">'
								+ '<img src="/image/common/com_bt004.gif" alt="印刷する" width="75" height="21" />'
								+ '</a>';

				// 予め全リンクを押せないようにしておき、プレビュー画面上での遷移を防ぐ
				$("a").each(function() {
					//$(this).unbind("click");
					$(this).click(function () {
						//$(this).blur();
						return false;
					});
				});
		}
		else {
				printBtnHtml =  ''
								+ '<a href="javascript:void(0);">'
								+ '<img src="/image/common/com_bt016.gif" alt="印刷プレビュー" width="112" height="21" id="'+ _this.openPreviewBtnId +'" />'
								+ '</a>';
		}
		$(this).append(printBtnHtml);
	});

	// 
	$("#"+this.openPreviewBtnId).live("click", function () {
		var newWin = window.open(location.href.replace(/#.*/, ""), JXHDLIB.previewWindowName);
		return false;
	});
}

/* ----------------------------------------------------------------------------------- */

/**
 * コピーライトの年部分の表示。現在の年をJSで出力。
 * @class コピーライトの年部分の出力
 * @constructor
 */
JXHDLIB.Copyright = function () {

	/** コピーライトの入った要素。
	    @type jQuery Object @constant @private */
	this.copyright   = $("#copyright");

	/** 開始年（yyyy）
	    @type Sting @constant @private */
	this.startYear   = "1997";

	/** 現在の年（yyyy）
	    @type Sting @private */
	this.currentYear = (new Date()).getFullYear();

	/** コピーライトに年を挿入する際に使用する、置換用の文字列。
	    @type RegExp @constant @private */
	this.reg         = new RegExp("(" + "^Copyright[^\\s]+\\s" + ")");// "Copyright&copy; "

	if (this.copyright.length ) {
		this.init();
	}
}

/**
 * 初期化処理、及びコピーライト書き出し。
 * @function
 */
JXHDLIB.Copyright.prototype.init = function () {
	var _this = this;
	this.copyright.each(function () {
		var t = $(this).html();
		if (t.match(_this.reg)) {
			var prefix = RegExp.$1;
			t = t.replace(_this.reg, prefix + _this.startYear + "-" + _this.currentYear + " "); // Copyright&copy; 1997-2009 The Japan Research Institute,Ltd.
			$(this).html(t);
		}
	});
}

/* ----------------------------------------------------------------------------------- */

/**
 * スムーススクロール処理。
 * @class スムーススクロール
 * @constructor
 */
JXHDLIB.SmoothScroll = function () {

	/** イベントを設定する要素を指定。
	    @type jQuery Object @constant @private */
	this.selector    = $('a[href^=#], area[href^=#]');

	/** 「ページ先頭へ戻る」のリンクの場合は、URLからフラグメント識別子を削除。
	    @type String @constant @private */
	this.goToPageTop = "#header-area";// 配列化が必要？

	/** アニメーション速度
	    @type Number @constant @private */
	this.speed       = 600;

	/** easing用キーワード
	    @type RegExp @constant @private */
	this.easing      = "easeOutExpo";

	/** スクロールさせたくない場合に設定するclass属性値
	    @type String @constant @private */
	this.noScrollCName    = "no-scroll";

	if (this.selector.length && window.name != JXHDLIB.previewWindowName) {
		this.init();
	}
}

/**
 * 初期化処理。とび先のid属性を持つ要素が無ければname属性を調べる。
 * ・アンカーリンク移動時の挙動
 * ・とび先がページの最下部付近の場合
 * @function
 * @return {Boolean} フラグメント識別子をURL末尾に付けるか否か。falseの場合は付けない。※falseの時は既に入っているものを消したい。
 */
JXHDLIB.SmoothScroll.prototype.init = function () {
	var _this = this;
	this.selector.each(function () {
		if (!$(this).hasClass(_this.noScrollCName)) {
			var fi = $(this).attr("href");
			if (fi == "#") return false;
			var el = ($(fi).length) ? $(fi) : $("a[name="+fi.replace(/#/,"")+"]");
			if (el.length) {
				$(this).click(function() {
					//e.preventDefault();
					var target = el[0];
					$.scrollTo(target, {
						speed: _this.speed,
						easing: _this.easing/*,
						onAfter: function() {
							return (fi == _this.goToPageTop) ? false : true;
						}*/
					});
					return (fi == _this.goToPageTop) ? false : true;
				});
			}
		}
	});
}

/* ----------------------------------------------------------------------------------- */

/**
 * PDFリンクの検出し、target="_blank"の設定、及びAdobe Readerバナーの表示
 * @class PDFリンク関連処理
 * @constructor
 */
JXHDLIB.PdfLinkDetection = function () {

	/** PDFファイルへのリンク検出用
	    @type RegExp @constant @private */
	this.regPdfLink = new RegExp("(\.pdf$|\.pdf#.*)", "i");

	/** PDFファイルへのリンク検出対象ノード
	    @type jQuery Object @constant @private */
	this.targetNode = $("#contents-area a");

	/** Adobe Readerバナーのブロック
	    @type jQuery Object @constant @private */
	this.bannerBlock = $("#pdf-banner-block");

	/** PDFリンクのノード格納用配列
	    @type Array @private */
	this.pdfLinkNodes = [];

	if (this.targetNode.length && this.bannerBlock.length) {
		this.init();
	}
}

/**
 * 初期化処理。PDFリンクが存在した場合、Adobe Readerバナーを表示。
 * @function
 */
JXHDLIB.PdfLinkDetection.prototype.init = function () {
	if (this.detectPdfLink()) {
		this.bannerBlock.css({"display":"block"});
	}
}

/**
 * PDFリンクを検索し、pdfLinkNodesに格納。その要素にtarget="_blank"を設定。
 * @function
 * @return {Boolean} PDFリンクが存在した場合はtrueを返す。
 */
JXHDLIB.PdfLinkDetection.prototype.detectPdfLink = function () {
	var _this = this;
	this.targetNode.each(function () {
		var href = $(this).attr("href");
		if (href.match(_this.regPdfLink)) {
			_this.pdfLinkNodes.push(this);
			this.setAttribute("target", "_blank");
		}
	});
	return _this.pdfLinkNodes.length ? true : false;
}

/* ----------------------------------------------------------------------------------- */

/**
 * プルダウンメニューからバックナンバーへの遷移。
 * @class プルダウンメニューからバックナンバーへの遷移。
 * @constructor
 */
JXHDLIB.SelectBackNumber = function () {

	/** 対象のプルダウンを含むバックナンバーブロックのclass属性。初期スタイル設定はdisplay: none;とする。
		@type String @constant @private */
	this.targetBlockCName = "backnumber-navi-block";

	/** 対象のプルダウンのform要素
		@type jQuery Object @constant @private */
	this.targetForm = $(".backnumber-navi");

	if (this.targetForm.length) {
		this.init();
	}
}

/**
 * 初期化処理。Submitボタン押下時、選択されているoption要素に設定されたvalue属性値（URL）へ移動。
 * @function
 */
JXHDLIB.SelectBackNumber.prototype.init = function () {
	var _this = this;

	// 子に$(".backnumber-navi")を持つ$(".backnumber-navi-block")のみ表示
	this.targetForm.parents().map(function(){
		if ($(this).hasClass(_this.targetBlockCName)) {
			$(this).show();
		}
	});

	this.targetForm.each(function () {
		$(this).submit(function (e) {
			e.preventDefault();
			$(this).find("option:selected").each(function () {
				var val = $(this).attr("value");
				if (val) {
					location.href = val;
				}
			});
		});
	});
}

/* ----------------------------------------------------------------------------------- */

/**
 * サイト内検索フォーム用のプレースホルダ。
 * @class サイト内検索フォーム用のプレースホルダ。
 * @constructor
 */
JXHDLIB.SearchPlaceholder = function () {

	/** 初期表示時の文字色。
		@type String @constant @private */
	this.d_fcolor = "#8a8a8a";// class attr

	/** 検索キーワード入力後の文字色。
		@type String @constant @private */
	this.fcolor = "#555555";// class attr

	/** 処理対象のフォームのセレクタ、及びキーワードの初期値を格納。targetに対象となる入力フォームを、defaultKeywordに初期キーワードをそれぞれ登録。
		@type Array @constant @private */
	this.formData = [
						{
							target : "#txtSearch",
							defaultKeyword : "サイト内検索"
						},
						{
							target : "#content-txtSearch",
							defaultKeyword : "お探しのキーワードを入力してください。"
						}
					];
//	if () {
		this.init();
//	}
}

JXHDLIB.SearchPlaceholder.prototype.init = function () {
	var _this = this;
	$.each(_this.formData, function (i) {
		var d_key = this.defaultKeyword;
		$(this.target).each(function () {
			var el = $(this);
			el.css({"color" : _this.d_fcolor})
				.attr({"value"  : d_key});

			el.focus(function () {
				if (el.attr("value") == d_key) {
					el.attr({"value" : ""});
				}
				el.css({"color" : _this.fcolor});
			});

			el.blur(function () {
				if (el.attr("value") == "") {
					el.css({"color" : _this.d_fcolor})
						.attr({"value"  : d_key});
				}
			});
		});

	});
}

/* ----------------------------------------------------------------------------------- */

/**
 * 「直前のページへ戻る」表示制御。
 * @class リファラーをチェックし、サイト内からの遷移であれば、パンくず右脇に「直前のページへ戻る」ボタンを表示。
 * @constructor
 */
JXHDLIB.HistoryBackBtn = function () {

	/** リファラー比較用の正規表現オブジェクト。
		@type RegExp @constant @private */
	this.reg    = new RegExp("www\.jri\.co\.jp");

	/** 「直前のページへ戻る」ボタンを含むブロック要素。
		@type jQuery Object @constant @private */
	this.btn = $(".page-back");

	/** パンくずリストを含むブロック要素。
		@type jQuery Object @constant @private */
	this.topicPath = $("#topic-path");

	/** 戻るボタン表示時のレイアウト調整用class属性値。
		@type String @constant @private */
	this.topicPathCName = "page-back-type";

	/** リファラーを格納。
		@type String */
	this.referrer = document.referrer;

	if (this.referrer && this.btn.length && this.topicPath.length) {
		this.init();
	}
}

/**
 * 初期化処理。リファラーをチェックし、ボタンの表示・非表示を制御。
 * @function
 */
JXHDLIB.HistoryBackBtn.prototype.init = function () {
	var _this = this;
	if (this.referrer && this.referrer.match(this.reg)) {
		var cname = this.topicPathCName
		this.topicPath.addClass(cname);
		this.btn.show();
	}
	else {
		this.btn.hide();
	}
}

/* ----------------------------------------------------------------------------------- */

/**
 * フォームへ遷移する直前の、個人情報保護方針への同意ボタン。
 * @class フォームへ遷移する直前の、個人情報保護方針への同意ボタン。
 * @constructor
 */
JXHDLIB.AgreementPolicy = function () {

	/** 同意確認用チェックボックス
		@type jQuery Object @constant @private */
	this.agreementCBox    = $("#agreement-checkbox");

	/** フォーム入力画面への遷移ボタン。
		@type jQuery Object @constant @private */
	this.nextStepBtn     = $("#goto-next-btn");

	/** 遷移ボタンの画像パス。
		@type jQuery Object @private */
	this.btnSrc    = "";
	this.btnOffSrc = "";
	this.btnSuffix = "off";


	/** プリロードした画像格納用。
		@type Array @private */
	this.buttons = [];

	/** 遷移ボタンの状態確認用フラグ。true：選択不可、false：選択可能。
		@type Boolean @private */
	this.btnDisabled = false;

	this.form = null;


	/** 同意確認用チェックボックスにチェックする前に遷移ボタンを押した場合に表示するメッセージ。
		@type String @constant @private */
	this.msg =   "個人情報保護方針をご確認の上、同意いただける場合は\n"
				+"「個人情報保護方針に同意する」にチェックを入れてから、\n"
				+"ボタンを押して入力ページにお進みください。";

	if (this.agreementCBox.length && this.nextStepBtn.length) {
		this.init();
	}
}

/**
 * 初期化処理。遷移ボタンの初期化、及び、同意確認用チェックボックスへのイベント設定。
 * @function
 */
JXHDLIB.AgreementPolicy.prototype.init = function () {
	var _this = this;

	var imgsrc = this.nextStepBtn.attr("src");
	this.btnSrc   = imgsrc;
	this.preloadImage(this.btnSrc);

	var reg = /\.(jpe?g|gif|png)$/i;
	this.btnOffSrc = imgsrc.replace(reg, this.btnSuffix+".$1");
	this.preloadImage(this.btnOffSrc);

	// nextボタンの初期値設定。既にチェック済み（ヒストリーバック時など）の場合は遷移ボタンもONに。
	if (!this.agreementCBox.attr("checked")) {
		this.deactivate();
	}

	// イベント設定
	this.agreementCBox.click(function() {
		if (_this.agreementCBox.attr("checked")) {
			_this.activate();
		}
		else {
			_this.deactivate();
		}
	});

	// 親ノードを辿りform要素を取得。
	var formEl = (function(el) {
					return (el.tagName.toLowerCase()=="form") ?
								el :
									(el.parentNode) ?
										arguments.callee(el.parentNode) : null;
				})(this.nextStepBtn.get(0));

	this.form = $(formEl);

	// 
	this.nextStepBtn.click(function(e) {
		e.preventDefault();
		if (_this.btnDisabled) {
			alert(_this.msg);
		}
		else {
			_this.form.submit();
		}
	});
}


/**
 * 遷移ボタンの活性化。遷移ボタンをON画像へ差し替え、及びdisabledの解除。
 * @function
 */
JXHDLIB.AgreementPolicy.prototype.activate = function () {
	if (this.nextStepBtn.attr("src") != this.btnSrc) {
		this.nextStepBtn.attr({"src": this.btnSrc});
	}
	//this.nextStepBtn.attr({"disabled": false});
	this.btnDisabled = false;
}

/**
 * 遷移ボタンの非活性化。遷移ボタンをOFF画像へ差し替え、及びdisabledに。
 * @function
 */
JXHDLIB.AgreementPolicy.prototype.deactivate = function () {
	if (this.nextStepBtn.attr("src") != this.btnOffSrc) {
		this.nextStepBtn.attr({"src": this.btnOffSrc});
	}
	//this.nextStepBtn.attr({"disabled": true});
	this.btnDisabled = true;
}

/**
 * 遷移ボタン画像のプリロード。
 * @function
 */
JXHDLIB.AgreementPolicy.prototype.preloadImage = function (path) {
	var img = new Image();
	img.src = path;
	this.buttons.push(img);
}


/* ----------------------------------------------------------------------------------- */
$(function() {

	/* for IE6 background image flicker */
	if (jQuery.browser.msie && jQuery.browser.version == 6) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	}

	/* Setup RolloverImages */
	var roi = new RolloverImages('rollover', 'on');

	/* Setup JXHDLIB  */
	for (module in JXHDLIB) {
		var obj = JXHDLIB[module];
		if (obj && typeof obj == "function") {
			new JXHDLIB[module]();
		}
	}

});



