var __S = new Array();
var __S_dragId = null;

function __S_stop() {
  for(k in __S) {
    __S[k].stopDrag();
    __S[k].stopInterval();
  }
}

function __S_mousemove(ev) {
  if(!__S_dragId)
    return;
  if(!isIE()) {
    x = ev.offsetX?ev.offsetX:ev.layerX
    if(isNaN(x) || x>__S[__S_dragId].arrowWidth)
      __S[k].stopDrag();
  }
  if(__S[__S_dragId])
    __S[__S_dragId].doDrag(ev);
}

function __S_resize() {
  for(k in __S) {
    __S[k].resize();
  }
}

function Scroller(id, w, h, targetY, bFullScreen, btmMargin, barH, arrowW, arrowH, bolComboMode) {
  if(__S[id]) {
    alert("Scroller error:\n\nDuplicate id: "+id);
    return false;
  }

  //methods
  this.doScroll = Scroller_doScroll;
  this.showBar = Scroller_showBar;
  this.hideBar = Scroller_hideBar;
  this.setContent = Scroller_setContent;
  this.scrollTo = Scroller_scrollTo;
  this.stopInterval = Scroller_stopInterval;
  this.startScrolling = Scroller_startScrolling;
  this.mouseWheel = Scroller_mouseWheel;
  this.startDrag = Scroller_startDrag;
  this.stopDrag = Scroller_stopDrag;
  this.doDrag = Scroller_doDrag;
  this.setHeight = Scroller_setHeight;
  this.resize = Scroller_resize;
  this.btmMargin = btmMargin;
  this.bolComboMode = bolComboMode;

  //properties
  this.id = id;
  this.width = w;
  this.height = h;
  this.divContainer = getE("divScrollerContainer_"+id);
  this.divContent = getE("divScrollerContent_"+id);  
  this.divBar = getE("divScrollerBar_"+id);      
  this.divBox = getE("divScrollerBox_"+id);      
  this.imgBox = getE("imgScrollerBox_"+id);      
  this.contentHeight = 1;
  this.scrolling = 0;
  this.boxScrolling = 0;
  this.idInterval = null;
  this.boxHeight = 0;
  this.ratio = 1;
  this.bolDrag = false;
  this.dragY = null;
  this.arrowWidth = arrowW?arrowW:7;
  this.arrowHeight = arrowH?arrowH:7;
  this.scrollbarHeight = barH?barH:200;
  this.targetY = targetY?targetY:(getPageY(this.divContainer) + h);
  this.bolTargetY = targetY?true:false;
  this.bolFullScreen = bFullScreen;
  this.imgBoxX = getPageX(this.divBox);
    
  //initialization
  __S[id] = this;
  this.setContent();

  return this;
}

function Scroller_resize() {
  this.setContent();
}

function Scroller_setContent(html) {
  if(html)
    this.divContent.innerHTML = html;
  this.contentHeight = this.divContent.offsetHeight;
  this.setHeight(this.bolTargetY?this.targetY:this.height);
  if(this.contentHeight==0)
    this.contentHeight = 1;
  if(this.contentHeight > this.height) {
    this.boxHeight = Math.floor(this.height * (this.scrollbarHeight - 2*this.arrowHeight) / this.contentHeight);
    this.ratio = (this.scrollbarHeight - 2*this.arrowHeight)/this.contentHeight;
    this.imgBox.height = this.boxHeight;
    this.showBar();
  } else {
    this.hideBar();
  }
  if(html)
    this.scrollTo(0);
}

