/**
 * @version 0.0.1
 * @requires jquery.js
 */
/* ----------------------------------------------------------------------------------- */


/**
 * @namespace 
 */
var MOLTENLIB = {};

/**  */
MOLTENLIB.COMMON_DIR_NAME = "common";

/**  */
MOLTENLIB.previewWindowName = "PrintPreviewWindow";

/**  */
MOLTENLIB.common_dir_relative_path = "";


/* ----------------------------------------------------------------------------------- */
/**
 * @returns {String} 相対パスを返す。'../../common/'など。
 * 
*/
MOLTENLIB.getCommonDir = function() {
	var dirName = arguments.length? arguments[0] : MOLTENLIB.COMMON_DIR_NAME;
	var links   = document.getElementsByTagName('LINK');
	var reg = new RegExp( "(.*\/?" + dirName + "\/).+$" );
	if( links ){
		for( var i=0; i<links.length; i++ ){
			if( links[i].getAttribute("rel") && links[i].getAttribute("rel").indexOf("stylesheet") != -1 ) {
				if( links[i].href && links[i].href.match( reg ) )
					return RegExp.$1;
			}
		}
	}
	return '/' + dirName +'/';
}
MOLTENLIB.common_dir_relative_path = MOLTENLIB.getCommonDir();


/* ----------------------------------------------------------------------------------- */
/**
 * 
 * 
*/
if (window.name == MOLTENLIB.previewWindowName) {
	var linkElement = '<link type="text/css" rel="stylesheet" href="'+ MOLTENLIB.getCommonDir() + '/css/print_preview.css" media="screen,print" />';
	document.write(linkElement);
}

MOLTENLIB.PrintPreview = function () {
	this.targetBlock         = $("div.preview");
	this.openPreviewBtnId    = "open-preview-btn";

	if (this.targetBlock.length) {
		this.init();
	}
}

MOLTENLIB.PrintPreview.prototype.init = function () {
	var _this = this;
	_this.targetBlock.each(function () {
		var printBtnHtml = "";
		
		if (window.name == MOLTENLIB.previewWindowName) {
				printBtnHtml =  ''
								+ ''
								+ '<a href="javascript:window.close();">'
								+ '<img src="'+ MOLTENLIB.getCommonDir() + 'images/close_bt001.gif" alt="关闭" width="83" height="27" class="rollover" />'
								+ '</a>'
								+ '<a href="javascript:window.print();">'
								+ '<img src="'+ MOLTENLIB.getCommonDir() + 'images/print_bt002.gif" alt="印刷" width="83" height="27" class="rollover margin-left-s" />'
								+ '</a>';

				// プレビュー画面上でのリンクを禁止。
				// 現状、閉じる・印刷するボタン無効
				$("a").each(function() {
					//$(this).unbind("click");
					$(this).click(function () {
						//$(this).blur();
						return false;
					});
				});
				
		}
		else {
				printBtnHtml =  ''
								+ '<a href="javascript:void(0);">'
								+ '<img src="'+ MOLTENLIB.getCommonDir() + 'images/print_bt001.gif" alt="打印预览" width="95" height="23" id="'+ _this.openPreviewBtnId +'" class="rollover-prv" />'
								+ '</a>';
		}
		
		if (printBtnHtml) {
			$(this).html(printBtnHtml);
			//$(this).append(printBtnHtml);
			new RolloverImages('rollover-prv');
		}
	});


	$("#"+this.openPreviewBtnId).live("click", function () {
		
		var url = location.href.replace(/#.*/, "");
		
		// メイン画像保持用のクエリを付与
		// url += ()? "?photo="+ *** : "";
		
		var newWin = window.open(url, MOLTENLIB.previewWindowName);
		return false;
	});
}


/* ----------------------------------------------------------------------------------- */
MOLTENLIB.SmoothScroll = function () {

	this.selector    = $('a[href^=#], area[href^=#]');

	this.goToPageTop = "#container";

	this.speed       = 600;

	this.easing      = "easeOutExpo";

	this.noScrollCName    = "no-scroll";

	if (this.selector.length) {
		this.init();
	}
}

MOLTENLIB.SmoothScroll.prototype.init = function () {
	var _this = this;
	this.selector.each(function () {
		if (!$(this).hasClass(_this.noScrollCName)) {
			var fi = $(this).attr("href");
			if (fi == "#") return;//return false;
			var el = ($(fi).length) ? $(fi) : $("a[name="+fi.replace(/#/,"")+"]");
			if (el.length) {
				$(this).click(function(e) {
//					e.preventDefault();
					var target = el[0];
					$.scrollTo(target, {
						speed: _this.speed,
						easing: _this.easing,
						onAfter: function() {
							location.hash = "#"+fi.split("#")[1];;
						}
					});
					return false;
				});
			}
		}
	});
}


/* ----------------------------------------------------------------------------------- */
/**
 * サイト内検索フォーム用のプレースホルダ。
 * @class サイト内検索フォーム用のプレースホルダ。
 * @constructor
 */
MOLTENLIB.SearchPlaceholder = function () {

	/** 初期表示時の文字色。
		@type String @constant @private */
	this.d_fcolor = "#999999";// class attr

	/** 検索キーワード入力後の文字色。
		@type String @constant @private */
	this.fcolor = "#333333";// class attr

	/** 処理対象のフォームのセレクタ、及びキーワードの初期値を格納。targetに対象となる入力フォームを、defaultKeywordに初期キーワードをそれぞれ登録。
		@type Array @constant @private */
	this.formData = [
						{
							target : "#keyword",
							defaultKeyword : "输入关键词"
						}
					];
//	if () {
		this.init();
//	}
}

MOLTENLIB.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});
				}
			});
		});

	});
}


/* ----------------------------------------------------------------------------------- */
/**
 * 
 * 
*/
$(function() {

	if (jQuery.browser.msie && jQuery.browser.version == 6) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	}

	var roi = new RolloverImages('rollover', 'on');
	new MOLTENLIB.SearchPlaceholder();
	new MOLTENLIB.PrintPreview();
	new MOLTENLIB.SmoothScroll();

});


/* ----------------------------------------------------------------------------------- */
/**
 * 印刷ボタン、シェアツールを表示
 * 
 */
(function(){

var styles = ''
+'<style type="text/css">'
+'#contents-area #contents-header .tool {'
+'	display: block !important;'
+'}'
+'</style>';

document.write(styles);

})();


