/*
   scrollbar.js
   Author:    Chad Lindstrom <webmaster(AT)chadlindstrom(DOT)ca>
   License:   http://creativecommons.org/licenses/by/1.0/

   Dependencies:
      - none
   Platform Support:
      - IE 6+, Firefox 1+, Safari 1.3+, Opera 8+
*/

function scrollbar(parentElm,target,scrollerPadding) {
   this.sb = getElm(parentElm);
   this.dir = 0;
   this.aniSpeed = 10;
   this.height = this.sb.className.split(" ")[1];

   this.target  = getElm(target);

   if(isNaN(parseInt(this.height)) == true){
      this.height = this.target.parentNode.offsetHeight;
   }

   this.sb.style.height = this.height+"px";

   this.arrowTop = createElm("DIV",this.sb.id+"_aTop",this.sb);
   this.arrowTop.className = "arrow arrowTop";
   this.arrowTop.sbRef = this;

   this.arrowBot = createElm("DIV",this.sb.id+"_aBot",this.sb);
   this.arrowBot.className = "arrow arrowBot";
   this.arrowBot.sbRef = this;

   this.track = createElm("DIV",this.sb.id+"_track",this.sb);
   this.track.className = "track";
   this.trackHeight = (this.height) - (this.arrowTop.offsetHeight + this.arrowBot.offsetHeight) - (scrollerPadding * 2);
   this.track.style.height = this.trackHeight+"px";
   this.track.sbRef = this;

   this.arrowTop.style.top = 0+scrollerPadding+"px";
   this.track.style.top = this.arrowTop.offsetHeight+scrollerPadding+"px";
   this.arrowBot.style.bottom = 0+scrollerPadding+"px";

   this.thumb = createElm("DIV",this.sb.id+"_thumb",this.track);
   this.thumb.className = "thumb";
   this.thumb.sbRef = this;
   this.thumb.thumbRef = null;

   this.thumbTop = createElm("DIV",this.sb.id+"_thumbTop",this.thumb);
   this.thumbTop.sbRef = this;
   this.thumbTop.thumbRef = this.thumb;

   this.thumbBtm = createElm("DIV",this.sb.id+"_thumbBtm",this.thumbTop);
   this.thumbBtm.sbRef = this;
   this.thumbBtm.thumbRef = this.thumb;

   this.thumbMid = createElm("DIV",this.sb.id+"_thumbMid",this.thumbBtm);
   this.thumbMid.sbRef = this;
   this.thumbMid.thumbRef = this.thumb;

   this.visibleArea = this.target.parentNode.offsetHeight;
   this.targetHeight = this.target.offsetHeight;
   this.targetScrollDistance =  this.visibleArea - this.targetHeight;

   if(this.targetScrollDistance >= 0) {
      this.targetHeight = 0
   }
   if(this.targetHeight == 0) {
      this.thumbHeight = 0;
      this.targetScrollDistance = 0;
      this.sb.parentNode.style.display = "none";
   }else{
      this.thumbHeight = Math.floor(Math.abs( (this.visibleArea/this.targetHeight)*this.trackHeight ) );
      this.thumbMidHeight = (this.thumbHeight + 8 )/2;

      this.thumb.style.height = this.thumbHeight+"px";            // Only makes sense to set these two
      this.thumbMid.style.height = this.thumbMidHeight+"px";      // heights if targetHeight > 0.
   }

   this.maxH = this.trackHeight - this.thumbHeight;

   addListener(this.thumb,"mousedown",passDragEventCall);
   addListener(this.thumbTop,"mousedown",passDragEventCall);
   addListener(this.thumbBtm,"mousedown",passDragEventCall);
   addListener(this.thumbMid,"mousedown",passDragEventCall);

   addListener(this.arrowTop,"mousedown",this.arrowDown);
   addListener(this.arrowBot,"mousedown",this.arrowDown);
   addListener(this.arrowTop,"mouseup",this.arrowUp);
   addListener(this.arrowBot,"mouseup",this.arrowUp);
   addListener(this.arrowTop,"mouseout",this.arrowUp);
   addListener(this.arrowBot,"mouseout",this.arrowUp);

   addListener(this.track,"mousedown",this.trackDown);
}

scrollbar.prototype.setScrollPerc = function() {
   if(window.activeScrollBar) {
      var sb = window.activeScrollBar;
      sb.scrollPerc = (sb.thumb.offsetTop/(sb.maxH));
      setY(sb.target, sb.targetScrollDistance*sb.scrollPerc);
   }
}