function Scroller_setHeight(y) {
  if(this.bolTargetY) {
    var h = y - getPageY(this.divContainer) - this.btmMargin;
    if(this.bolFullScreen)
      h = Math.max((document.body.clientHeight + document.body.scrollTop - getPageY(this.divContainer) - this.btmMargin), h);
    this.divContainer.style.height = h;
    this.height = h;
  } else {
    this.divContainer.style.height = y;
    this.height = y;
  }
  var boxScrolling = parseInt(this.divBox.style.top);
  if(boxScrolling <= this.arrowHeight) {
    boxScrolling = this.arrowHeight;
  } else if(boxScrolling + this.boxHeight > this.scrollbarHeight - this.arrowHeight) {
    boxScrolling = this.scrollbarHeight - this.arrowHeight - this.boxHeight;
  }
  this.divBox.style.top = boxScrolling;
  this.boxScrolling = boxScrolling;
}

function Scroller_showBar() {
  this.divBar.style.display = "";
  this.divBox.style.display = "";
}

function Scroller_hideBar() {
  this.scrollTo(0);
  this.divBar.style.display = "none";
  this.divBox.style.display = "none";
}

function Scroller_doScroll(step, stopAt) {
  var scrolling = this.scrolling + step;
  if(stopAt) {
    if((step > 0 && this.boxScrolling-this.arrowHeight < stopAt) || (step < 0 && (this.boxScrolling-this.arrowHeight+this.boxHeight) > stopAt)) {
      this.stopInterval();
      return;
    }
  }
  if(scrolling > 0) {
    scrolling = 0;
    this.stopInterval();
  }
  if(scrolling < -this.contentHeight + this.height) {
    scrolling = - this.contentHeight + this.height;
    this.stopInterval();
  }
  this.scrollTo(scrolling);
}

function Scroller_scrollTo(scrolling) {
  this.scrolling = scrolling;
  this.divContent.style.top = scrolling;
  this.boxScrolling = Math.ceil(this.arrowHeight + (-scrolling*this.ratio));
  this.divBox.style.top = this.boxScrolling;
} 

function Scroller_stopInterval() {
  if(this.idInterval)
    clearInterval(this.idInterval);
  this.idInterval = null;
}

function Scroller_startScrolling(dir, ev) {
  if(this.bolComboMode)
    __C_bolClick = true;
  var step = 10;
  var interval = 50;
  var stopAt = "null";
  if(dir==0) {
    interval = 150;
    var stopAt = ev.offsetY?ev.offsetY:(ev.layerY-this.arrowHeight);
    if(!stopAt)
      stopAt = "null";
    else {
      if(stopAt > this.boxScrolling)
        step = -this.height;
      else
        step = this.height;
    }
  } else if(dir==-1)
    step = -step;
  this.doScroll(step);
  this.stopInterval();
  this.idInterval = setInterval("__S['"+this.id+"'].doScroll("+step+", "+stopAt+");", interval);
}

function Scroller_mouseWheel() {
  if(this.contentHeight<=this.height)
    return false;
  if(window.event && window.event.wheelDelta)
    this.doScroll(Math.floor(window.event.wheelDelta/2));
}

function Scroller_startDrag(ev) {
  if(this.bolComboMode)
    __C_bolClick = true;
  this.dragY = ev.screenY;
  this.bolDrag = true;
  __S_dragId = this.id;
}

function Scroller_stopDrag() {
  this.bolDrag = false;
  this.dragY = null;
  __S_dragId = null;
}

function Scroller_doDrag(ev) {
  if(this.bolDrag) {
    var y = ev.screenY;
    var delta = this.dragY - y;
    this.dragY = y;
    var boxScrolling = parseInt(this.divBox.style.top)-delta;
    if(boxScrolling <= this.arrowHeight) {
      boxScrolling = this.arrowHeight;
      scrolling = 0;
    } else if(boxScrolling + this.boxHeight > this.scrollbarHeight - this.arrowHeight) {
      boxScrolling = this.scrollbarHeight - this.arrowHeight - this.boxHeight;
      scrolling = -this.contentHeight + this.height;
    } else
      scrolling = (this.arrowHeight - boxScrolling)/this.ratio;
    this.divBox.style.top = boxScrolling;
    this.divContent.style.top = scrolling;
    this.boxScrolling = boxScrolling;
    this.scrolling = scrolling;
  }
}
