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

function HectorScrollArea(content)
{
	this.content = content;
	this.content.object = this;
	
	this.scrollsVertically = true;
	this.scrollsHorizontally = true;
	this.singlepressScrollPixels = 10;
	
	this.viewHeight = 0;
	this.viewToContentHeightRatio = 1.0;
	this.viewWidth = 0;
	this.viewToContentWidthRatio = 1.0;
	
	this.content.style.overflow = "hidden";
	this.content.scrollTop = 0;
	this.content.scrollLeft = 0;

	this.sbars = new Array();
  
  this.refresh();
  
  for (var i = 1; i < arguments.length; i++) {
  	this.addScrollbar(arguments[i]);
  }
  
}

HectorScrollArea.prototype.addScrollbar = function(scrollbar) {
  scrollbar.setScrollArea(this);
  this.sbars.push(scrollbar);
  scrollbar.refresh();
}

HectorScrollArea.prototype.removeScrollbar = function(scrollbar) {
  for (var i = 0; i < scollbars.length; i++) {
  	if (this.sbars[i] == scrollbar) {
  	  scrollbar.setScrollArea(null);
  	  this.sbars.slice(i);
  	  break;
  	}
  }
}

HectorScrollArea.prototype.remove = function() {
  for (var i = 0; i < scollbars.length; i++) {
  	this.sbars[i].setScrollArea(null);
  }	
}

HectorScrollArea.prototype.refresh = function() {	
  if (myBrowser.isIE) {
	  this.viewWidth  = this.content.offsetWidth;	  
	  this.viewHeight = this.content.offsetHeight;
	}
	else {
	  var style = this.content.ownerDocument.defaultView.getComputedStyle(this.content, null);
	  if (style) {
	    this.viewWidth  = parseInt(style.width);	  
	    this.viewHeight = parseInt(style.height);	    
	  }  
	  else {
	    this.viewWidth = 0;
	    this.viewHeight = 0;
	  }
	}  
	this.viewToContentWidthRatio = (this.viewWidth < this.content.scrollWidth) ? 
	  this.viewWidth / parseFloat(this.content.scrollWidth) : 1.0;
	this.viewToContentHeightRatio = (this.viewHeight < this.content.scrollHeight) ? 
	  this.viewHeight / parseFloat(this.content.scrollHeight) : 1.0;	

  if (this.viewToContentWidthRatio == 1.0) {
    this.horizontalScrollTo(0);
  }
  if (this.viewToContentHeightRatio == 1.0) {
    this.verticalScrollTo(0);
  }

  for (var i = 0; i < this.sbars.length; i++) {
  	this.sbars[i].refresh();
  }
}

/*
 * focus() member function.
 * Tell the scrollarea that it is in focus. It will capture keyPressed events
 * and if they are arrow keys scroll accordingly.
 */
HectorScrollArea.prototype.focus = function() {
  this.content.onkeypressed = this.keyPressed;
}

/*
 * blur() member function.
 * Tell the scrollarea that it is no longer in focus. It will cease capturing
 * keypress events.
 */
HectorScrollArea.prototype.blur = function() {
  this.content.onkeypressed = null;
}


HectorScrollArea.prototype.reveal = function(element) {
  var y = 0;
  var eParent = element;
  do {
    y += eParent.offsetTop;
    eParent = eParent.offsetParent;
  }
  while (eParent != this.content && eParent);
  this.verticalScrollTo(y);
  
  var x = 0;
  var eParent = element;
  do {
    x += eParent.offsetTop;
    eParent = eParent.offsetParent;
  }
  while (eParent != this.content && eParent);
  this.horizontalScrollTo(x);
}


HectorScrollArea.prototype.verticalScrollTo = function(y) {
  if (this.scrollsVertically) {
    var max = this.content.scrollHeight - this.viewHeight;
    y = Math.min(y, max);
    y = Math.max(y, 0);
    this.content.scrollTop = y;
    for (var i = 0; i < this.sbars.length; i++) {      
    	this.sbars[i].verticalHasScrolled();
    }
  }
}

HectorScrollArea.prototype.horizontalScrollTo = function(x) {
  if (this.scrollsHorizontally) {
    var max = this.content.scrollWidth - this.viewWidth;
    x = Math.min(x, max);
    x = Math.max(x, 0);
    this.content.scrollLeft = x;
    for (var i = 0; i < this.sbars.length; i++) {      
    	this.sbars[i].horizontalHasScrolled();
    }
  }
}

/*********************
 * Keypressed events
 */
HectorScrollArea.prototype.keyPressed = function(event)
{
	if (event.altKey)
		return;
	if (event.shiftKey)
		return;
	
	switch (event.keyCode) {
		case "Home":
			this.verticalScrollTo(0);
			break;
		case "End":
			this.verticalScrollTo(this.content.scrollHeight - this.viewHeight);
			break;
		case "Up":
			this.verticalScrollTo(this.content.scrollTop - this.singlepressScrollPixels);
			break;
		case "Down":
			this.verticalScrollTo(this.content.scrollTop + this.singlepressScrollPixels);
			break;
		case "PageUp":
			this.verticalScrollTo(this.content.scrollTop - this.viewHeight);
			break;
		case "PageDown":
			this.verticalScrollTo(this.content.scrollTop + this.viewHeight);
			break;
		case "Left":
			this.horizontalScrollTo(this.content.scrollLeft - this.singlepressScrollPixels);
			break;
		case "Right":
			this.horizontalScrollTo(this.content.scrollLeft + this.singlepressScrollPixels);
			break;
		default:
			return;
	}
	event.stopPropagation();
	event.preventDefault();
}

HectorScrollArea.prototype.mousewheelScroll = function(event) {
  //not supported
}

HectorScrollArea.prototype._mousewheelScrollHandler = function() {};