scrollbar.prototype.arrowDown = function(e) {
   if(!e) {
      var e = window.event;
   }
   var tg = (e.target) ? e.target : e.srcElement;
   window.activeScrollBar = tg.sbRef;

   if(window.activeScrollBar.targetScrollDistance == 0){
      return;
   }

   if(tg.id.indexOf("_aBot") >= 0){
      tg.sbRef.dir = 1;
      //tg.className = "arrow arrowBotOn";
   }else{
      tg.sbRef.dir = -1;
      //tg.className = "arrow arrowTopOn";
   }
   window.sbTimer = setInterval("moveThumb()",50);
}

scrollbar.prototype.arrowUp = function() {
   if(window.activeScrollBar) {
      if(window.activeScrollBar.dir == 1) {
          //window.activeScrollBar.arrowBot.className = "arrow arrowBot";
      }else{
          //window.activeScrollBar.arrowTop.className = "arrow arrowTop";
      }
      window.activeScrollBar.dir = 0;
      window.activeScrollBar.setScrollPerc();
   }
   window.activeScrollBar = null;
   if(window.sbTimer) {
      clearInterval(window.sbTimer);
   }
}

scrollbar.prototype.trackDown = function(e) {
var isSafari;
   if(!e) {
      var e = window.event;
   }
   var tg = (e.target) ? e.target : e.srcElement;

   if(tg.id.indexOf("_track") >= 0) {
      window.activeScrollBar = tg.sbRef;
      tg.sbRef.scrollPerc = ( (e.clientY+ ( (isSafari) ? 0 : getScrollTop(window) )  ) - getOffsetProperty(tg, "Top")   )/tg.sbRef.trackHeight;
      setY(tg.sbRef.thumb, tg.sbRef.maxH * tg.sbRef.scrollPerc);
      setY(tg.sbRef.target, tg.sbRef.targetScrollDistance*tg.sbRef.scrollPerc);
   }
}

scrollbar.prototype.reset = function(target) {
   if(typeof target != "object") {
      target = getElm(target);
   }
   this.target  = target;
   this.visibleArea = this.target.parentNode.offsetHeight;
   this.targetHeight = this.target.offsetHeight;
   this.targetScrollDistance =  this.visibleArea - this.targetHeight;

   if(this.targetScrollDistance >= 0) {
      this.targetHeight = 0
   }
   if(this.targetHeight == 0) {
      this.thumbHeight = 0;
      this.targetScrollDistance = 0;
      //this.sb.style.display = "none";
   }else{
      this.thumbHeight = Math.floor(Math.abs( (this.visibleArea/this.targetHeight)*this.trackHeight ) );
      this.sb.style.display = "block";
   }
   this.thumb.style.height = this.thumbHeight+"px";
   this.thumb.style.top = "0px";
   this.target.style.top = "0px";

   this.arrowTop.style.top = (this.trackHeight)+"px";
   this.arrowBot.style.top = (this.trackHeight+this.arrowBot.offsetHeight)+"px";
   this.maxH = this.trackHeight - this.thumbHeight;
}


scrollbar.prototype.scrollIntoView = function(itemTop, itemHeight) {
   var adjustScroll = false;
   var newTargetTop;
   var targetTop = this.target.offsetTop;

   if (itemTop > this.height - itemHeight - targetTop) {
      newTargetTop = -itemTop + (this.height - itemHeight);
      adjustScroll = true;
   } else if (itemTop < -targetTop) {
      newTargetTop = -itemTop;
      adjustScroll = true;
   }

   if (adjustScroll == true) {
      setY(this.target, newTargetTop);
      this.scrollPerc = (-this.target.offsetTop / (this.target.offsetHeight - this.height));
      setY(this.thumb, this.maxH * this.scrollPerc);
   }
}

function moveThumb() {
   var sb = window.activeScrollBar;
   var thumb = sb.thumb;
   if(isNaN(parseInt(thumb.style.top)) == true)
      thumb.style.top = "0px";

   moveBy(thumb,0,sb.dir*sb.aniSpeed,0);
   correctThumbPos(sb,sb.arrowUp);
   sb.setScrollPerc();
}

function correctThumbPos(sb,func) {
   if(getY(sb.thumb) <  0){
      setY(sb.thumb, 0);
      if(func) {
         func();
      }
   }
   if(getY(sb.thumb) > sb.maxH){
      setY(sb.thumb, sb.maxH);
      if(func){
         func();
      }
   }
}

function passDragEventCall(e) {

   if(!e){
      var e = window.event;
   }
   var tg = (e.target) ? e.target : e.srcElement;

   if(tg.thumbRef && tg.thumbRef != null){
   	tg = tg.thumbRef;
   }
   window.activeScrollBar = tg.sbRef;

   dragable(e,tg,tg.sbRef.track,tg.sbRef.setScrollPerc,function(){window.activeScrollBar=null});
}

