/*
   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) {
   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});
}
// global.js
//simple object validation
function check(e){
   if(typeof e == "string"){elm = getElm(e);}else{elm = e;}
   if(elm == null || typeof elm != "object") throw Error("Element "+e+" does not exist. It is type "+typeof elm);
   return elm;
}

function getElm(e,f){
   if (f == null) f = self;
   return f.document.getElementById(e);
}

/***
  GET/CREATE/DELETE/WRITE
                       ***/

function createElm(t,id,p){
   if (p == null)
      p = document.body;
   p = check(p);
   var elm = document.createElement(t);
   if (id != null && id != "") elm.id = id;
         p.appendChild(elm);
    return elm;
}

function deleteElm(e){e = check(e); e.parentNode.removeChild(e);}

function clearElm(e){
   e = check(e);
   while (e.hasChildNodes()) { e.removeChild(e.lastChild) };
}

function writeTo(e,c){
e = check(e);
if(c == null) c = " ";
 var tNode = document.createTextNode(c);
 e.appendChild(tNode);
}

// portions of this function ripped from http://wsabstract.com/javatutors/dynamiccontent4.shtml by Rey Nunez
function writeHTML(e,c){
 e = check(e);
 if(c == null) c = "<br />";
 if(document.innerHTML != -1){
  e.innerHTML = c;
 }else if(document.createRange != -1){
  rng = document.createRange();
  rng.setStartBefore(e);
  htmlFrag = rng.createContextualFragment(c);
  clearElm(e);
  e.appendChild(htmlFrag);
 }else{
  e.document.open();
  e.document.writeln(c);
  e.document.close();
 }
}

/***
   CLIPPING
           ***/
function setClip(e,t,r,b,l)
{
  if(t==null) t=0;
  if(r==null) r=0;
  if(b==null) b=0;
  if(l==null) l=0;
  e.style.clip='rect('+t+'px '+r+'px '+b+'px '+l+'px)';
}

function getClip(e,s)
{
   var clipv = e.style.clip.split('rect(')[1].split(')')[0].split(' ');
   for(i=0;i<clipv.length;i++){
      clipv[i]= parseInt(clipv[i]);
   }
   switch (s){
      case ('t') : return clipv[0]; break;
      case ('r') : return clipv[1]; break;
      case ('b') : return clipv[2]; break;
      case ('l') : return clipv[3]; break;
      default : return clipv; break;
   }
}
function clipBy(e,t,r,b,l)
{
   if(t==null) t=0;
   if(r==null) r=0;
   if(b==null) b=0;
   if(l==null) l=0;
   setClip(e, parseInt(getClip(e,'t') + t), parseInt(getClip(e,'r') + r), parseInt(getClip(e,'b') + b), parseInt(getClip(e,'l') + l));
}


/***
   SIZE
       ***/

function setWidth(e,w){if(w==null) w=0; e = check(e); e.style.width=w+"px"; }
function setHeight(e,h){if(h==null) h=0; e = check(e); e.style.height=h+"px";}
function getWidth(e){e = check(e); return parseInt(e.style.width); }
function getHeight(e){e = check(e); return parseInt(e.style.height);}
function growBy(e,w,h){if(w==null) w=0; if(h==null) h=0; setWidth(e,getWidth(e)+w); setHeight(e,getHeight(e)+h);}
function growTo(e,w,h){setWidth(e,w); setHeight(e,h);}

/***
  POSISTION ADJUST
                ***/

function setX(e,x){if(!x) x=0; e = check(e); e.style.left=x+"px"; }
function setY(e,y){if(!y) y=0; e = check(e); e.style.top=y+"px"; /* alert(y + " - " + e.style.top); */ }
function setR(e,r){if(!r) r=0; e = check(e); e.style.right=r+"px"; }
function setB(e,b){if(!b) b=0; e = check(e); e.style.bottom=b+"px"; }
function setZ(e,z){if(!z) z=1; e = check(e); e.style.zIndex=z; }

function getX(e){e = check(e); return parseInt(e.style.left); }
function getY(e){e = check(e); return parseInt(e.style.top); }
function getR(e){e = check(e); return parseInt(e.style.right); }
function getB(e){e = check(e); return parseInt(e.style.bottom); }
function getZ(e){e = check(e); return parseInt(e.style.zIndex); }
function moveBy(e,x,y,z){e = check(e); if(!x) x=0; if(!y) y=0; if(!z) z=0; setX(e,getX(e)+x); setY(e,getY(e)+y); setZ(e,getZ(e)+z);}
function moveTo(e,x,y,z){setX(e,x); setY(e,y); setZ(e,z)}

