//jQuery.noConflict();
(function($) {
	$(function() {
		$('.scroll > a[href^=#], .scroll > area[href^=#]').initScroll();
	});
	
	$.fn.initScroll = function() {
		return (this.each(function() {
			$(this).click(function(){
				var target = $('#' + this.getAttribute('href').split('#')[1]);
				
				if (target) {
					$.scroller.start.init({
						targetPos: target.offset().top
					});
					
					return (false);
				}
			});
		}));
	};
	
	$.scroller = {
		start: (function() {
			var params;
			var TimerId;
			var stepCount = 0;
			var lastPos = -1;
			
			function move() {
				var currentXPos = getCurrentXPos();
				var currentYPos = getCurrentYPos();
				
				if (stepCount >= params.step) {
					window.scrollTo(currentXPos, params.targetPos);
					stepCount = 0;
				}
				else if (lastPos != currentYPos) {
					stepCount = 0;
				}
				else {
					stepCount++;
					window.scrollTo(currentXPos, getEasingY());
					lastPos = getEasingY();
					TimerId = setTimeout(move, Math.floor(1000 / params.fps)); 
				}
			};
			var getCurrentYPos = function() {
				return (document.body.scrollTop  || document.documentElement.scrollTop);
			}
			var getCurrentXPos = function() {
				return (document.body.scrollLeft  || document.documentElement.scrollLeft);
			}
			var getEasingY = function() {
				return (Math.floor(getEasing(params.startPos, params.targetPos, stepCount, params.step, params.easing)));
			}
			var getEasing = function(start, end, stepCount, step, easing) {
				var s = stepCount / step;
				return ((end - start) * (s + easing / (100 * Math.PI ) * Math.sin(Math.PI * s)) + start);
			}
			return {
				init: function(options) {
					params = $.extend({
						startPos : getCurrentYPos(),
						targetPos : 0,
						easing : 100,
						step : 30,
						fps : 60
					}, options);
					
					lastPos = params.startPos;
					TimerId = setTimeout(move, Math.floor(1000 / params.fps)); 
				}
			};
		})()
	};
})(jQuery);
