/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

function HectorVerticalScrollbar(scrollbar) {
  this.scrollbar = scrollbar;
  this.scrollarea = null;
  
  this.minThumbSize = 28; //Read/Write; the smallest scroller thumb size allowed
  this.padding = -1; //Read/Write; the padding on the scroll bar
  this.autohide = true;  //Read only; reflects if the scroll bar is always shown or only when there is scrollable content
  this.hidden = false;  //Read only; TRUE if the scroll bar is hidden, FALSE if it shown
  this.size = 19;  //Read only; the height of a vertical scroll bar or the width of a horizontal scroll bar, in pixels
  this.trackStartPath = null; //Read only; the path to the current image used for the left end of a horizontal scroll bar's track or the top end of a vertical scroll bar's track
  this.trackStartLength = 18;  //Read only; if used on a horizontal scroll bar, the width of the image specified as trackStartPath ; if vertical, the height of the image specified as trackStartPath 
  this.trackMiddlePath = null; //Read only; the path to the current image used middle of the scroll bar's track 
  this.trackEndPath = null; //Read only; the path to the current image used for the right end of a horizontal scroll bar's track or the bottom end of a vertical scroll bar's track
  this.trackEndLength = 18; //Read only; if used on a horizontal scroll bar, the width of the image specified as trackEndPath ; if vertical, the height of the image specified as trackEndPath
  this.thumbStartPath = null; //Read only; the path to the current image used for the left end of a horizontal scroll bar's scroller thumb or the top end of a vertical scroll bar's scroller thumb
  this.thumbStartLength = 9;  //Read only; if used on a horizontal scroll bar, the width of the image specified as thumbStartPath ; if vertical, the height of the image specified as thumbStartPath
  this.thumbMiddlePath = null; //Read only; the path to the current image used middle of the scroll bar's scroller thumb 
  this.thumbEndPath = null; //Read only; the path to the current image used for the right end of a horizontal scroll bar's scroller thumb or the bottom end of a vertical scroll bar's scroller thumb
  this.thumbEndLength = 9; //Read only; if used on a horizontal scroll bar, the width of the image specified as thumbEndPath ; if vertical, the height of the image specified as thumbEndPath
  
  this.styles = null;
  
  if (scrollbar == null) {
    return;
  }
  
  this.setupDefaults();
  
  this.setupLayout();
}  
  
