// BYKELLY 2010 Autumn - COLLECTION
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

function AMITH() {
	this.scrollInterval   = null;
	this.scrollTargetID   = '';
	this.waitDuration     = 500;
	this.updateDuration   = 10;
	this.easingSpeed      = 10;
}

AMITH.prototype = {
		
	addEventListener : function (o, e, f, c) {
		if (e == 'DOMMouseScroll') {
			if (window.addEventListener) window.addEventListener('DOMMouseScroll', f, false);
			window.onmousewheel = document.onmousewheel = f;
		}
		else {
			(o.addEventListener) ? o.addEventListener(e, f, c) : o.attachEvent('on' + e, f);
		}
	},

	removeEventListener : function (o, e, f, c) {
		if (e == 'DOMMouseScroll') {
			if (window.removeEventListener) window.removeEventListener('DOMMouseScroll', f, false);
			window.onmousewheel = document.onmousewheel = null;
		}
		else {
			(o.removeEventListener) ? o.removeEventListener(e, f, c) : o.detachEvent('on' + e, f);
		}
	},

	getElementsByClassName : function(tagName, className) {
		var results  = new Array();
		var elements = document.getElementsByTagName(tagName);
		for (var i = 0, count = elements.length; i < count; i++) {
			if (!elements[i].className) continue;
			var classes = elements[i].className.split(' ');
			for (var j = 0, n = classes.length; j < n; j++) {
				if (classes[j] == className) {
					results[results.length] = elements[i];
					break;
				}
			}
		}
		return results;
	},

	/*--------------------------------------------------*/

	getOffsetTop : function(o) {
		return o.offsetTop + ((o.offsetParent) ? K.getOffsetTop(o.offsetParent) : 0);
	},

	getOffsetLeft : function(o) {
		return o.offsetLeft + ((o.offsetParent) ? K.getOffsetLeft(o.offsetParent) : 0);
	},

	getScrollTop : function() {
		return document.body.scrollTop || document.documentElement.scrollTop;
	},

	/*--------------------------------------------------*/

	getStyle : function (elememt, property, pseudo) {
		var camelize = function(str) {
			return str.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); });
		};
		var deCamelize = function(str) {
			return str.replace(/[A-Z]/g, function($0) { return '-' + $0.toLowerCase(); });
		};
		if (elememt.currentStyle) {
			if (property.indexOf('-') != -1) property = camelize(property);
			return elememt.currentStyle[property];
		}
		else if (getComputedStyle) {
			if (property.indexOf('-') == -1) property = deCamelize(property);
			return document.defaultView.getComputedStyle(elememt, pseudo).getPropertyValue(property);
		}
		return '';
	},

	getFloatStyle : function(element) {
		var userAgent = navigator.userAgent.toLowerCase();
		var float = K.getStyle(element, 'float');
		if (!float) {
			var styleFloat = (userAgent.match(/msie/) && !userAgent.match(/opera/)) ? 'styleFloat' : 'cssFloat';
			float = K.getStyle(element, styleFloat);
		}
		return float;
	},

	/*--------------------------------------------------*/

	smoothScroll : function() {
		clearInterval(K.scrollInterval);
		var targetTop = K.getOffsetTop(document.getElementById(K.scrollTargetID));
		var pageHeight = (document.body.scrollHeight) ? document.body.scrollHeight : document.height;
		var windowHeight = (window.innerHeight) ? window.innerHeight : document.documentElement.offsetHeight;
		var scrollMax = pageHeight - windowHeight;
		if (targetTop >scrollMax) targetTop = scrollMax;
		var scrollTop = K.getScrollTop();
		var distance = targetTop - scrollTop;
		distance += (distance >0) ? K.easingSpeed : -K.easingSpeed;
		if (Math.abs(distance) >K.easingSpeed) {
			var moving = distance / K.easingSpeed;
			if (Math.abs(moving) < 1) moving = (moving >0) ? 1 : -1;
			window.scrollTo(0, scrollTop + moving);
			K.scrollInterval = setInterval(K.smoothScroll, K.updateDuration);
		}
		else {
			window.scrollTo(0, targetTop);
		}
	},

	setupAnchor : function() {
		var anchors = document.getElementsByTagName('a');
		for (var i = 0, length = anchors.length; i < length; i++) {
			var anchor = anchors[i].getAttribute('href', 2).replace(/#/g, '');
			if (!anchor || !document.getElementById(anchor)) continue;
			var clickHandler = function(e) {
				(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
				var anchor = (e.srcElement) ? e.srcElement : this;
				while (anchor.tagName.toLowerCase() != 'a') anchor = anchor.parentNode;
				K.scrollTargetID = anchor.getAttribute('href', 2).replace(/#/g, '');
				K.scrollInterval = setInterval(K.smoothScroll, K.updateDuration);
			};
			K.addEventListener(anchors[i], 'click', clickHandler, false);
		}
	},

	/*--------------------------------------------------*/
	
	addStopScrollEvent : function() {
		var stopScroll = function(e) {
			if (K.scrollInterval != null) {
				clearInterval(K.scrollInterval);
				K.scrollInterval = null;
			}
		};
		K.addEventListener(document, 'mousedown', stopScroll, false);
		K.addEventListener(window, 'DOMMouseScroll', stopScroll, false);
	}
	
}

function setupPage() {
	K.setupAnchor();
	K.addStopScrollEvent();
}

var K = new AMITH();
K.addEventListener(window, 'load', setupPage, false);

