// JavaScript Document

document.write("<style type=\"text/css\">.scbBtn { position:absolute; left:0px; top:-5000px; z-index:50; background-color:#000000; width:15px; height:20px; cursor:pointer; }</style>");

var _scrollAll = new Array();
var scrollOn = null;

function scrollbar(objTarget,defaultX,defaultY,maxX,maxY,scrollDX,scrollDY,scrollMX,scrollMY,color) {
	initial();
	this.btn = document.createElement("div");
	this.plate = createFlyDiv(49);
	this.plate.style.left = "0px";
	this.plate.style.top = "-5000px";
	this.plate.style.width = fullX + "px";
	this.plate.style.height = fullY + "px";
	//this.plate.style.backgroundColor = "#ff0000";
	this.plate.style.cursor = "pointer";
	//setAlpha(this.plate,20);
	this.btn.className = "scbBtn";
	this.target = objTarget;
	this.target.dX = defaultX;
	this.target.dY = defaultY;
	this.target.mX = maxX;
	this.target.mY = maxY;
	this.target.nX = Math.abs(parseInt(this.target.style.marginLeft));
	this.target.nY = Math.abs(parseInt(this.target.style.marginTop));
	this.target.rX = this.target.mX - this.target.dX;
	this.target.rY = this.target.mY - this.target.dY;
	this.btn.obj = this;
	this.btn.dX = scrollDX;
	this.btn.dY = scrollDY;
	this.btn.mX = scrollMX;
	this.btn.mY = scrollMY;
	this.btn.rX = this.btn.mX - this.btn.dX;
	this.btn.rY = this.btn.mY - this.btn.dY;
	//setAlpha(this.btn,50);
	if (color != undefined) this.btn.style.backgroundColor = color;
	this.btn.style.left = this.btn.dX + "px";
	this.btn.style.top = this.btn.dY + "px";
	this.btn.nX = this.btn.dX;
	this.btn.nY = this.btn.dY;
	this.btn.onmousedown = btnClick;
	document.body.appendChild(this.btn);
	document.body.appendChild(this.plate);
	_scrollAll.push(this);
	this.nID = _scrollAll.length - 1;
}

function btnClick(e) {
	this.obj.plate.style.left = "0px";
	this.obj.plate.style.top = "0px";
	scrollOn = this.obj;
	document.body.onmouseup = bodyUp;
	this.obj.move = document.body.onmousemove;
	document.body.onmousemove = bodyMove;
}

function bodyMove(e) {
	if (document.all) {
		x = window.event.clientX;
		y = window.event.clientY;
	}
	else {
		x = e.pageX;
		y = e.pageY;
	}
	if (bIE) {
		x += document.body.scrollLeft;
		y += document.body.scrollTop;
	}
	if (x > scrollOn.btn.mX) x = scrollOn.btn.mX;
	else if (x < scrollOn.btn.dX) x = scrollOn.btn.dX;
	if (y > scrollOn.btn.mY) y = scrollOn.btn.mY;
	else if (y < scrollOn.btn.dY) y = scrollOn.btn.dY;
	if (scrollOn.btn.rX > 0) scrollOn.target.style.marginLeft = "-" + Math.round((x - scrollOn.btn.dX) / scrollOn.btn.rX * scrollOn.target.rX) + "px";
	if (scrollOn.btn.rY > 0) scrollOn.target.style.marginTop = "-" + Math.round((y - scrollOn.btn.dY) / scrollOn.btn.rY * scrollOn.target.rY) + "px";
	scrollOn.btn.style.left = x + "px";
	scrollOn.btn.style.top = y + "px";
}

scrollbar.prototype.setScroll = function(x,y) {
	if (x > this.btn.mX) x = this.btn.mX;
	else if (x < this.btn.dX) x = this.btn.dX;
	if (y > this.btn.mY) y = this.btn.mY;
	else if (y < this.btn.dY) y = this.btn.dY;
	if (this.btn.rX > 0) this.target.style.marginLeft = "-" + Math.round((x - this.btn.dX) / this.btn.rX * this.target.rX) + "px";
	if (this.btn.rY > 0) this.target.style.marginTop = "-" + Math.round((y - this.btn.dY) / this.btn.rY * this.target.rY) + "px";
	this.btn.style.left = x + "px";
	this.btn.style.top = y + "px";
}

