// Create scrolling variable if it doesn't exist
if (!Scrolling) var Scrolling = {};
var oArr = new Array();

Scrolling = function () {

        this.add = function (oID, Horizontal)  {
            var x = new Scrolling.Scroller (document.getElementById(oID), Horizontal);
            oArr[oID] = x;
	}

        this.resize = function (oID)  {
          if (oID) {
            if (oArr[oID]) {
              oArr[oID].resize();
            }
          } else {
            for( var sIndex in oArr ) {
              var o = oArr[sIndex];
              o.resize();
            }
          }
	}

        this.displayArrows = function (displayValue, oID)  {
          if (oID) {
            oArr[oID].displayArrows(displayValue);
          } else {
            for( var sIndex in oArr ) {
              var o = oArr[sIndex];
              o.displayArrows(displayValue);
            }
          }
	}

        this.startScroll = function (oID, x, y)  {
          oArr[oID].startScroll(x,y);
	}

        this.stopScroll = function (oID)  {
          oArr[oID].stopScroll();
	}
}

Scrolling.Scroller = function (container, horizontal) {
	//private variables
	var self = this;
	var _components = {};
	var _dimensions = {};
	var _temporary  = {};
	var _timer, _ratioY, _ratioX;
	var _horizontal = horizontal ? true : false;
        var heightAndWidth = {};

	//public variables
	this.onMouseDown   = function (){};
	this.onMouseUp     = function (){};
	this.onScroll      = function (){};
	this.scrollAmount  = 5;
	this.scrollSpeed   = 50;
	this.disabled      = false;
        this.elementID     = container.id;
        this.element       = container;

	//private functions
	function initialize () {
		var c = _components;
		var d = _dimensions;

                // find the scroller container and scrollbar container
		var scrollercontainer = findComponent("scroller-container", container);
		c.scrollercontainer = scrollercontainer;
		c.scrollercontent = findComponent("scroller-content", scrollercontainer);
		c.contentcontainer = findComponent("content-container", c.scrollercontent);
		c.scrollertransleft = findComponent("scroller-trans-left", c.scrollercontent);
		c.scrollertransright = findComponent("scroller-trans-right", c.scrollercontent);

                c.scrollbarcontainer = findComponent("scrollbar-container", scrollercontainer);

                if (c.scrollbarcontainer) {
		  c.track  = findComponent("scrollbar-track", c.scrollbarcontainer);
		  c.handle = findComponent("scrollbar-handle", c.track);

                  if (c.handle) {
                    c.handle.ondragstart  = function () {return false;};
  	            c.handle.onmousedown = function () {return false;};
           //       addEvent(c.handle, "mousedown", scrollbarClickPrimer);
                  }
                  addEvent(c.scrollercontainer, "mousewheel", scrollbarWheel);
                  addEvent(c.scrollercontainer, "DOMMouseScroll", scrollbarWheel);
                  addEvent(c.scrollbarcontainer, "mousedown", scrollbarClickPrimer);
                }

	//	show();
	}

        function GetContentDimensions (element) {
          heightAndWidth.scrollheight = 0;
          heightAndWidth.scrollwidth = 0;

          var largestY = 0;
          var largestX = 0;
          
          // First get any ccsubmeasure nodes
          var measureContent = [];
          var submeasureContent = document.getElementsByClassName("ccsubmeasure", element);
          if (submeasureContent.length > 0) {
            for (var i = 0; (submeasure = submeasureContent[i]) != null; i++) {
              var temp = submeasure.getElementsByTagName("*");
              for (var j = 0; (element = temp[j]) != null; j++) {
                measureContent.push(element);
              }
            }
          } else {
            measureContent = document.getElementsByClassName("ccmeasure", element);
          }

          if (measureContent.length == 0) {
            measureContent = element.getElementsByTagName("*");
          }


          for (var i = 0; (measure = measureContent[i]) != null; i++) {
            oLeft = getObjLeft(measure);
            oTop = getObjTop(measure);
            oWidth = getObjWidth(measure);
            oHeight = getObjHeight(measure);

            if (largestY < oTop + oHeight) largestY = oTop + oHeight;
            if (largestX < oLeft + oWidth) largestX = oLeft + oWidth;
          }
          heightAndWidth.scrollheight = largestY;
          heightAndWidth.scrollwidth = largestX;
          return heightAndWidth;
        }

	function scroll (x, y) {

                if (_horizontal) {
                        if (x < 0) x = 0;
                        if (x < _dimensions.trackLeft + _dimensions.handleOffsetLeft) {
                           x = _dimensions.trackLeft + _dimensions.handleOffsetLeft;
                        }

                        if (x > _dimensions.trackWidth - _dimensions.handleWidth + _dimensions.trackLeft + _dimensions.handleOffsetLeft) {
                               	x = _dimensions.trackWidth - _dimensions.handleWidth +_dimensions.trackLeft + _dimensions.handleOffsetLeft;
                        }
                        _dimensions.x = x - _dimensions.trackLeft - _dimensions.handleOffsetLeft;

                        x = x - _dimensions.trackLeft - _dimensions.handleOffsetLeft;

		      //  _components.handle.style.left = x +"px";
		        _dimensions.y = 0;
                } else {
 		        if (y < 0) y = 0;
                        if (y < _dimensions.trackTop + _dimensions.handleOffsetTop)
                           y = _dimensions.trackTop + _dimensions.handleOffsetTop;

                        if (y > _dimensions.trackHeight - _dimensions.handleHeight + _dimensions.trackTop + _dimensions.handleOffsetTop)
      	         		y = _dimensions.trackHeight - _dimensions.handleHeight +_dimensions.trackTop + _dimensions.handleOffsetTop;

                        _dimensions.y = y - _dimensions.trackTop - _dimensions.handleOffsetTop;

		  //      if (_components.handle) _components.handle.style.top = y +"px";
		        _dimensions.x = 0;
                }


		scrollTo(Math.round(_dimensions.x * _ratioX), Math.round(_dimensions.y * _ratioY));
		self.onScroll();
	}

	function scrollDir(dir) {
          var c = _components;
          var d = _dimensions;

          x = parseFloat(c.contentcontainer.style.left);
          y = parseFloat(c.contentcontainer.style.top);
          if (_horizontal) {
            x = x + dir;
            if (x > 0) x = 0;
            if (x + -d.scroller_offsetwidth < -d.scrollwidth) {
              x = -(d.scrollwidth - d.scroller_offsetwidth);
            }
          } else {
            y = y + dir;
            if (y > 0) y = 0;
            if (y + -d.scroller_offsetheight < -d.scrollheight) {
              y = -(d.scrollheight - d.scroller_offsetheight);
            }
          }

          setPosition( x,  y);
	}

	 function scrollTo(x, y) {
		setPosition(-x, -y);
	}

	function setPosition (x, y) {
          var c = _components;
          var d = _dimensions;

          c.contentcontainer.style.left = x +"px";
          if (!isNaN(y)) c.contentcontainer.style.top  = y +"px";
          if (c.handle) {
            var h_x = -x / _ratioX
            var h_y = -y / _ratioY;
            if (!isNaN(h_x)) c.handle.style.left = h_x + "px";
            if (!isNaN(h_y)) c.handle.style.top = h_y + "px";
          }

	}

	function scrollbarClickPrimer (e) {
		if (self.disabled) return false;
		var cy = e.clientY + document.body.scrollTop;
		var d = _dimensions;

		e = e?e:event;
		if (!e.target) e.target = e.srcElement;

		_dimensions.trackTop = findOffsetTop(_components.track);
		if (_components.handle) {
                  _dimensions.handleOffsetLeft = e.clientX - d.handleLeft;
                  _dimensions.handleOffsetTop = e.clientY - d.handleTop;
                }
		scrollbarClick(e.target.className, e);
	}

        function show(){
            var c = _components;
            c.scrollercontainer.style.visibility = "visible";
        }

	function scrollbarClick (c, e) {
		var d  = _dimensions;
		var t  = _temporary;
		var cy = e.clientY + document.body.scrollTop;
		var cx = e.clientX + document.body.scrollLeft;

                var source = null;
		if (e.srcElement) source = e.srcElement.id;
		if (e.originalTarget) source = e.originalTarget.id;

		if (source == "scrollbar-up")
			startScroll(-self.scrollAmount);

		if (source == "scrollbar-down")
			startScroll(self.scrollAmount);

		if (source == "scrollbar-track") {
                  if (_horizontal) {
                    scroll(cx + _dimensions.handleOffsetLeft - d.handleWidth, 0);
                  } else {
                    scroll(0, cy + _dimensions.handleOffsetTop - d.handleHeight);
                  }
                }

		if (source == "scrollbar-handle") {
			addEvent(document, "mousemove", scrollbarDrag, false);
		}

		t.target = e.target;
		t.select = document.onselectstart;

		document.onselectstart = function (){ return false; };
		self.onMouseDown(e.target, c, e);

		addEvent(document, "mouseup", self.stopScroll);
	}

	function scrollbarDrag (e) {
		var d  = _dimensions;
		e = e?e:event;

                var x = e.clientX + d.handleOffsetLeft - d.handleWidth/2;
          	var y = e.clientY + d.handleOffsetTop - d.handleHeight/2;
	      scroll(x,y);
	}

	function scrollbarWheel (e) {
		if (self.disabled) return false;

		e = e ? e : event;
		var dir = 0;

		if (typeof e.wheelDelta == 'undefined') {
			if (e.detail > 0) dir = -1;
			if (e.detail < 0) dir = 1;
		} else {
			if (e.wheelDelta >= 120) dir = 1;
			if (e.wheelDelta <= -120) dir = -1;
		}

		scrollDir(dir * 20);

		if(e.stopPropagation) e.stopPropagation();
		if(e.preventDefault) e.preventDefault();
		e.cancelBubble = true;
		e.cancel = true;
		e.returnValue = false;
		return false;
	}

	this.startScroll = function(x, y) {
		_temporary.disty = y;
		_timer = window.setInterval(function () {self.scrollBy(x,y);}, self.scrollSpeed);
	}

	this.stopScroll = function () {
		removeEvent(document, "mousemove", scrollbarDrag);
		removeEvent(document, "mouseup", self.stopScroll);

		if (_timer) window.clearInterval(_timer);
	}

        this.resize = function ()  {
            self.reset();
            setPosition(0,0);
	}

	//public functions
	this.reset = function () {
		var d = _dimensions;
		var c = _components;

                d.handleOffsetLeft = 0;
                d.handleOffsetTop  = 0;
                d.handleHeight     = 0;
		d.handleWidth      = 0;
		d.handleLeft       = 0;
		d.handleTop        = 0;
                if (c.scrollbarcontainer) {
                  try {
                    d.trackTop     = findOffsetTop(c.track);
                    d.trackHeight  = c.track.offsetHeight;
                    d.trackLeft     = findOffsetLeft(c.track);
                    d.trackWidth  = c.track.offsetWidth;
                    d.handleHeight = c.handle.offsetHeight;
		    d.handleWidth  = c.handle.offsetWidth;
		    d.handleLeft   = findOffsetLeft(c.handle);
		    d.handleTop    = findOffsetTop(c.handle);
                  }
                  catch (e) {}
                }

                d.scroller_offsettop   = findOffsetTop(c.scrollercontainer);
	        d.scroller_offsetleft  = findOffsetLeft(c.scrollercontainer);
	        d.scroller_offsetwidth   = c.scrollercontainer.offsetWidth;
	        d.scroller_offsetheight  = c.scrollercontainer.offsetHeight;

		heightAndWidth = GetContentDimensions(c.contentcontainer);
                d.scrollwidth = heightAndWidth.scrollwidth;
                d.scrollheight = heightAndWidth.scrollheight;

                _ratioY = (d.scrollheight - d.scroller_offsetheight) / (d.trackHeight - d.handleHeight);
                if (!_ratioY || _ratioY == Infinity)
                    _ratioY=1;
		_ratioX = (d.scrollwidth - d.scroller_offsetwidth) / (d.trackWidth - d.handleWidth);
                if (!_ratioX)
                    _ratioX=1;

                if (c.handle) {
		  c.handle.ondragstart = function (){ return false; };
		  c.handle.onmousedown = function (){ return false; };
		  c.handle.style.top   = "0px";
		  c.handle.style.left   = "0px";
                }

		_x = 0;
		_y = 0;

		c.scrollercontainer.style.left = "0px";
		c.scrollercontainer.style.top  = "0px";

		setPosition(0,0);

                if (_horizontal) {
                  if (d.scrollwidth <= d.scroller_offsetwidth) {
                    this.disable()
                  } else {
                    this.enable();
                  }
                } else {
                  if (d.scrollheight <= d.scroller_offsetheight) {
                    this.disable()
                  } else {
                    this.enable();
                  }
                }
	}

	this.scrollBy = function (x, y) {
          var c = _components;
          var d = _dimensions;

          if (_horizontal) {
            x = parseFloat(c.contentcontainer.style.left) + (x / _ratioX);
            y = parseFloat(c.contentcontainer.style.top) ;
            if (x > 0) x = 0;
            if (x + -d.scroller_offsetwidth < -d.scrollwidth) {
              x = -(d.scrollwidth - d.scroller_offsetwidth);
            }
          } else {
            y = parseFloat(c.contentcontainer.style.top) + (y / _ratioY);
            if (y > 0) y = 0;
            if (y + -d.scroller_offsetheight < -d.scrollheight) {
              y = -(d.scrollheight - d.scroller_offsetheight);
            }
          }
          setPosition( x,  y);
	}

	this.disable = function () {
		var c = _components;
		this.disabled = true;
		self.showHideScroller("hidden", this.element);
	}

	this.enable = function () {
		var c = _components;
		this.disabled = false;
		self.showHideScroller("inherit", this.element);
	}

	this.showHideScroller = function (displayValue, element) {
                sbcs = document.getElementsByClassName("sbc-vertical" ,element);
                for (var i = 0; (sbc = sbcs[i]) != null; i++) {
                  sbc.style.visibility = displayValue;
                }
                var sbcs = document.getElementsByClassName("sbc-vertical_left", element);
                for (var i = 0; (sbc = sbcs[i]) != null; i++) {
                  sbc.style.visibility = displayValue;
                }
                sbcs = document.getElementsByClassName("sbc-horizontal" ,element);
                for (var i = 0; (sbc = sbcs[i]) != null; i++) {
                  sbc.style.visibility = displayValue;
                }
        }

	initialize();
}
