

var JRILIB = {};


JRILIB.previewWindowName = "ContentPrintPreviewWindow";

if (window.name == JRILIB.previewWindowName) {
	var linkElement = '<link type="text/css" rel="stylesheet" href="/material/english/common/css/print_content.css" media="screen,print" />';
	document.write(linkElement);
}

/* ----------------------------------------------------------------------------------- */

JRILIB.PrintPreview = function () {
	this.targetBlock         = $(".content-utility");
	this.openPreviewBtnId    = "open-preview-btn";

	if (this.targetBlock.length) {
		this.init();
	}
}

JRILIB.PrintPreview.prototype.init = function () {
	var _this = this;
	_this.targetBlock.each(function () {
		var printBtnHtml = "";
		if (window.name == JRILIB.previewWindowName) {
				printBtnHtml =  ''
								+ '<a href="javascript:window.close();" id="close-btn">'
								+ '<img src="/image/english/common/com_bt017.gif" alt="Close" width="62" height="21" />'
								+ '</a>'
								+ '<a href="javascript:window.print();" id="print-btn">'
								+ '<img src="/image/english/common/com_bt004.gif" alt="Print" width="62" 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/english/common/com_bt016.gif" alt="Print preview" width="112" height="21" id="'+ _this.openPreviewBtnId +'" />'
								+ '</a>';
		}
		$(this).append(printBtnHtml);
	});

	$("#"+this.openPreviewBtnId).live("click", function () {
		var newWin = window.open(location.href.replace(/#.*/, ""), JRILIB.previewWindowName);
		return false;
	});
}

/* ----------------------------------------------------------------------------------- */

JRILIB.Copyright = function () {
	this.copyright   = $("#copyright");
	this.startYear   = "1997";
	this.currentYear = (new Date()).getFullYear();
	this.reg         = new RegExp("(" + "^Copyright[^\\s]+\\s" + ")");// "Copyright&copy; "

	if (this.copyright.length ) {
		this.init();
	}
}

JRILIB.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);
		}
	});
}

/* ----------------------------------------------------------------------------------- */

JRILIB.SmoothScroll = function () {
	this.selector    = $('a[href^=#], area[href^=#]');
	this.goToPageTop = "#header-area";
	this.speed       = 600;
	this.easing      = "easeOutExpo";
	this.noScrollCName    = "no-scroll";

	if (this.selector.length && window.name != JRILIB.previewWindowName) {
		this.init();
	}
}

JRILIB.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;
				});
			}
		}
	});
}

/* ----------------------------------------------------------------------------------- */

JRILIB.PdfLinkDetection = function () {
	this.regPdfLink = new RegExp("(\.pdf$|\.pdf#.*)", "i");
	this.targetNode = $("#contents-area a,#contents-container a");
	this.bannerBlock = $("#pdf-banner-block");
	this.pdfLinkNodes = [];

	if (this.targetNode.length) {
		this.init();
	}
}

JRILIB.PdfLinkDetection.prototype.init = function () {
	if (this.detectPdfLink() && this.bannerBlock.length) {
		this.bannerBlock.css({"display":"block"});
	}
}

JRILIB.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;
}

/* ----------------------------------------------------------------------------------- */

JRILIB.SelectBackNumber = function () {
	this.targetBlockCName = "backnumber-navi-block";
	this.targetForm = $(".backnumber-navi");

	if (this.targetForm.length) {
		this.init();
	}
}

JRILIB.SelectBackNumber.prototype.init = function () {
	var _this = this;

	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;
				}
			});
		});
	});
}

/* ----------------------------------------------------------------------------------- */

JRILIB.SearchPlaceholder = function () {
	this.d_fcolor = "#8a8a8a";// class attr
	this.fcolor = "#555555";// class attr
	this.formData = [
						{
							target : "#txt-search",
							defaultKeyword : "Site internal search"
						},
						{
							target : "#content-txt-search",
							defaultKeyword : "Please enter the keyword"
						}
					];
	this.init();
}

JRILIB.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});
				}
			});
		});

	});
}

/* ----------------------------------------------------------------------------------- */

JRILIB.HistoryBackBtn = function () {
	this.reg    = new RegExp("www\.jri\.co\.jp||202\.32\.75\.130");
	this.btn = $(".page-back");
	this.topicPath = $("#topic-path");
	this.topicPathCName = "page-back-type";
	this.referrer = document.referrer;

	if (this.referrer && this.btn.length && this.topicPath.length) {
		this.init();
	}
}

JRILIB.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();
	}
}

/* ----------------------------------------------------------------------------------- */

JRILIB.AgreementPolicy = function () {
	this.agreementCBox    = $("#agreement-checkbox");
	this.nextStepBtn     = $("#goto-next-btn");
	this.btnSrc    = "";
	this.btnOffSrc = "";
	this.btnSuffix = "off";
	this.buttons = [];
	this.btnDisabled = false;
	this.form = null;

	this.msg =   "Please confirm our Privacy Policy and check 'I agree to the Privacy Policy' if you agree. \n"
				+"You will advance to the form by clicking on the button below after you agree our Privacy Policy.";

	if (this.agreementCBox.length && this.nextStepBtn.length) {
		this.init();
	}
}

JRILIB.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);

	if (!this.agreementCBox.attr("checked")) {
		this.deactivate();
	}

	this.agreementCBox.click(function() {
		if (_this.agreementCBox.attr("checked")) {
			_this.activate();
		}
		else {
			_this.deactivate();
		}
	});

	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();
		}
	});
}

JRILIB.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;
}

JRILIB.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;
}

JRILIB.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 JRILIB  */
	for (module in JRILIB) {
		var obj = JRILIB[module];
		if (obj && typeof obj == "function") {
			new JRILIB[module]();
		}
	}

});



