/*******************************************************************
	Dynamic ScrollBar Version 1.0
	
	©2009 A+R Media Studio
	http://www.AandRmediastudio.com
	
	written by Rudy Dominguez (rudy@AandRmediastudio.com)
	
	These scripts are free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
*******************************************************************/
dsb = new DivScrollBar();
function DivScrollBar(containerID, contentID, barBGDivID, barBtnDivID) {
	this.bilh = new BILH();

	
	this.startX = 0;
	this.startY = 0;
	this.mX = 0;
	this.mY = 0;
	
	this.moving = false;
	this.timerID = null;
}

DivScrollBar.prototype.init = function (containerID, contentID, barBGDivID, barBtnDivID) {
	this.bilh.register(containerID, contentID, barBGDivID, barBtnDivID);

	this.divContainer = this.bilh.refs[containerID];
	this.divContent = this.bilh.refs[contentID];
	this.divBarBG = this.bilh.refs[barBGDivID];
	this.divBarButton = this.bilh.refs[barBtnDivID];

	this.divBarButton.layer.onmousedown = DivScrollBarMouseDownFunction;
	this.divBarBG.layer.onmousedown = DivScrollBarBGMouseDownFunction;

	document.onmousemove = DivScrollBarMouseMoveFunction;
	document.onmouseup = DivScrollBarMouseUpFunction;
	
	this.OffClassName = this.divBarButton.layer.className;
	this.OnClassName = this.divBarButton.layer.className + "on";
	
	this.reset();
	DivScrollBarMouseUpFunction();
}

DivScrollBar.prototype.setContentWidth = function (newWidth){
	this.divContent.style.width = newWidth + "px";
	this.reset();
}

DivScrollBar.prototype.reset = function (){
	if (this.divContent.layer.offsetWidth > this.divContainer.layer.offsetWidth) {
		this.divBarButton.style.width = ((this.divContainer.layer.offsetWidth / this.divContent.layer.offsetWidth) * this.divBarBG.layer.offsetWidth) + "px";//alert()
	
		this.bilh.show(this.divBarBG.name, true);
	} else {
		this.bilh.hide(this.divBarBG.name, true);	
	}
}

DivScrollBar.prototype.jumpScrollBar = function (toValue) {
	var actualX = dsb.divBarButton.layer.offsetLeft, obj = dsb.divBarButton.layer;
	while ((obj.tagName.toLowerCase() != "body") && (obj.tagName.toLowerCase() != "html")) {
		obj = obj["offsetParent"];
		
		actualX += obj.offsetLeft;
	}

	if (this.mX < actualX) {
		this.divBarButton.style.left = (this.divBarButton.layer.offsetLeft - (this.divContainer.layer.offsetWidth*0.10)) + "px";
	} else if (this.mX > actualX + this.divBarButton.layer.offsetWidth) {
		this.divBarButton.style.left = (this.divBarButton.layer.offsetLeft + (this.divContainer.layer.offsetWidth*0.10)) + "px";
	} else {
		clearInterval(this.timerID);
	}
	
	if (this.divBarButton.layer.offsetLeft < 0) {
		this.divBarButton.style.left = 0 + "px";
	} else if (this.divBarButton.layer.offsetLeft + this.divBarButton.layer.offsetWidth > this.divBarBG.layer.offsetWidth) {
		this.divBarButton.style.left = (this.divBarBG.layer.offsetWidth - this.divBarButton.layer.offsetWidth) + "px";
	}
	
	this.divContent.style.left = (-this.divBarButton.layer.offsetLeft) + "px";
}

//Event Handler Functions
function DivScrollBarBGMouseDownFunction(e) {
	e = (typeof(event) != "undefined")? event : e;

	//dsb.divBarButton.layer.className = dsb.OnClassName;
	dsb.mX = e.clientX;
	dsb.mY = e.clientY;

	dsb.startX = dsb.divBarButton.layer.offsetLeft;
	dsb.startY = dsb.divBarButton.layer.offsetTop;

	document.onSelectStart = function() {
		return false;
	}

	dsb.timerID = setInterval("dsb.jumpScrollBar()", 100);
	dsb.jumpScrollBar();
	
}

function DivScrollBarMouseDownFunction(e) {
	e = (typeof(event) != "undefined")? event : e;

	dsb.divBarButton.layer.className = dsb.OnClassName;
	
	dsb.mX = e.clientX;
	dsb.mY = e.clientY;
	
	dsb.startX = dsb.divBarButton.layer.offsetLeft;
	dsb.startY = dsb.divBarButton.layer.offsetTop;

	dsb.moving = true;
	
	document.onSelectStart = function() {
		return false;
	}
}

function DivScrollBarMouseMoveFunction(e) {
	if (!dsb.moving) return;
	
	e = (typeof(event) != "undefined")? event : e;

	var deltaX = e.clientX - dsb.mX;
	var nx = dsb.startX + deltaX;
	
	nx = (nx < 0)? 0 : ((nx + dsb.divBarButton.layer.offsetWidth > dsb.divBarBG.layer.offsetWidth)? dsb.divBarBG.layer.offsetWidth - dsb.divBarButton.layer.offsetWidth : nx);
	dsb.divBarButton.style.left =  nx + "px";
	dsb.divContent.style.left = (-nx / (dsb.divBarBG.layer.offsetWidth - dsb.divBarButton.layer.offsetWidth) * (dsb.divContent.layer.offsetWidth - dsb.divContainer.layer.offsetWidth)) + "px";
}

function DivScrollBarMouseUpFunction(e) {
	dsb.divBarButton.layer.className = dsb.OffClassName;
	dsb.moving = false;

	clearInterval(dsb.timerID);
	
	document.onSelectStart = null;
}
