function BASmoothScroll () {
	this.offsetX    =  0;
	this.offsetY    =  -10;
	this.scrollUnit =  5;
	this.scrollWait = 16;

	this.targets = [];
	this.timer   = null;
}

function isSamePage(obj) {
	if (!obj) { return false }

	var indexFiles = new Array(
		'index.html',
		'index.htm',
		'index.shtml',
		'index.shtm',
		'index.cgi',
		'index.php',
		'Default.asp'
	);

	var linkTo;
	if (!!obj.href) {
		if (obj.protocol != location.protocol) { return false; }
		if (obj.hostname != location.hostname) { return false; }
		if (obj.search != location.search) { return false }
		var linkTo = obj.pathname;
		if (linkTo.indexOf('/') != 0) { linkTo = '/'+linkTo; }
		var stayOn = location.pathname;
	} else {
		return false;
	}

	if (linkTo == stayOn) {
		return true;
	} else {
		if (!!indexFiles) {
			for (var i=0; i<indexFiles.length; i++) {
				if (linkTo.lastIndexOf(indexFiles[i]) == linkTo.length - indexFiles[i].length) {
					if (linkTo.replace(indexFiles[i], '') == stayOn) { return true }
					return false;
				} else if (stayOn.lastIndexOf(indexFiles[i]) == stayOn.length - indexFiles[i].length) {
					if (stayOn.replace(indexFiles[i], '') == linkTo) { return true }
					return false;
				}
			}
		}
		return false;
	}
}

BASmoothScroll.prototype = {
	init : function() {
		var oHTML = BA.getElementsByTagName('html')[0];
		var oBODY = BA.getElementsByTagName('body')[0];
		var cDIV  = BA.createElement('div');

		if (!document.getElementById('top'))    oHTML.id = 'pagetop';
		if (!document.getElementById('bottom')) cDIV.id  = 'bottom';
		cDIV.style.margin = cDIV.style.padding = 0;
		oBODY.appendChild(cDIV);
		oBODY.onclick = oBODY.onmousewheel = BASS.clearTimer;
		BASS.searchIDandNames();
		
	},

	searchIDandNames : function() {
		var elms = BA.getElementsByTagName('*');
		var ancs = BA.getElementsByTagName('a');

		for (var i = 0; i < elms.length; i++) {
			var id   = elms[i].id;
			var name = elms[i].name;
			if (id || name && elms[i].tagName.toLowerCase() == 'a'){
				BASS.targets['#' + ((id) ? id : name)] = elms[i];
			}
		}

		for (var i = 0; i < ancs.length; i++) {
			if (!ancs[i].href || !ancs[i].href.match(/#/)){
				continue;
			}
			if(isSamePage(ancs[i])){
				ancs[i].onclick = BASS.scroll;
			}
		}
	},

	scroll : function(e) {
		if (window.event){
			window.event.cancelBubble = true;
		} else if (e && e.stopPropagation) {
			e.stopPropagation();
		}

		var href = this.href.replace(/[^#]*/, '');
		BASS.href = href;
		
		if (!href || !BASS.targets[href]) return true;

		var temp_href = 'temp_' + href.substr(1);

		BASS.targets[href].name = (BASS.targets[href].name)? temp_href : '';
		BASS.targets[href].id = (BASS.targets[href].id)? temp_href : '';

		var x = BA.getOffset(BASS.targets[href], 'X') + BASS.offsetX;
		var y = BA.getOffset(BASS.targets[href], 'Y') + BASS.offsetY;
		
		BASS.transition = 1;
		document.body.style.visibility = 'hidden';
		BASS.scrollMain(x, y, BASS.scrollUnit, BASS.scrollWait);
		BASS.transition = 0;
		document.body.style.visibility = 'visible';
		BASS.targets[href].name = (BASS.targets[href].name)? href.substr(1) : '';
		BASS.targets[href].id = (BASS.targets[href].id)? href.substr(1) : '';
		return false;
	},

	scrollMain : function(toX, toY, unit, wait, cuX, cuY) {
		var w = window;
		var d = document.documentElement;
		var b = d.getElementsByTagName('body')[0];
		var isMacIE = (BA.env.isMac && BA.env.isIE);
		var scrollX = (w.scrollX) ? w.scrollX : (d.scrollLeft) ? d.scrollLeft : b.scrollLeft;
		var scrollY = (w.scrollY) ? w.scrollY : (d.scrollTop)  ? d.scrollTop  : b.scrollTop;
		var windowW = (w.innerWidth)  ? w.innerWidth  : (!isMacIE) ? d.offsetWidth : b.scrollWidth;
		var windowH = (w.innerHeight) ? w.innerHeight : (!isMacIE) ? d.offsetHeight: b.scrollHeight;
		var pageW   = (!isMacIE) ? b.scrollWidth  : d.offsetWidth;
		var pageH   = (!isMacIE) ? b.scrollHeight : d.offsetHeight;
		var maxW    = (pageW > windowW) ? pageW - windowW : 0;
		var maxH    = (pageH > windowH) ? pageH - windowH : 0;

		if(BASS.transition && !BA.env.isOpera){
			location.hash = BASS.href.substr(1);
		}

		BASS.clearTimer();
		if (!toX || toX < 0) toX = 0;
		if (!toY || toY < 0) toY = 0;
		if (!cuX) cuX = scrollX;
		if (!cuY) cuY = scrollY;
		if (!unit) unit = 5;
		if (!wait) wait = 16;
		if (toX > maxW) toX = maxW;
		if (toY > maxH) toY = maxH;
	
		cuX += (toX - scrollX) / unit; if (cuX < 0) cuX = 0;
		cuY += (toY - scrollY) / unit; if (cuY < 0) cuY = 0;
		var posX = Math.floor(cuX);
		var posY = Math.floor(cuY);
	
		window.scrollTo(posX, posY);
		if (posX != toX || posY != toY) {
			BASS.timer = setTimeout('BASS.scrollMain(' + toX + ',' + toY + ',' + unit + ',' + wait + ',' + cuX + ',' + cuY + ')', wait);
		}
	},

	clearTimer : function() {
		if (BASS.timer) clearTimeout(BASS.timer);
	}
};

if (typeof BA == 'object' && BA.env.DOMok && !BA.env.isSafari && !BA.env.isN6) {
	var BASS = new BASmoothScroll;
	BA.addOnload(BASS.init);
}

