//jQuery.noConflict();
//(function($) {
	$(function() {
		$('a[href^=#], area[href^=#]').initScroll();
	});
	
	$.fn.initScroll = function() {
		this.each(function() {
			$(this).click(function() {
				var target = $('#' + this.getAttribute('href').split('#')[1]);
				
				if (target) {
					$.scroller.start.init({
						targetPos: $(target).offset().top
					});
					
					return (false);
				}
				
				return (false);
			});
		});
		
		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(initParams) {
						params = $.extend({
							startPos : getCurrentYPos(),
							targetPos : 0,
							easing : 100,
							step : 30,
							fps : 60
						}, initParams);
						
						lastPos = params.startPos;
						TimerId = setTimeout(move, Math.floor(1000 / params.fps)); 
					}
				};
			}
		)()
	};
//})(jQuery);
