/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       Smooth Scroll w/auto setup
 *
 *    @version rev010.2005-09-14
 *    @requires common.js
 */
/* -------------------------------------------------------------------------- */


/* -------------------- Settings for BASmoothScrollAutoSetup -------------------- */

var BASMOOTHSCROLL_AUTOSETUP_ENABLED         = true;
var BASMOOTHSCROLL_AUTOSETUP_USE_POSTPROCESS = false;
var BASMOOTHSCROLL_AUTOSETUP_OFFSET_X        =  0;
var BASMOOTHSCROLL_AUTOSETUP_OFFSET_Y        = -10;



/* -------------------- Constructor : BASmoothScroll -------------------- */

function BASmoothScroll() {
	this.offsetX =  0;
	this.offsetY =  0;
	this.unit    =  5;
	this.wait    = 10;
	this.usePostProcess = false;
}
BASmoothScroll.prototype = {
	setTargetNode : function(node) {
		this.targetNode = node;
		this.timer      = null;
	},
	
	setFromNode : function(node) {
		this.fromNode = node;
	},
	
	startScroll : function() {
		BAGetGeometry();
		if (!this.timer) {
			var maxX   = (BA.geom.pageW > BA.geom.windowW) ? BA.geom.pageW - BA.geom.windowW : 0;
			var maxY   = (BA.geom.pageH > BA.geom.windowH) ? BA.geom.pageH - BA.geom.windowH : 0;
			var absOst = this.targetNode.getAbsoluteOffsetBA();
			var posX   = absOst.X + this.offsetX;
			var posY   = absOst.Y + this.offsetY;
			this.cuX   = BA.geom.scrollX;
			this.cuY   = BA.geom.scrollY;
			this.toX   = (posX < 0) ? 0 : (posX > maxX) ? maxX : posX;
			this.toY   = (posY < 0) ? 0 : (posY > maxY) ? maxY : posY;
			this.timer = new BASetInterval(arguments.callee, this.wait, this);
		}

		this.cuX += (this.toX - BA.geom.scrollX) / this.unit; if (this.cuX < 0) this.cuX = 0;
		this.cuY += (this.toY - BA.geom.scrollY) / this.unit; if (this.cuY < 0) this.cuY = 0;
		var newX = Math.floor(this.cuX);
		var newY = Math.floor(this.cuY);
		window.scrollTo(newX, newY);
		if (newX == this.toX && newY == this.toY) this.stopScroll();
	},
	
	stopScroll : function() {
		if (this.timer) {
			this.timer.clearTimer();
			this.timer = null;
			this.postProcess();
		}
	},
	
	postProcess : function() {
		if (this.usePostProcess && this.offsetX == 0 && this.offsetY == 0 &&
		    this.fromNode && (BA.ua.isGecko || BA.ua.isWinIE)) {
			var href = this.fromNode.getAttributeBA('href');
			if (href) location.href = href;
		}
	}
}



/* -------------------- Function : BASmoothScrollAutoSetup -------------------- */

function BASmoothScrollAutoSetup() {
	var html    = document.getElementsByTagNameBA('html')[0];
	var body    = document.getElementsByTagNameBA('body')[0];
	var div     = document.createElementBA('div');
	var anchors = document.getElementsByTagNameBA('a');

	if (!document.getElementById('top'   )) html.setAttributeBA('id', 'top'   );
	if (!document.getElementById('bottom'))  div.setAttributeBA('id', 'bottom');
	div.style.margin  = 0;
	div.style.padding = 0;
	div.style.clear   = 'both';
	body.appendChildBA(div);

	BASMOOTHSCROLL_AS = new BASmoothScroll;
	BASMOOTHSCROLL_AS.offsetX        = BASMOOTHSCROLL_AUTOSETUP_OFFSET_X;
	BASMOOTHSCROLL_AS.offsetY        = BASMOOTHSCROLL_AUTOSETUP_OFFSET_Y;
	BASMOOTHSCROLL_AS.usePostProcess = BASMOOTHSCROLL_AUTOSETUP_USE_POSTPROCESS;

	document.addEventListenerBA('click'     , function(e) { BASMOOTHSCROLL_AS.stopScroll() });
	document.addEventListenerBA('mousewheel', function(e) { BASMOOTHSCROLL_AS.stopScroll() });

	for (var i = 0, n = anchors.length; i < n; i++) {
		var ident = _getInternalLinkFragmentIdentifier(anchors[i]);
		if (ident) {
			var target = document.getElementById(ident) || document.getElementsByName(ident)[0];
			if (target) {
				anchors[i].__BASmoothScrollAutoSetup_targetNode__ = target;
				anchors[i].addEventListenerBA('click', function(e) {
					e.preventDefault();
					e.stopPropagation();
					e.currentTarget.blur();
					BASMOOTHSCROLL_AS.setTargetNode(e.currentTarget.__BASmoothScrollAutoSetup_targetNode__);
					BASMOOTHSCROLL_AS.setFromNode(e.currentTarget);
					BASMOOTHSCROLL_AS.startScroll();
				});
			}
		}
	}

	function _getInternalLinkFragmentIdentifier(node) {
		var ret      = '';
		var linkHref = node.getAttributeBA('href');
		if (linkHref && linkHref.match(/#/)) {
			linkHref = linkHref.split('#');
			if (!linkHref[0] || linkHref[0] == location.href.split('#')[0]) {
				ret = linkHref[1];
			}
		}
		return ret;
	}
}





/* -------------------- Main : register start-up -------------------- */

if (typeof BA == 'object' && BA.ua.DOMok && BASMOOTHSCROLL_AUTOSETUP_ENABLED) {
	var BASMOOTHSCROLL_AS;
	BAAddOnload(function() {
		BASmoothScrollAutoSetup();
		BASMOOTHSCROLL_AS.unit = 1;
	});
}