HectorVerticalScrollbar.prototype.setupLayout = function() {
  this.scrollbar.style[this.styles.fixed] = this.size + "px";
  var doc = this.scrollbar.ownerDocument;
  
  //track
  this.track = doc.createElement("div");
  this.scrollbar.appendChild(this.track);
  this.track.style.position = "absolute";
  this.track.style.width = "100%";
  this.track.style.height = "100%";
  
  var element = doc.createElement("div");
  this.track.appendChild(element);
  element.style.position = "absolute";
  element.style[this.styles.start] = "0px";
  element.style[this.styles.variable] = this.trackStartLength + "px";
  element.style[this.styles.fixed] = "100%";
  element.style.backgroundImage = "url(" + this.trackStartPath + ")";
  element.style.backgroundRepeat = "no-repeat";

  element = doc.createElement("div");
  this.track.appendChild(element);
  element.style.position = "absolute";
  element.style[this.styles.start] = this.trackStartLength + "px";
  element.style[this.styles.end] = this.trackEndLength + "px";
  element.style[this.styles.fixed] = "100%";
  element.style.backgroundImage = "url(" + this.trackMiddlePath + ")";
  element.style.backgroundRepeat = this.styles.repeat;

  element = doc.createElement("div");
  this.track.appendChild(element);
  element.style.position = "absolute";
  element.style[this.styles.end] = "0px";
  element.style[this.styles.variable] = this.trackEndLength + "px";
  element.style[this.styles.fixed] = "100%";
  element.style.backgroundImage = "url(" + this.trackEndPath + ")";
  element.style.backgroundRepeat = "no-repeat";

  //thumb
  this.thumb = doc.createElement("div");
  this.track.appendChild(this.thumb);
  this.thumb.style.position = "absolute";
  this.thumb.style[this.styles.fixed] = "100%";
  this.thumb.style[this.styles.variable] = this.minThumbSize + "px";
  
  var element = doc.createElement("div");
  this.thumb.appendChild(element);
  element.style.position = "absolute";
  element.style[this.styles.start] = "0px";
  element.style[this.styles.variable] = this.thumbStartLength + "px";
  element.style[this.styles.fixed] = "100%";
  element.style.backgroundImage = "url(" + this.thumbStartPath + ")";
  element.style.backgroundRepeat = "no-repeat";

  element = doc.createElement("div");
  this.thumb.appendChild(element);
  element.style.position = "absolute";
  element.style[this.styles.start] = this.thumbStartLength + "px";
  element.style[this.styles.end] = this.thumbEndLength + "px";
  element.style[this.styles.fixed] = "100%";
  element.style.backgroundImage = "url(" + this.thumbMiddlePath + ")";
  element.style.backgroundRepeat = this.styles.repeat;
  this.thumbMiddle = element;

  element = doc.createElement("div");
  this.thumb.appendChild(element);
  element.style.position = "absolute";
  element.style[this.styles.end] = "0px";
  element.style[this.styles.variable] = this.thumbEndLength + "px";
  element.style[this.styles.fixed] = "100%";
  element.style.backgroundImage = "url(" + this.thumbEndPath + ")";
  element.style.backgroundRepeat = "no-repeat";

  var _self = this;
  this.trackOnClick = function(event) {
    if (!event) {
      event = _self.track.ownerDocument.defaultView.event;
    }
    var pos = event[_self.styles.mousePos] - _self.track[_self.styles.offsetPos];
    var thumbPos = parseInt(_self.thumb.style[_self.styles.start]);
    //alert("Click " + pos + " " + thumbPos + " " + _self.jumpSize);
    if (pos < thumbPos) {
      _self.scrollBy(-_self.jumpSize);
      if (event.stopPropagation) {
        event.stopPropagation();
        event.preventDefault();
      }  
    }
    else if (pos > thumbPos + _self.thumb[_self.styles.scrollSize]) {
      _self.scrollBy(_self.jumpSize);      
      if (event.stopPropagation) {
        event.stopPropagation();
        event.preventDefault();
      }  
    }
  }
  //this.track.addEventListener("mousedown", this.trackOnClick, true);
  this.track.onmousedown = this.trackOnClick;

  this.thumb.onmousedown = function(event) {
    if (!event) {
      event = _self.track.ownerDocument.defaultView.event;
    }
    _self.thumbDragPos = event[_self.styles.mousePos];
    _self.thumbStartDragPos = parseInt(_self.thumb.style[_self.styles.start]);
    doc.addEventListener("mousemove", _self.thumbDrag, true);
    doc.addEventListener("mouseup", _self.thumbDragStop, true);
    doc.ondrag = function() {return false;};
    doc.onselectstart = function() {return false;};
    //document.addEventListener("mouseout", _self.thumbDragStop, true);
    if (event.stopPropagation) {
      event.stopPropagation();
      event.preventDefault();
    }  
  }
  
  this.thumbDrag = function(event) {
    if (!event) {
      event = _self.track.ownerDocument.defaultView.event;
    }
    var pos = event[_self.styles.mousePos];
    var y = _self.thumbStartDragPos + (pos - _self.thumbDragPos);
    _self.scrollTo(y);
    if (event.stopPropagation) {
      event.stopPropagation();
      event.preventDefault();
    }  
  }

  this.thumbDragStop = function(event) {
    if (!event) {
      event = _self.track.ownerDocument.defaultView.event;
    }
    doc.removeEventListener("mousemove", _self.thumbDrag, true);
    doc.removeEventListener("mouseup", _self.thumbDragStop, true);
    //document.removeEventListener("mouseout", _self.thumbDragStop, true);
    if (event.stopPropagation) {
      event.stopPropagation();
      event.preventDefault();
    }  
  }
}

