
var scrollNav = {

  mouseX : null,
  win : null,
  winContent : null,
  winView : null,
  winTravel : null,
  track : null,
  trackX : null,
  trackLength : null,
  thumb : null,
  thumbX : null,
  thumbXMax : null,
  thumbSize : null,
  dragOffset : null,

	init : function () {
	  try {
	    var slideNav = document.getElementById('slideNav');
  	  scrollNav.win = bv.dom.getElementsByClassName(slideNav,'div','scrollContainer')[0];
  	  scrollNav.winContent = scrollNav.win.getElementsByTagName('ol')[0];
  	  scrollNav.track = bv.dom.getElementsByClassName(scrollNav.win,'div','scrollTrack')[0];
  	  scrollNav.thumb = bv.dom.getElementsByClassName(scrollNav.track,'div','scrollThumb')[0];
	  } catch (err) {
	    return false;
	  }
    scrollNav.winView = scrollNav.win.offsetWidth;
    scrollNav.winTravel = scrollNav.winContent.offsetWidth;
    scrollNav.trackX = bv.css.getOffsetFromRoot(scrollNav.track).left;
    scrollNav.trackLength = scrollNav.track.offsetWidth;
    scrollNav.thumbSize = scrollNav.trackLength * (scrollNav.winView / scrollNav.winTravel); // the thumb is to the track, as the view is to the content
    scrollNav.thumb.style.width = scrollNav.thumbSize +'px';
    scrollNav.thumbX = bv.css.getOffset(scrollNav.thumb).left;
    scrollNav.thumbXMax = scrollNav.trackLength - scrollNav.thumbSize;
    
    bv.event.add(window, 'mousemove', scrollNav.dragThumb);
    bv.event.add(scrollNav.track, 'click', scrollNav.clickTrack);
    bv.event.add(scrollNav.thumb, 'mousedown', scrollNav.grabThumb);
    bv.event.add(window, 'mouseup', scrollNav.releaseThumb);// target the window, in case the lift the button while not over the thumb
    bv.event.add(scrollNav.thumb, 'click', function (e) { e.stopPropagation(); return false; });
	},

  clickTrack : function (e) {
    var clickX = e.offsetX || e.layerX;
    var newLoc = (clickX < scrollNav.thumbX) ? scrollNav.thumbX - scrollNav.thumbSize : scrollNav.thumbX + scrollNav.thumbSize;
    scrollNav.jumpTo(newLoc);
    return false;
  },
  
  grabThumb : function (e) {
    scrollNav.dragOffset = e.pageX - scrollNav.thumbX - scrollNav.trackX;
    return true;
  },
  
  releaseThumb : function (e) {
    scrollNav.dragOffset = null;
    return true;
  },
  
  dragThumb : function (e) {
    if (scrollNav.dragOffset !== null) {
      scrollNav.jumpTo(e.pageX - scrollNav.dragOffset - scrollNav.trackX);
      return false;
    }
  },
  
  jumpTo : function (x) {
    var newThumbLoc = Math.max(0, Math.min(x, scrollNav.thumbXMax));
    scrollNav.thumb.style.marginLeft = newThumbLoc+(newThumbLoc?'px':'');
    scrollNav.thumbX = newThumbLoc;
    var ratio = newThumbLoc / scrollNav.thumbXMax;
    var newWinLoc = -1* Math.round(ratio * (scrollNav.winTravel - scrollNav.winView));
    scrollNav.winContent.style.marginLeft = newWinLoc+(newWinLoc?'px':'');
  },


  complete : true

}

if (window.bv && bv.event) {
  bv.event.add(window, 'domready', scrollNav.init );
  bv.event.add(window, 'resize', scrollNav.init );
}





var thumbHilight = {
  lastThumb : null,
  init : function () {
    try {
      var Linkies = document.getElementById('slideNav').getElementsByTagName('a');
      for (var xx=0; xx<Linkies.length; xx++) {
        bv.event.add(Linkies[xx],'click', thumbHilight.hiliteThis);
      }
    } catch (err) { return false; }
  },
  hiliteThis : function () {
    bv.css.addClass(this,'thumbHilight');
    if (thumbHilight.lastThumb) {
      bv.css.removeClass(thumbHilight.lastThumb,'thumbHilight');
    }
    thumbHilight.lastThumb = this;
  },
  
  complete : true
}
if (window.bv && bv.event) bv.event.add(window, 'domready', thumbHilight.init );