/***
  OPACITY
         ***/
function setOpacity(e,op){
  e = check(e);
  if(op==null) op=100;
  if(isNaN(op)) op = parseFloat(op);
  if(op<=1) op=op*100;
  e.style.filter = "alpha(opacity="+op+")";
  op = op/100;
  e.style.MozOpacity = op;
  e.style.KhtmlOpacity = op;
  e.style.opacity = op;
}

/***
  GET OFFSET PROPERTY
                ***/
function getOffsetProperty(elm, property, relativeElmID) {
   var offset = 0;
   var element = check(elm);
   if(typeof element == "undefined" || element == null)
   {
      return "NaN";
   }
   var relativeElm = document.getElementById(relativeElmID);

   if(relativeElm == null || typeof relativeElm == "undefined")
      relativeElm = document.body;
   do {
      offset += eval('element.offset' + property);
      element = element.offsetParent;
   } while (element != relativeElm && element != null);
   return parseInt(offset);
}

/***
  ADD AND REMOVE EVENT LISTENERS
                ***/

function addListener(elm,event,func){
  if(document.attachEvent){
    elm.attachEvent("on"+event, func);
  }else if(document.addEventListener){
    elm.addEventListener(event, func, true);
  }else{
      eval(elm+".on"+event+"="+func);
  }
}

function removeListener(elm,event,func){
  if(document.detachEvent){
    elm.detachEvent("on"+event, func);
  }else if(document.removeEventListener){
    elm.removeEventListener(event, func, true);
  }else{
      eval(elm+".on"+event+"= function(){return false;}");
  }
}

function stopBubbleStopReturn(event){
 if(event.stopPropagation){
    event.stopPropagation();
    event.preventDefault();
 }else  if(event.cancelBubble == false || event.returnValue == true){
    event.cancelBubble = true;
    event.returnValue = false;
 }
}

/***
  GET THE WINDOW SCROLL
                ***/

function getScrollTop(win) {
   var scrollTop = 0;
   if (win.pageYOffset) {
      scrollTop = win.pageYOffset;
   }
   else if (win.document.documentElement && win.document.documentElement.scrollTop) {
      scrollTop = win.document.documentElement.scrollTop;
   }
   else if (win.document.body) {
      scrollTop = win.document.body.scrollTop;
   }
   return scrollTop;
}

function encodeUrl(url) {
    return escape(url).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

/*******************
 dragable.js  v0.4
 http://www.obliquemotion.com/js/dragable.js
 by Byron Tredwell (byron(AT)obliquemotion(DOT)com)
******************/

function dragable(event,elm,boundingBox,movefunc,releaseFunc){

if(elm==null){
  if(event.srcElement){
    elm=event.srcElement;
  }else{
    elm=event.target;
  }
}

var maxLeft = 0;
var maxTop = 0;
var maxRight = 0;
var maxBottom = 0;

if (document.documentElement) {
   var maxRight = document.documentElement.offsetWidth;
   var maxBottom = document.documentElement.offsetHeight;
}else if (document.body && document.body.offsetWidth) {
   var maxRight = document.body.offsetWidth;
   var maxBottom = document.body.offsetHeight;
}

if(boundingBox != null && boundingBox != document.body)
{
  maxRight = boundingBox.offsetWidth;
  maxBottom = boundingBox.offsetHeight;
}

var x = parseInt(elm.style.left);
var y = parseInt(elm.style.top);

if(isNaN(x) == true)
   x = 0;
if(isNaN(y) == true)
   y = 0;
   
var deltaX = event.clientX - x;
var deltaY = event.clientY - y; 

stopBubbleStopReturn(event);
addListener(document,"mousemove",startDrag);
addListener(document,"mouseup",stopDrag);

  function startDrag(e){
     if(!e)
      var e = window.event;
    stopBubbleStopReturn(e);
    var newX = (e.clientX - deltaX);
    var newY = (e.clientY - deltaY);

    if( (newX >= 0 && newX <= maxRight-elm.offsetWidth) )
      elm.style.left = newX + "px";
    if( (newY >= 0 && newY <= maxBottom-elm.offsetHeight)  )
      elm.style.top  = newY + "px"; 

    if(movefunc)
      movefunc(newX,newY)
  }
  
  function stopDrag(e){
     if(!e)
      var e = window.event;
    stopBubbleStopReturn(e);
    removeListener(document,"mousemove",startDrag);
    removeListener(document,"mouseup",stopDrag);
    if(releaseFunc)
      releaseFunc();
  }
}
