/**
 * @fileOverview *************************
 * @version 0.0.1
 */
/* ----------------------------------------------------------------------------------- */


/**
 * @namespace 
 */
var EMLIB = {};


/* ----------------------------------------------------------------------------------- */
/**
 * スムーススクロール処理。
 * @class スムーススクロール
 * @constructor
 */
EMLIB.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) {
		this.init();
	}
}

/**
 * 初期化処理。とび先のid属性を持つ要素が無ければname属性を調べる。
 * ・アンカーリンク移動時の挙動
 * ・とび先がページの最下部付近の場合
 * @function
 * @return {Boolean} フラグメント識別子をURL末尾に付けるか否か。falseの場合は付けない。※falseの時は既に入っているものを消したい。
 */
EMLIB.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) {
					e.preventDefault();
					var target = el[0];
					$.scrollTo(target, {
						speed: _this.speed,
						easing: _this.easing,
						onAfter: function() {
							if (!fi.match(_this.goToPageTop)) {
								location.href = fi;
							}/*
							else {
								location.href = "./";
							}*/
						}
						/*,
						onAfter: function() {
							return (fi == _this.goToPageTop) ? false : true;
						}*/
					});
					//return (fi == _this.goToPageTop) ? false : true;
					// フラグメント識別子を後付けしてあげる必要あり。
					// ブラウザ別に処理が必要。
					//return true;
					//
				});
			}
		}
	});
}

/* ----------------------------------------------------------------------------------- */
$(function() {


	/* Setup EMLIB  */
	for (module in EMLIB) {
		var obj = EMLIB[module];
		if (obj && typeof obj == "function") {
			new EMLIB[module]();
		}
	}

});
