//////////////////////////////
// ScrollBar class

ScrollBar = function(varName, id, contentId, upId, downId, barId, h) {
	this.scrolling = false;
	this.timeOutID = 0;
	this.prevMousePos = null;
	// public fields
	this.element = document.getElementById(id);
	this.varName = varName;
	this.speed = 10;
	this.content = document.getElementById(contentId);
	this.up = document.getElementById(upId);
	this.down = document.getElementById(downId);
	this.bar = document.getElementById(barId);
	this.height = h;
	this.step = 10;
	
	this.UP = this.varName + "." + "scrollUp()";
	this.DOWN = this.varName + "." + "scrollDown()";
	
	this.up.scrollPane = this;
	this.up.fn = this.UP;
	this.up.onmousedown = function() { this.scrollPane.startScroll(this.fn)	};
	this.up.onmouseup = function() { this.scrollPane.stopScroll() };
	this.up.onmouseout = function() { this.scrollPane.stopScroll() };

	this.down.scrollPane = this;
	this.down.fn = this.DOWN;
	this.down.onmousedown = function() { this.scrollPane.startScroll(this.fn)	};
	this.down.onmouseup = function() { this.scrollPane.stopScroll() };
	this.down.onmouseout = function() { this.scrollPane.stopScroll() };

	this.bar.scrollPane = this;
	this.bar.onmousedown = function(e) {
		var e = (e)?e:window.event;
		this.scrollPane.startMovingBar(e.clientY);
	};
	document.body.scrollPane = this;
	document.body.onmousemove = function(e) { 
		var leftBtn = false;
		var _event = null;
		if (e) {
			_event = e;
			leftBtn = (_event.which == 1);
		} else {
			_event = window.event; 
			leftBtn = (_event.button == 1);
		}
		if (leftBtn) { 
			this.scrollPane.movingBar(_event.clientY);
		}
	}

	document.body.onmouseup = function(e) { this.scrollPane.stopMovingBar() };
	this.init();
}

ScrollBar.prototype.init = function() {
	if (this.content.scrollHeight <= this.content.offsetHeight) 
		this.element.style.display = "none";
	else
		this.element.style.display = "block";
	if (this.content.scrollHeight != 0)		
		this.bar.style.height = (this.height * this.content.offsetHeight / this.content.scrollHeight) + "px";
	else
		this.bar.style.height = "1px";
	document.getElementById("BarContainer").style.height = (this.height+4) + "px";
	this.bar.style.top = 0;
	this.content.scrollTop = 0;
}
	
ScrollBar.prototype.scrollUp = function() {
	if (this.scrolling == true) {
		this.content.scrollTop -= this.step;
		this.bar.style.top = (this.height * this.content.scrollTop / this.content.scrollHeight) + "px";
	}
	if (this.content.scrollTop <= 0) 
		this.stopScroll();
}

ScrollBar.prototype.scrollDown = function() {
	if (this.scrolling == true) {
		this.content.scrollTop += this.step;
		this.bar.style.top = (this.height * this.content.scrollTop / this.content.scrollHeight) + "px";
	}
}

ScrollBar.prototype.startScroll = function(fn) {
	this.scrolling = true;
	this.timeOutID = window.setInterval(fn, this.speed);
}

ScrollBar.prototype.stopScroll = function() {
	if (this.scrolling == true) {
		this.scrolling = false;
		window.clearTimeout(this.timeOutID);
	}
}
	
ScrollBar.prototype.startMovingBar = function(mousePos) {
	this.scrolling = true;
	this.prevMousePos = mousePos;
}

ScrollBar.prototype.stopMovingBar = function() {
	this.scrolling = false;
}

ScrollBar.prototype.movingBar = function(mousePos) {
	if (this.scrolling == true) {

		var newPos = CSSUnitsToNumber(this.bar.style.top) + (mousePos - this.prevMousePos);
		if (newPos <= 0)
			newPos = 0;
		var barContainer = (this.bar.parentElement)?this.bar.parentElement:this.bar.parentNode;
		var containerH = CSSUnitsToNumber(document.getElementById("BarContainer").clientHeight);
		if ((newPos + this.bar.clientHeight) >= containerH - 1 )
			newPos = containerH  - this.bar.clientHeight - 2;

		this.bar.style.top = newPos + "px";
		this.content.scrollTop = CSSUnitsToNumber(this.bar.style.top) * this.content.scrollHeight / this.height;
		this.prevMousePos = mousePos;
	}
}

//////////////////////////////////////////////////
// NavigateBar

NavigateBar = function(name, prevId, nextId, max, listener) {
	this.name = name;
	this.position = 0;
	this.max = max;
	this.prevButton = prevId;
	this.nextButton = nextId;
	this.onChange = listener;

	this.next = function() {
		var pos = this.position;
		if (this.position < this.max) {
			pos++;
		}
		if (this.onChange != null) {
			this.onChange(pos);
		}
		this.position = pos;
		if (this.position == this.max) {
			this.nextButton.style.visibility = "hidden";
		} else {
			this.nextButton.style.visibility = "visible";
		}
		if (this.prevButton.style.visibility = "hidden") {
			this.prevButton.style.visibility = "visible";
		}
	};
	
	this.previous = function() {
		var pos = this.position;
		if (this.position > 0) {
			pos--;
		}
		if (this.onChange != null) {
			this.onChange(pos);
		}
		this.position = pos;
		if (this.position == 0) {
			this.prevButton.style.visibility = "hidden";
		} else {
			this.prevButton.style.visibility = "visible";
		}
		if (this.nextButton.style.visibility = "hidden") {
			this.nextButton.style.visibility = "visible";
		}		
	};
	
	this.init = function() {
		this.prevButton.style.visibility = "hidden";
		if (this.max <= 0) 
			this.nextButton.style.visibility = "hidden";
		else
			this.nextButton.style.visibility = "visible";
	};
	
	this.prevButton.navigateBar = this;
	this.nextButton.navigateBar = this;
	this.init();
}
