/* Javascript for handling navigation scrolling
   Written by Brian Stanback <http://www.stanback.net>
   Requires: mootools v1.x
*/
var Scroller = {
	start: function() {
		Scroller.scrollId = 'sub-menu';  // Id for the scrollable page
		Scroller.upId = 'up';            // Id for the up arrow
		Scroller.dnId = 'dn';            // Id for the down arrow
		Scroller.pane = 'pane';          // Id for slide-in "pane"
		Scroller.inTransform = 0;        // Used to determine whether a transformation is currently occuring
		window.addEvent('domready', Scroller.finishLoad);    // Hide the load area
	},
	finishLoad: function() {
		Scroller.initScroll();
	},
	initScroll: function() {
		setTimeout("Scroller.scrollPage('init')", 200);
		$(Scroller.scrollId).setStyle('overflow', 'hidden');
		document.addEvent('mousewheel', function(event) {
			event = new Event(event);
 			if (event.wheel > 0) {
				Scroller.scrollPage('up', 60, 35);
			} else if (event.wheel < 0) {
				Scroller.scrollPage('down', 60, 35);
			}
		});
		document.addEvent('keydown', function(event) {
			event = new Event(event);
			switch (event.code) {
				case 38:
					Scroller.scrollPage('up', 25, 35);
					break;
				case 40:	
					Scroller.scrollPage('down', 25, 35);
			}
		});
	},
	scrollPage: function(dir, incr, speed) {
		incr = (incr == null) ? 200 : incr;
		speed = (speed == null) ? 950 : speed;
		var scrollObj = $(Scroller.scrollId);
		var pos = scrollObj.getSize();
		var curPos = pos['scroll']['y'];
		if (pos['size']['y'] < pos['scrollSize']['y']) {
			if (Scroller.inTransform < 1) {
				$('scroll-bar').setStyle('display', 'block');
				var scroller = new Fx.Scroll(scrollObj, {duration: speed});
				switch (dir) {
					case 'up':
						if (curPos > 0) {
							curPos -= incr;
							if (speed > 100) {
								Scroller.inTransform = 1;
								setTimeout(function() {Scroller.inTransform = 0;}, speed);
							}
							scroller.scrollTo(0, curPos);
						}
						break;
					case 'down':
						if ((curPos + pos['size']['y']) < pos['scrollSize']['y']) {
							curPos += incr;
							if (speed > 100) {
								Scroller.inTransform = 1;
								setTimeout(function() {Scroller.inTransform = 0;}, speed);
							}
							scroller.scrollTo(0, curPos);
						}
				}
				if (curPos > 0)
					$(Scroller.upId).addClass('ud_enabled');
				else
					$(Scroller.upId).removeClass('ud_enabled');
				if ((curPos + pos['size']['y']) < pos['scrollSize']['y'])
					$(Scroller.dnId).addClass('ud_enabled');
				else
					$(Scroller.dnId).removeClass('ud_enabled');
			}
		}
	}
};
Scroller.start();
