//
// Adapted from jsScrollbar written by Nathan Faubion: http://n-son.com
//

function bbh_ScrollBar_V1 (a_Content, a_ScrollBar, a_Options, a_Event, a_SyncContent) {
  var self = this;

  this.func_AddEvent = function (a_Object, a_Type, a_Function) {
    if (a_Object.addEventListener) {
       a_Object.addEventListener(a_Type, a_Function, false); }
    else {
      if (a_Object.attachEvent) {
        a_Object.attachEvent('on'+ a_Type, a_Function); }
      else {
        a_Object['on'+ a_Type] = a_Function; } }
  };

  this.func_RemoveEvent = function (a_Object, a_Type, a_Function) {
    if (a_Object.removeEventListener) {
      a_Object.removeEventListener(a_Type, a_Function, false); }
    else {
      if (a_Object.detachEvent) {
        a_Object.detachEvent('on'+ a_Type, a_Function); }
      else {
        a_Object['on'+ a_Type] = null; } }
  };

  this.func_FindComponent = function (a_Class, a_Object) {
    var i_Kids = a_Object.childNodes;
    var i_ClassName = "";
    for (var i = 0; i < i_Kids.length; i++) {
      if (i_Kids[i].className) {
        i_ClassName = i_Kids[i].className;
        if (i_ClassName.substr(i_ClassName.length - a_Class.length) == a_Class) {
          return i_Kids[i]; } } }
  };

  this.GetCellAt = function (a_Pos) {
    var i = 0;
    var i_Cell = self.p_NoCells;
    var i_Pos = a_Pos;
    if (i_Pos < 0) { i_Pos = 0; }
    if (i_Pos > this.p_LenContent) { i_Pos = this.p_LenContent; }
    for (i = 1; i < this.p_NoCells; i++) {
      if (this.p_PosCell[i] > i_Pos) {
        i_Cell = i;
        i = self.p_NoCells.length; } }
    i_Cell--;
    return i_Cell;
  };


  this.ScrollBarClick = function (a_Event) {
    if (self.p_Disabled) { return false; }
    var i_Event = a_Event ? a_Event : event;
    self.p_EventTarget = i_Event.target ? i_Event.target : i_Event.srcElement;
    var i_ClassName = self.p_EventTarget.className;
    i_ClassName = i_ClassName.substr(i_ClassName.lastIndexOf("-") + 1);
    self.p_ScrollRepeat = 0;
    switch (i_ClassName) {
      case "Up":       self.ScrollUp(); break;
      case "Down":     self.ScrollDown(); break;
      case "PageUp":   self.ScrollPageUp(); break;
      case "PageDown": self.ScrollPageDown(); break;
      case "Handle":   self.ScrollHandle(i_Event); break; }
    self.p_SelectFunc = document.onselectstart;
    document.onselectstart = function () {return false;};
    self.p_EventHandler(self.p_EventTarget, "mousedown");
    self.func_AddEvent(document, "mouseup", self.StopScroll, false);
    return false;
  };

  this.ScrollBarDrag = function (a_Event) {
    if (self.p_Disabled) { return false; }
    var i_Event = a_Event ? a_Event : event;
    var i_NewMouse = 0;
    var i_NewHandle = 0;
    var i_NewContent = 0;
    if (self.p_Dir == "Vert") {
      i_NewMouse = i_Event.clientY; }
    else {
      i_NewMouse = i_Event.clientX; }

    i_NewHandle = self.p_GrabHandle + (i_NewMouse - self.p_GrabMouse);
    i_NewContent = Math.round(i_NewHandle * self.p_Ratio);
    self.Scroll(i_NewContent);
  };

  this.ScrollBarWheel = function (a_Event) {
    if (self.p_Disabled) { return false; }
    var i_Event = a_Event ? a_Event : event;
    var i_Dir = 0;
    if (i_Event.wheelDelta >= 120) { i_Dir = -1; }
    if (i_Event.wheelDelta <= -120) { i_Dir = 1; }
    self.ScrollBy(i_Dir * 20);
    i_Event.returnValue = false;
  };


  this.StopScroll = function () {
    self.func_RemoveEvent(document, "mousemove", self.ScrollBarDrag, false);
    self.func_RemoveEvent(document, "mouseup", self.StopScroll, false);

    if (self.p_SelectFunc) {
      document.onselectstart = self.p_SelectFunc; }
    else {
      document.onselectstart = function () { return true; }; }
    if (self.p_ScrollBarTimer) {
      window.clearInterval(self.p_ScrollBarTimer);
      self.p_ScrollBarTimer = null;
      self.p_EventHandler (self.p_EventTarget, "mouseup");
      self.p_EventTarget = null; }
  };

  this.ScrollToCell = function (p_Cell) {
    var i_Cell = p_Cell;
    if (i_Cell < 0) { i_Cell = 0; }
    if (i_Cell > self.p_MaxCell) { i_Cell = self.p_MaxCell; }
    self.Scroll(self.p_PosCell[i_Cell]);
  };

  this.ScrollUp = function () {
    self.p_ScrollRepeat = self.p_ScrollRepeat + 1;
    if (self.p_ScrollRepeat != 1 && self.p_ScrollRepeat < self.p_RepeatSkip) { return; }
    if (self.p_ScrollMode == "Pixels") {
      self.ScrollBy(0 - self.p_StepSize); }
    else {
      if (self.p_FirstWholeCell == 0) {
        self.Scroll(0); }
      else {
        self.Scroll(self.p_PosCell[self.p_FirstWholeCell - 1]); } }
    if (self.p_ScrollBarTimer == null) {
      self.p_ScrollBarTimer = window.setInterval(function () { self.ScrollUp(); }, 40); }
  };

  this.ScrollDown = function () {
    self.p_ScrollRepeat = self.p_ScrollRepeat + 1;
    if (self.p_ScrollRepeat != 1 && self.p_ScrollRepeat < self.p_RepeatSkip) { return; }
    if (self.p_ScrollMode == "Pixels") {
      self.ScrollBy(self.p_StepSize); }
    else {
      if (self.p_LastWholeCell < self.p_MaxCells) {
        self.Scroll(self.p_PosCell[self.p_LastWholeCell + 1] + self.p_LenCell[self.p_LastWholeCell + 1] - self.p_LenViewable); }
      else {
        self.Scroll(self.p_MaxContent); } }
    if (self.p_ScrollBarTimer == null) {
      self.p_ScrollBarTimer = window.setInterval(function () { self.ScrollDown(); }, 40); }
  };

  this.ScrollPageUp = function () {
    self.p_ScrollRepeat = self.p_ScrollRepeat + 1;
    if (self.p_ScrollRepeat != 1 && self.p_ScrollRepeat < self.p_RepeatSkip) { return; }
    if (self.p_ScrollMode == "Pixels") {
      self.ScrollBy(0 - self.p_PageSize); }
    else {
      if (self.p_PosCell[self.p_FirstWholeCell] <= self.p_LenViewable) {
        self.Scroll(0); }
      else {
        var i_CellNo = self.GetCellAt(self.p_PosCell[self.p_FirstWholeCell] - self.p_LenViewable -1);
        i_CellNo++;
        if (i_CellNo == self.p_FirstWholeCell) {
          i_CellNo--; }
        self.Scroll(self.p_PosCell[i_CellNo]); } }
    if (self.p_ScrollBarTimer == null) {
      self.p_ScrollBarTimer = window.setInterval(function () { self.ScrollPageUp(); }, 40); }
  };

  this.ScrollPageDown = function () {
    self.p_ScrollRepeat = self.p_ScrollRepeat + 1;
    if (self.p_ScrollRepeat != 1 && self.p_ScrollRepeat < self.p_RepeatSkip) { return; }
    if (self.p_ScrollMode == "Pixels") {
      self.ScrollBy(self.p_PageSize); }
    else {
      var i_NewContent = self.p_MaxContent;
      if (self.p_LastWholeCell <= self.p_MaxCells) {
        i_NewContent = self.p_PosCell[self.p_LastWholeCell + 1];
        if (i_NewContent < self.p_MaxContent) {
          var i_NextPos = i_NewContent + self.p_LenViewable;
          var i_CellNo = self.GetCellAt(i_NextPos);
          i_NewContent = self.p_PosCell[i_CellNo] - self.p_LenViewable; } }
      self.Scroll(i_NewContent); }
    if (self.p_ScrollBarTimer == null) {
      self.p_ScrollBarTimer = window.setInterval(function () { self.ScrollPageDown(); }, 40); }
  };

  this.ScrollHandle = function (a_Event) {
    this.p_GrabHandle = this.p_PosHandle;
    if (this.p_Dir == "Vert") {
      this.p_GrabMouse = a_Event.clientY; }
    else {
      this.p_GrabMouse = a_Event.clientX; }
    this.func_AddEvent(document, "mousemove", this.ScrollBarDrag, false);
  };

  this.Scroll = function (a_Pos) {
    var i = 0;
    var i_CheckCell = 0;
    this.p_PosContent = a_Pos;
    if (this.p_PosContent > this.p_MaxContent) {
      this.p_PosContent = this.p_MaxContent; }
    if (this.p_PosContent < 0) {
      this.p_PosContent = 0; }
    this.p_PosHandle = Math.round(this.p_PosContent / this.p_Ratio);
    if (this.p_PosHandle > this.p_MaxHandle) {
      this.p_PosHandle = this.p_MaxHandle; }
    if (this.p_PosHandle < 0) {
      this.p_PosHandle = 0; }

    var i_LenPageDown = this.p_MaxTrack - this.p_PosHandle;

    if (this.p_Dir == "Vert") {
      this.p_PageUp.style.height = this.p_PosHandle +"px";
      this.p_PageDown.style.height = i_LenPageDown +"px";
      this.p_Content.scrollTop = this.p_PosContent;
      if (this.p_SyncContent) {
        this.p_SyncContent.scrollTop = this.p_PosContent; } }
    else {
      this.p_PageUp.style.width = this.p_PosHandle +"px";
      this.p_PageDown.style.width = i_LenPageDown +"px";
      this.p_Content.scrollLeft = this.p_PosContent;
      if (this.p_SyncContent) {
        this.p_SyncContent.scrollLeft = this.p_PosContent; } }

    this.p_LastContent = this.p_PosContent + this.p_LenViewable;
    if (this.p_ScrollMode == "Cell") {
      if (this.p_PosContent == 0) {
        this.p_FirstCell = 0;
        this.p_FirstWholeCell = 0; }
      else {
        this.p_FirstCell = this.GetCellAt(this.p_PosContent);
        if (this.p_PosCell[this.p_FirstCell] == this.p_PosContent) {
          this.p_FirstWholeCell = this.p_FirstCell; }
        else {
          this.p_FirstWholeCell = this.p_FirstCell + 1; } }
      if (this.p_LastContent > this.p_LenContent) {
        this.p_LastCell = this.p_NoCells - 1;
        this.p_LastWholeCell = this.p_NoCells - 1; }
      else {
        this.p_LastCell = this.GetCellAt(this.p_LastContent);
        this.p_LastWholeCell = this.p_LastCell - 1;
        if (this.p_PosCell[this.p_LastCell] == this.p_LastContent) {
          this.p_LastCell--; } } }
    else {
      this.p_FirstCell = 0;
      this.p_FirstWholeCell = 0;
      this.p_LastCell = 0;
      this.p_LastWholeCell = 0; }
    this.p_LastContent--;
    this.p_EventHandler (self.p_EventTarget, "mousemove");
  };
	
  this.ScrollBy = function (a_Value) {
    i_Pos = this.p_PosContent + a_Value;
    this.Scroll(i_Pos);
  };
  
  this.ScrollTo = function (a_Value) {
    this.Scroll(a_Value);
  };

  this.SetScrollCells = function (p_ClassName) {
    var i_CellSize = 0;
    var i_NetSize = 0;
    this.p_NoCells = 0;
    var i_Cells = this.p_Content.getElementsByTagName('div');
    for (var i = 0; i < i_Cells.length; i++) {
      if (i_Cells[i].className == p_ClassName) {
        if (this.p_Dir == "Vert") {
          i_CellSize = i_Cells[i].scrollHeight; }
        else {
          i_CellSize = i_Cells[i].scrollWidth; }
        this.p_PosCell[this.p_NoCells] = i_NetSize;
        this.p_LenCell[this.p_NoCells] = i_CellSize;
        this.p_EndCell[this.p_NoCells] = i_NetSize + i_CellSize - 1;
        this.p_NoCells++;
        i_NetSize = i_NetSize + i_CellSize; } }
    this.p_MaxCells = this.p_NoCells - 1;
    this.p_ScrollMode = "Cell";
    this.p_CellClass = p_ClassName;
    this.p_CellCalc = "dom";
//    this.Scroll(this.p_PosContent);
  };


  this.CalcCells = function (p_Width) {
    var i_Width = parseInt(p_Width, 10);
    var i_NetSize = 0;
    this.p_NoCells = 0;
    while (i_NetSize < this.p_LenContent) {
      this.p_PosCell[this.p_NoCells] = i_NetSize;
      this.p_LenCell[this.p_NoCells] = i_Width;
      this.p_EndCell[this.p_NoCells] = i_NetSize + i_Width - 1;
      this.p_NoCells++;
      i_NetSize = i_NetSize + p_Width; }
    this.p_MaxCells = this.p_NoCells - 1;
    this.p_ScrollMode = "Cell";
    this.p_CellClass = "";
    this.p_CellCalc = "width";
    this.p_CellWidth = p_Width;
//    this.Scroll(this.p_PosContent);
  };


  this.LoadSizing = function (a_LenContent, a_LenViewable, a_LenCell) {
    var i_LenContent = a_LenContent ? a_LenContent : -1;
    var a_LenViewable = a_LenViewable ? a_LenViewable : -1;
    var i_LenCell = a_LenCell ? a_LenCell : -1;
    if (this.p_Up.offsetLeft == this.p_Down.offsetLeft) {
      this.p_Dir = "Vert";
      this.p_LenContent = i_LenContent == -1 ? this.p_Content.scrollHeight : i_LenContent;
      this.p_LenViewable = a_LenViewable == -1 ? this.p_Content.offsetHeight : a_LenViewable;
      this.p_LenTrack = this.p_Track.offsetHeight;
      this.p_LenHandle = this.p_Handle.offsetHeight; }
    else {
      this.p_Dir = "Horiz";
      this.p_LenContent = i_LenContent == -1 ? this.p_Content.scrollWidth : i_LenContent;
      this.p_LenViewable = a_LenViewable == -1 ? this.p_Content.offsetWidth : a_LenViewable;
      this.p_LenTrack = this.p_Track.offsetWidth;
      this.p_LenHandle = this.p_Handle.offsetWidth; }
    this.p_MaxContent = this.p_LenContent - this.p_LenViewable;
    this.p_MaxTrack = this.p_LenTrack - this.p_LenHandle;
    if (this.p_MaxContent < 1) {
      this.p_MaxContent = 0;
      this.p_MaxTrack = 0;
      this.p_Ratio = 1; }
    else {
      this.p_Ratio = (this.p_LenContent - this.p_LenViewable) / this.p_MaxTrack; }
    this.p_PageSize = this.p_LenViewable * 0.9;
		
    if (this.p_MaxContent < 1) {
      this.p_Disabled = true;
      this.p_Handle.style.visibility = "hidden";
      if (this.p_AutoHide) {
        this.p_ScrollBar.style.visibility = "hidden"; }
      return; }

    this.p_Disabled = false;
    this.p_Handle.style.visibility = "visible";
    this.p_ScrollBar.style.visibility  = "visible";
    this.p_PosContent = this.p_Content.scrollLeft;
    this.p_PosHandle = Math.round(this.p_PosContent / this.p_Ratio);
    
    if (i_LenCell == -1) {
      if (this.p_ScrollMode == "Cell") {
        if (this.p_CellCalc == "dom") {
          this.SetScrollCells(this.p_CellClass); }
        else {
          this.CalcCells(this.p_CellWidth); } }   }
//      else {
//        this.Scroll(this.p_PosContent); } }
    else {
      this.CalcCells(i_LenCell); }
  };
  
  var i_LenContent = -1;
  var i_LenViewable = -1;
  var i_LenCell = -1;
  var i_Pos = 0;
  var i_Options = a_Options ? a_Options : "";
  var i_OptionName = "";
  var i_OptionValue = "";
  var i_OptionValue9 = 0;
  while (i_Options != "") {
    i_Pos = i_Options.indexOf("=");
    if (i_Pos == -1) {
      i_Options = ""; }
    else {
      i_OptionName = i_Options.substr(0, i_Pos).toLowerCase();
      i_Options = i_Options.substr(i_Pos + 1);
      i_Pos = i_Options.indexOf(";");
      if (i_Pos == -1) {
        i_OptionValue = i_Options;
        i_Options = ""; }
      else {
        i_OptionValue = i_Options.substr(0, i_Pos);
        i_Options = i_Options.substr(i_Pos + 1); }
      i_OptionValue9 = parseInt("0" + i_OptionValue, 10);
      switch (i_OptionName) {
        case "lencontent":
          i_LenContent = i_OptionValue9;
          break;
        case "lenviewable":
          i_LenViewable = i_OptionValue9;
          break;
        case "lencell":
          i_LenCell = i_OptionValue9;
          break; } } }

  this.p_Content = document.getElementById(a_Content);
  this.p_ScrollBar = document.getElementById(a_ScrollBar);
  this.p_AutoHide = false;
  this.p_ScrollMode = "Pixels";
  this.p_RepeatSkip = 10;
  this.p_NoCells = 0;
  this.p_MaxCells = 0;
  this.p_CellClass = "";
  this.p_CellCalc = "width";
  this.p_CellWidth = 0;
  this.p_PosCell = new Array();
  this.p_LenCell = new Array();
  this.p_EndCell = new Array();
  this.p_EventHandler = a_Event ? a_Event : function () {};
  this.p_EventTarget = null;
  this.p_SyncContent = a_SyncContent ? document.getElementById(a_SyncContent) : null;
  this.p_Up = self.func_FindComponent("-Up", this.p_ScrollBar);
  this.p_Down = this.func_FindComponent("-Down", this.p_ScrollBar);
  this.p_Track  = this.func_FindComponent("-Track", this.p_ScrollBar);
  this.p_PageUp = this.func_FindComponent("-PageUp", this.p_Track);
  this.p_Handle = this.func_FindComponent("-Handle", this.p_Track);
  this.p_PageDown = this.func_FindComponent("-PageDown", this.p_Track);
  this.p_PosHandle = 0;
  this.p_PosContent = 0;
  this.p_LastContent = 0;
  this.p_FirstCell = 0;
  this.p_FirstWholeCell = 0;
  this.p_LastCell = 0;
  this.p_LastWholeCell = 0;
  this.p_StepSize = 5;
  this.p_ScrollBarTimer = null;
  this.p_ContentTimer = null;
  this.p_SelectFunc = null;
  this.p_GrabMouse = null;
  this.p_GrabHandle = null;
  this.p_Disabled = false;
  this.p_Handle.ondragstart = function () {return false;};
  this.p_Handle.onmousedown = function () {return false;};
  this.func_AddEvent(this.p_Content, "mousewheel", this.ScrollBarWheel);
  this.func_RemoveEvent(this.p_ScrollBar, "mousedown", this.ScrollBarClick);
  this.func_AddEvent(this.p_ScrollBar, "mousedown", this.ScrollBarClick);

  this.p_Dir = "";
  this.p_LenContent = 0;
  this.p_LenViewable = 0;
  this.p_LenTrack = 0;
  this.p_LenHandle = 0;
  this.p_MaxContent = 0;
  this.p_MaxTrack = 0;
  this.p_Ratio = 1;
  this.p_PageSize = 0;
  this.p_Disabled = true;
  this.p_PosContent = 0;
  this.p_PosHandle = 0;

  this.LoadSizing(i_LenContent, i_LenViewable, i_LenCell);
};
