/**
* anchorScroll
* animates transition between anchor linking
* more info: http://www.xs4all.nl/~ppk/js/doctypes.html
* author: Brad Benninger of studio814 (www.studio814.com)
* note: currently scrollInc must be set to 2 otherwise the loop doesn't seem to end
* last updated: Oct 6, 2005
*/
var anchorScroll = {
	
	// targeted elements
	containerName: 'col1',
	onClassName: 'active',
	offClassName: 'visited',
	scrollSpeed: 50,
	scrollInc: 2,
	
	w3c: document.getElementById,
	iex: document.all,
	scrollLoop: false, 
	scrollInterval: null, // setInterval id
	currentBlock: null, // object reference

    // addEvent cross-browser event handling for IE5+, NS6 and Mozilla
    // By Scott Andrew
	addEvent: function(elm, evType, fn, useCapture)
	{
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture); 
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
	// setup scroll events
	addListeners: function()
	{
		if (!document.getElementById || !document.getElementsByTagName) return false;
		var all_links = document.getElementsByTagName('a');
		for (var i = 0; i < all_links.length; i++) {
			// only add listener to links that are anchors
			if (all_links[i].href.search(/^(.*)#(.*)$/) != -1) {
				anchorScroll.addEvent(all_links[i], 'click', anchorScroll.handleClick, false);
				// cancel the default action for safari
				//all_links[i].onclick = cancelClick;
			}
		}
	},
	
	handleClick: function(e)
	{
		var el;
		if (window.event && window.event.srcElement)
			el = window.event.srcElement;
		if (e && e.target)
			el = e.target;
		if (!el)
			return;
		
		// make sure we have a reference to the link element
		// some browsers may pass the text node within the element instead of the link itself
		while (el.nodeName.toLowerCase() != 'a' && el.nodeName.toLowerCase() != 'body')
			el = el.parentNode;

		// cancel the default action
		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault) {
			e.stopPropagation();
			e.preventDefault();
		}
		// remove everything before and including the # from the anchor
		anchorId = el.hash.substring(1);
		anchorScroll.scroll(anchorId);
	},
	
	getWindowHeight: function()
	{
		if (this.iex) {
			if (document.documentElement.clientHeight) {
				return document.documentElement.clientHeight;
			} else {
				return document.body.clientHeight;
			}
		} else {
			return window.innerHeight;
		}
	},
	
	getScrollLeft: function() {
		if (this.iex) {
			if (document.documentElement.scrollLeft) {
				return document.documentElement.scrollLeft;
			} else {
				return document.body.scrollLeft;
			}
		} else {
			return window.pageXOffset;
		}
	},
	
	getScrollTop: function() {
		if(this.iex) {
			if (document.documentElement.scrollTop) {
				return document.documentElement.scrollTop;
			} else { 
				return document.body.scrollTop;
			}
		} else {
			return window.pageYOffset;
		}
	},
	
	getElementYpos: function(el)
	{
		var y = 0;
		while (el.offsetParent) {
			y += el.offsetTop
			el = el.offsetParent;
		}
		return y;
	},
	
	scroll: function(anchorId)
	{
		if (!this.w3c) {
			location.href = "#" + anchorId;
			return;
		}
		if (this.scrollLoop) {
			clearInterval(this.scrollInterval);
			this.scrollLoop = false;
			this.scrollInterval = null;
		}
		
		// disable active block if already active
		/*if (this.currentBlock != null)
			this.currentBlock.className = this.offClassName;*/
		
		//activate active style for new block
		this.currentBlock = document.getElementById(anchorId);
		//this.currentBlock.className = this.onClassName;
		
		// get position of anchored element
		var doc = document.getElementById(this.containerName);
		var documentHeight = this.getElementYpos(doc) + doc.offsetHeight;
		var windowHeight = this.getWindowHeight();
		var ypos = this.getElementYpos(this.currentBlock);
		if (ypos > documentHeight - windowHeight)
			ypos = documentHeight - windowHeight;
		this.scrollTo(0, ypos);
	},
	
	scrollTo: function(x, y)
	{
		if (this.scrollLoop) {
			var left = this.getScrollLeft();
			var top = this.getScrollTop();
			if (Math.abs(left - x) <= 1 && Math.abs(top - y) <= 1) {
				window.scrollTo(x, y);
				clearInterval(this.scrollInterval);
				this.scrollLoop = false;
				this.scrollInterval = null;
			} else {
				window.scrollTo(left + (x - left) / this.scrollInc, top + (y - top) / this.scrollInc);
			}
		} else {
			this.scrollInterval = setInterval('anchorScroll.scrollTo(' + x + ',' + y + ')', this.scrollSpeed);
			this.scrollLoop = true;
		}
	}
};

// Initialize anchorScroll object
anchorScroll.addEvent(window, 'load', anchorScroll.addListeners, false);