scrollbar.prototype.setPos = function(x,y) {
	//mX = parseInt(this.target.style.marginLeft);
	//mY = parseInt(this.target.style.marginTop);
	this.target.nX = (0 - (x + this.target.dX));
	this.target.nY = (0 - (y + this.target.dY));
	this.target.style.marginLeft = this.target.nX + "px";
	this.target.style.marginTop = this.target.nY + "px";
	this.btn.nX = this.btn.dX + ((this.target.nX - this.target.dX) / this.target.rX * this.btn.rX);
	this.btn.nY = this.btn.dY + ((this.target.nY - this.target.dY) / this.target.rY * this.btn.rY);
	if (isNaN(this.btn.nX)) this.btn.nX = this.btn.dX;
	if (isNaN(this.btn.nY)) this.btn.nY = this.btn.dY;
	this.btn.style.left = this.btn.nX + "px";
	this.btn.style.top = this.btn.nY + "px";
}

scrollbar.prototype.setPosAnima = function(x,y,nTime) {
	d = new Date();
	this.target.nTimeNow = d.getTime();
	this.target.nTimeEnd = this.target.nTimeNow + nTime;
	this.target.nTimeUse = this.target.nTimeEnd - this.target.nTimeNow;
	this.target.nBeginX = parseInt(this.target.style.marginLeft);
	this.target.nBeginY = parseInt(this.target.style.marginTop);
	this.target.nDestX = 0 - x;
	this.target.nDestY = 0 - y;
	this.target.nRangeX = this.target.nDestX - this.target.nBeginX;
	this.target.nRangeY = this.target.nDestY - this.target.nBeginY;
	//alert(this.target.nBeginY + ":" + this.target.nRangeY);
	setTimeout("_scrollAll[" + this.nID + "].setPosAnima2()",30);
}

scrollbar.prototype.setPosAnima2 = function() {
	d = new Date();
	kt = d.getTime();
	ktx = Math.round((kt - this.target.nTimeNow) / this.target.nTimeUse * this.target.nRangeX);
	kty = Math.round((kt - this.target.nTimeNow) / this.target.nTimeUse * this.target.nRangeY);
	if (kt >= this.target.nTimeEnd) {
		this.target.nX = this.target.nDestX;
		this.target.nY = this.target.nDestY;
		this.target.style.marginLeft = this.target.nX + "px";
		this.target.style.marginTop = this.target.nY + "px";
		this.btn.nX = this.btn.dX - ((this.target.nX - this.target.dX) / this.target.rX * this.btn.rX);
		this.btn.nY = this.btn.dY - ((this.target.nY - this.target.dY) / this.target.rY * this.btn.rY);
		if (isNaN(this.btn.nX)) this.btn.nX = this.btn.dX;
		if (isNaN(this.btn.nY)) this.btn.nY = this.btn.dY;
		this.btn.style.left = this.btn.nX + "px";
		this.btn.style.top = this.btn.nY + "px";
		return;
	}
	this.target.nX = this.target.nBeginX + ktx;
	this.target.nY = this.target.nBeginY + kty;
	this.target.style.marginLeft = (this.target.nX) + "px";
	this.target.style.marginTop = (this.target.nY) + "px";
	setTimeout("_scrollAll[" + this.nID + "].setPosAnima2()",30);
}

function bodyUp(e) {
	document.body.onmousemove = null;
	if (scrollOn != null) {
		document.body.onmousemove = scrollOn.move;
		scrollOn.plate.style.top = "-5000px";
		scrollOn = null;
	}
}

scrollbar.prototype.clearMe = function() {
	if (this.btn != null) {
		document.body.removeChild(this.btn);
		this.btn = null;
	}
	if (this.plate != null) {
		document.body.removeChild(this.plate);
		this.plate = null;
	}
	_scrollAll.splice(this.nID,1);
}

