//Written by Nathan Faubion: http://n-son.com
//Use this or edit how you want, just give me
//some credit!

function jsScroller (o, w, h) {
	var self = this;
	var list = o.getElementsByTagName("div");
	for (var i = 0; i < list.length; i++) 
	{
		if (list[i].className.indexOf("Scroller-Container") > -1) 
		{
			o = list[i];
		}
	}
	
	this.initMouseWheel = function(ele) {
		if (window.addEventListener)
			window.addEventListener('DOMMouseScroll', this.wheel, false);
		window.onmousewheel = document.onmousewheel = this.wheel;
		if( navigator.userAgent.match( "MSIE" ) ){ ele.attachEvent("onmousewheel", this.wheel ); }
		if( navigator.userAgent.match( "Gecko" ) && !navigator.userAgent.match( "Safari" ) ){ ele.addEventListener("DOMMouseScroll", this.wheel, false )};
		if( navigator.userAgent.match( "Safari" ) ){ ele.onmousewheel = this.wheel; };
	};
	
	/** Event handler for mouse wheel event.
	 */
	this.wheel = function(event){
		var delta = 0;
		if (!event) /* For IE. */
				event = window.event;
		if (event.wheelDelta) { /* IE/Opera. */
				delta = event.wheelDelta/120;
				/** In Opera 9, delta differs in sign as compared to IE.
				 */
				if (window.opera)
						delta = -delta;
		} else if (event.detail) { /** Mozilla case. */
				/** In Mozilla, sign of delta is different than in IE.
				 * Also, delta is multiple of 3.
				 */
				delta = -event.detail/3;
		}
		/** If delta is nonzero, handle it.
		 * Basically, delta is now positive if wheel was scrolled up,
		 * and negative, if wheel was scrolled down.
		 */
		if (delta)
		{
			if (delta < 0)
			{
				self.scrollBy(0, -10);
			}
			else
			{
				self.scrollBy(0, 10);
			}
		}
		/** Prevent default actions caused by mouse wheel.
		 * That might be ugly, but we handle scrolls somehow
		 * anyway, so don't bother here..
		 */
		if (event.preventDefault)
			event.preventDefault();
		event.returnValue = false;
	};
	
	//Private methods
	this._setPos = function (x, y) {
		//alert(this.viewableHeight + ' - ' + this.totalHeight);
		if (x < this.viewableWidth - this.totalWidth) 
			x = this.viewableWidth - this.totalWidth;
		if (x > 0) x = 0;
		if (y < this.viewableHeight - this.totalHeight) 
			y = this.viewableHeight - this.totalHeight;
		if (y > 0) y = 0;
		this._x = x;
		this._y = y;
		with (o.style) {
			left = this._x +"px";
			top  = this._y +"px";
		}
	};
	
	this.getHeight = function() {
      var sHGT;
	  var minorOffset = 38;
      if (this.content.innerHeight)
      {
           sHGT = this.content.innerHeight;
      }
      else
      {
         if (document.documentElement && document.documentElement.clientHeight)
         {
              sHGT = document.documentElement.clientHeight;
         }
         else
         {
            if (document.body)
            {
              sHGT = document.body.clientHeight;
            }
         }
      }
	  sHGT = sHGT + this.content.offsetHeight + (5*minorOffset);
	  this.totalHeight = sHGT;
	}
	
	//Public Methods
	this.reset = function () {
		this.content = o;
		var minorOffset = (document.all)? 25 : 38;
		this.totalHeight = o.height + minorOffset;
		this.totalWidth	 = o.offsetWidth;
		this._x = 0;
		this._y = 0;
		with (o.style) {
			left = "0px";
			top  = "0px";
		}
	};
	this.scrollBy = function (x, y) {
		this._setPos(this._x + x, this._y + y);
	};
	this.scrollTo = function (x, y) {
		this._setPos(-x, -y);
	};
	this.stopScroll = function () {
		if (this.scrollTimer) window.clearInterval(this.scrollTimer);
	};
	this.startScroll = function (x, y) {
		this.stopScroll();
		this.scrollTimer = window.setInterval(
			function(){ self.scrollBy(x, y); }, 40
		);
	};
	this.swapContent = function (c, w, h) {
		o = c;
		var list = o.getElementsByTagName("div");
		for (var i = 0; i < list.length; i++) {
			if (list[i].className.indexOf("Scroller-Container") > -1) {
				o = list[i];
			}
		}
		if (w) this.viewableWidth  = w;
		if (h) this.viewableHeight = h;
		this.reset();
	};
	
	//variables
	this.content = o;
	this.viewableWidth  = w;
	this.viewableHeight = h;
	this.totalWidth	 = o.offsetWidth;
	this.totalHeight = o.offsetHeight;
	this.scrollTimer = null;
	this.reset();
	this.getHeight();
};