

/* --------------------------------
	共通
-------------------------------- */


// 読み込み
$(function() {
	ux.init();
});


var ux = {
	
	// 実行
	init: function() {
		this.mouseover();
		this.scroller.init();
	},
	
	
	// マウスオーバー
	mouseover:function() {
		$('div.pagebar a, div#footer img, div#searchResult img, #submit, #searchsubmit').hover(function(){
			$(this).stop().fadeTo(400,0.5);
		},
		function(){
			$(this).stop().fadeTo(400,1.0);
		});
		
		$('div#gallery a').hover(function(){
			$(this).fadeTo(300,0.4);
		},
		function(){
			$(this).fadeTo(300,1.0);
		});
	},
	

	// スクローラー
	scroller: {
		// 設定
		conf: {
			pitch: 10,
			interval: 10
		},

		// 初期化
		init: function(conf) {
			var self = this;
			var temp = this.conf;
			for (var key in conf) temp[key] = conf[key];
			this.conf = temp;
			$('a[href^="#"]').each(function() {
				if (!this.hash || this.hash == '#') return;
				$(this).click(function(e) {
					e.preventDefault();
					this.blur();
					self.scroll(this.hash);
				});
			});
		},

		// スクロール
		scroll: function(id) {
			if (this.timer) clearInterval(this.timer);
			var self = this;
			var pitch = this.conf.pitch;
			var interval = this.conf.interval;
			var top = $(id).offset().top;
			var win = window;
			var $win = $(win);
			var scrollLeft = $win.scrollLeft();
			var limit = $(document).height() - $win.height();
			if (limit < 0) limit = 0;
			if (top > limit) top = limit;
			var dir = (top > $win.scrollTop()) ? 1 : -1;
			var math = (dir > 0) ? Math.ceil : Math.floor;
			top = math(top);
			this.timer = setInterval(function() {
				var scrollTop = $win.scrollTop();
				if (scrollTop == top) {
					clearInterval(self.timer);
					return;
				}
				scrollTop += math((top - scrollTop) / pitch);
				if (dir > 0 && scrollTop > top || dir < 0 && scrollTop < top) scrollTop = top;
				win.scrollTo(scrollLeft, scrollTop);
			}, interval);
		}
	}
}
