﻿var VScroll = Scroll.extend({
	constructor: function(scrollbarbg, scrollbar){
		this.scrollbarbg = scrollbarbg;
		this.scrollbar = scrollbar;
		this.init();
	}
});

function Scroll(scrollbarbg, scrollbar){
	// Properties
	// true 为固定滚动条长度， false 为根据滚动范围自动计算
	this.barChangeless	= false;
	this.areaPadding = 0;
	this.scrollbarbg = scrollbarbg;
	this.scrollbar = scrollbar;

	// Events
	// 滚动回调函数
	this.OnScroll		= function(nPos){};		
	this.OnScrollEnd	= function(nPos){};		

	// Methods
	this.ScrollTo = function(pos, type){
		switch(type){
			case 2:
				pos = this.l2p(pos);
		}
		pos = pos.limit(this._nMin, this._nMax);
		this.SetScrollInfo({fMask:4, nPos:pos});
		this.OnScroll.call(this, pos);
	};

	this.SetScrollInfo = function(si){
		if((si.fMask & 1) == 1){
			this._nMin = si.nMin;
			this._nMax = si.nMax;
			_nPage = si.nPage;
			if(!this.barChangeless)
				scrollbar.setStyle('height', scrollbarbg.offsetHeight * _nPage / (this._nMax - this._nMin) );
		}
		if((si.fMask & 4) == 4){
			this._nPos = si.nPos.limit(0, this._nMax);
			scrollbar.setStyle('top', this.areaPadding + (scrollbarbg.offsetHeight - scrollbar.offsetHeight - 2*this.areaPadding) * (this._nPos - this._nMin) / (this._nMax - this._nMin) );
		}
		
	};

	this.init = function(){
		scrollbarbg = this.scrollbarbg;
		scrollbar = this.scrollbar;

		bd_mm = dMouseMove.bindWithEvent(this);
		bd_mu = dMouseUp.bindWithEvent(this);

		scrollbarbg.addEvent('mousedown', function(event){
			if(document.body.setCapture)document.body.setCapture();
			document.body.style.cursor = 'default';
			var pOfst = scrollbarbg.getPosition();
			di.offset = {y:pOfst.y};
			if(event.page.y < pOfst.y + scrollbar.offsetTop)
				this.ScrollTo(event.page.y - pOfst.y, 2);// 跳转
			else if(event.page.y > pOfst.y + scrollbar.offsetTop + scrollbar.offsetHeight){
				this.ScrollTo(event.page.y - pOfst.y - scrollbar.offsetHeight, 2);// 跳转
				di.offset.y += scrollbar.offsetHeight;
			}else if(event.page.y >= pOfst.y + scrollbar.offsetTop)
				di.offset.y = event.page.y - scrollbar.offsetTop;

			document.addListener('mousemove', bd_mm);
			document.addListener('mouseup', bd_mu);
			event.preventDefault();
		}.bindWithEvent(this));
	}

	// private
	// Fields
	var scrollbarbg;
	var scrollbar;
	var _nPage;
	this._nMin = this._nMax = this._nPos = 0;
	var di = {};
	var thisClass;
	var bd_mm;
	var bd_mu;
	// init
	//init();

	
	function dMouseMove(event){
		if(this.fx)this.fx.stop();
		this._nPos = this.l2p( (event.page.y - di.offset.y).limit(0+this.areaPadding, scrollbarbg.offsetHeight - scrollbar.offsetHeight - this.areaPadding) );
		this.SetScrollInfo({fMask:4, nPos:this._nPos});
		this.OnScroll.call(this, this._nPos);
	}
	
	function dMouseUp(){
		this.OnScrollEnd.call(this, this._nPos);

		if(document.releaseCapture)	document.releaseCapture();
		document.removeListener('mousemove', bd_mm);
		document.removeListener('mouseup', bd_mu);
	}
	
	this.l2p = function(left){
		return (this._nMax - this._nMin) * (left-this.areaPadding) / (scrollbarbg.offsetHeight - scrollbar.offsetHeight - 2*this.areaPadding) + this._nMin;
	}

}