HectorVerticalScrollbar.prototype.setScrollArea = function(scrollarea) {
  this.scrollarea = scrollarea;
  this.refresh();
}

HectorVerticalScrollbar.prototype.refresh = function() {
  var parentElement = this.scrollbar;
  this.track.posTop = parentElement.offsetTop;
  this.track.posLeft = parentElement.offsetLeft;
  while (parentElement.offsetParent != null) {
    parentElement = parentElement.offsetParent;
    this.track.posTop += parentElement.offsetTop;
    this.track.posLeft += parentElement.offsetLeft;
  }
  var swidth = this.scrollarea.content[this.styles.scrollSize];
  var vwidth = this.scrollarea[this.styles.viewSize];
  
  if (swidth <= vwidth) {
    if (this.autohide) {
      this.hide();  
    }
    this.thumb.style[this.styles.variable] = "100%";
  }
  else {
    this.show();  
    var sheight = this.track[this.styles.scrollSize] - this.minThumbSize;
    var theight = sheight * vwidth / swidth + this.minThumbSize;
    this.jumpSize = (theight - this.minThumbSize) * 3 / 4;
    if (theight < this.minThumbSize) {
      theight = this.minThumbSize;
    }
    this.thumb.style[this.styles.variable] = theight + "px";
    this.hasScrolled();
  }
}

HectorVerticalScrollbar.prototype.scrollTo = function(x) {
  var swidth = this.scrollarea.content[this.styles.scrollSize];
  var vwidth = this.scrollarea[this.styles.viewSize];
  var sheight = this.track[this.styles.scrollSize] - this.minThumbSize;

  var left = x * swidth / sheight;
  if (left < 0) {
    left = 0;
  }
  this.thumb.style[this.styles.start] = x + "px";
  this.scrollarea[this.styles.scroller](left);
}

HectorVerticalScrollbar.prototype.scrollBy = function(x) {
  var pos = parseInt(this.thumb.style[this.styles.start]);
  this.scrollTo(pos + x);
}

HectorVerticalScrollbar.prototype.verticalHasScrolled = function() {
  this.hasScrolled();  
}

HectorVerticalScrollbar.prototype.horizontalHasScrolled = function() {
}

HectorVerticalScrollbar.prototype.hasScrolled = function() {
  var swidth = this.scrollarea.content[this.styles.scrollSize];
  var sleft = this.scrollarea.content[this.styles.scrollPos];  
  var sheight = this.track[this.styles.scrollSize] - this.minThumbSize;

  var left = 0;
  if (swidth != 0) {
    left = sleft * sheight / swidth;
  }  
  this.thumb.style[this.styles.start] = left + "px";  
}

HectorVerticalScrollbar.prototype.remove = function() {
  this.scrollbar.removeChild(this.track);
}

HectorVerticalScrollbar.prototype.show = function() {
  this.track.style.display = "block";  
  this.hidden = false;
}

HectorVerticalScrollbar.prototype.hide = function() {
  this.track.style.display = "none";  
  this.hidden = true;
}

HectorVerticalScrollbar.prototype.setSize = function(size) {
  this.size = size;
  this.scrollbar.style[this.styles.fixed] = size + "px";
  this.refresh();  
}

HectorVerticalScrollbar.prototype.setTrackStart = function(path, size) {
  this.trackStartPath = path;
  this.trackStartLength = size;
  this.remove();
  this.setupLayout();
  this.refresh();
}

HectorVerticalScrollbar.prototype.setTrackMiddle = function(path) {
  this.trackMiddlePath = path;
  this.remove();
  this.setupLayout();
  this.refresh();    
}

HectorVerticalScrollbar.prototype.setTrackEnd = function(path, size) {
  this.trackEndPath = path;
  this.trackEndLength = size;
  this.remove();
  this.setupLayout();
  this.refresh();  
}

HectorVerticalScrollbar.prototype.setThumbStart = function(path, size) {
  this.thumbStartPath = path;
  this.thumbStartLength = size;
  this.remove();
  this.setupLayout();
  this.refresh();  
}

HectorVerticalScrollbar.prototype.setThumbMiddle = function(path) {
  this.thumbMiddlePath = path;
  this.remove();
  this.setupLayout();
  this.refresh();    
}

HectorVerticalScrollbar.prototype.setThumbEnd = function(path, size) {
  this.thumbEndPath = path;
  this.thumbEndLength = size;
  this.remove();
  this.setupLayout();
  this.refresh();  
}

HectorVerticalScrollbar.prototype.setupDefaults = function() {
  var path = getHectorPath() + "scripts/HectorClasses/Images/";  

	this.trackStartPath = path + "vscrollerTop.png";
	this.trackMiddlePath = path + "vscrollerMiddle.png";
	this.trackEndPath = path + "vscrollerBottom.png";
	this.thumbStartPath = path + "vthumbTop.png";
	this.thumbMiddlePath = path + "vthumbMiddle.png";
	this.thumbEndPath = path + "vthumbBottom.png";
	
	this.styles = {
	  start: "top",
	  end: "bottom",
	  fixed: "width",
	  variable: "height",
	  repeat: "repeat-y",
	  scrollSize: "scrollHeight",
	  viewSize: "viewHeight",
	  scrollPos: "scrollTop",
	  scroller: "verticalScrollTo",
	  mousePos: "clientY",
	  offsetPos: "posTop"
	}
}

HectorVerticalScrollbar.prototype.setAutohide = function(hide) {
  this.autohide = hide;
  this.refresh();
}

function HectorHorizontalScrollbar(scrollbar) {
  this.scrollbar = scrollbar;
  this.setupDefaults();
  this.setupLayout();
}

HectorHorizontalScrollbar.prototype = new HectorVerticalScrollbar(null);

HectorHorizontalScrollbar.prototype.setupDefaults = function() {
  var path = getHectorPath() + "scripts/HectorClasses/Images/";  

	this.trackStartPath = path + "hscrollerLeft.png";
	this.trackMiddlePath = path + "hscrollerMiddle.png";
	this.trackEndPath = path + "hscrollerRight.png";
	this.thumbStartPath = path + "hthumbLeft.png";
	this.thumbMiddlePath = path + "hthumbMiddle.png";
	this.thumbEndPath = path + "hthumbRight.png";
	
	this.styles = {
	  start: "left",
	  end: "right",
	  fixed: "height",
	  variable: "width",
	  repeat: "repeat-x",
	  scrollSize: "scrollWidth",
	  viewSize: "viewWidth",
	  scrollPos: "scrollLeft",
	  scroller: "horizontalScrollTo",
	  mousePos: "clientX",
	  offsetPos: "posLeft"
	}
}

HectorHorizontalScrollbar.prototype.verticalHasScrolled = function() {
}

HectorHorizontalScrollbar.prototype.horizontalHasScrolled = function() {
  this.hasScrolled();  
}

/*
 * Standard Scroller methods, redirected to HectorScrollbar
 */
function hectorScrollerInit (barDiv, trackDiv, thumbDiv) {
  //trackDiv.parentNode.removeChild(trackDiv);
  //thumbDiv.parentNode.removeChild(thumbDiv);
  //trackDiv.style.visibility = "hidden";
  //thumbDiv.style.visibility = "hidden";
  scrollBar = new HectorVerticalScrollbar(barDiv);
  barDiv.style.backgroundColor = "red";
}

function hectorCalculateAndShowThumb(contentDiv) {
  if (!window.scrollBar) {
    hectorScrollerInit(document.getElementById(SCROLLBAR_DIV_NAME));
  }
  if (!window.currentContent) {
    currentContent = new HectorScrollArea(contentDiv);
    currentContent.addScrollbar(scrollBar);
  	currentContent.scrollsHorizontally = false;
  }
  if (currentContent.content != contentDiv) {
    currentContent.content = contentDiv;
  }
  currentContent.refresh();
} 
