/*!
 * http://cmc3.cn/e
 * E-mail:59194618@qq.com
 * Custom Scrollbar Copyright 2010, caii 蔡美纯
 * Dual licensed under the MIT or GPL Version 2 licenses.
 */
(function($) {
	$.fn.wheel = function(fn) {
		return this[fn ? "bind": "trigger"]("wheel", fn)
	};
	var type = $.browser.mozilla ? "DOMMouseScroll": "mousewheel";
	$.event.special.wheel = {
		setup: function() {
			$.event.add(this, type, we, {})
		},
		teardown: function() {
			$.event.remove(this, type, we)
		}
	};
	function we(e) {
		e.delta = e.wheelDelta ? e.wheelDelta / 120 : -e.detail / 3;
		e.type = "wheel";
		return $.event.handle.call(this, e, e.delta)
	}
})(jQuery);
function scr(inits) {
	var _s = this,
	_c = inits;
	this.tiao = _c.tiao;
	this.quyu = _c.quyu;
	_s.tiao.style.visibility = 'hidden';
	var tiaoDiv = _s.tiao.getElementsByTagName("div");
	for (var i = 0; i < tiaoDiv.length; i++) {
		var _nm = tiaoDiv[i].className;
		if (_nm == "da_") _s._Ae = tiaoDiv[i];
		if (_nm == "dg_") _s._Dg = tiaoDiv[i]
	}
	var sdg = $(_s._Dg);
	sdg.unbind('mousedown');
	$(_s.quyu).unbind('scroll', scr.sfun);
	_s.quyu.scrollTop = 0;
	scr.f = 1;
	_s.rorig=_s.raito = _s.quyu.offsetHeight / _s.quyu.scrollHeight;
	_s.tiao.style.visibility = _s.raito < 1 ? 'visible': 'hidden';
	_s._sclDH = parseInt(_s.quyu.offsetHeight * _s.raito);
	_s._s_o = _s.quyu.scrollHeight - _s.quyu.offsetHeight;
	_s.step = 4;
	_s._oT = null;
	_s._minY = 0;
	_s._maxY = _s.quyu.offsetHeight - _s._sclDH;
	_s._Ae.style.height = _s.quyu.offsetHeight + "px";
	sdg.css({
		position: "absolute",
		width: _s.tiao.offsetWidth ,
		height: _s._sclDH 
	});
	if (scr.first) {
		sdg.css({
			"top": _s._minY 
		});
		_s._Dg.getElementsByTagName("div")[0].style.height = _s._sclDH + "px";
		scr.first = 0;
		scr.f = 0
	} else {
		sdg.animate({top: _s._minY},function() {scr.f = 0});
		$('div', _s._Dg).animate({height: _s._sclDH});
	}
	if (_s._Dg) {
		_s._Dg.onmouseover = function() {
			this.className = "dg_Over";
			scr.dm(this, {
				minY: _s._minY,
				maxY: _s._maxY
			},
			{
				down: _s.Startdrag,
				move: _s.draging,
				up: _s.Dragstop,
				obj: _s
			})
		}
		_s._Dg.onmouseout = function() {
			if (!_s._Ding) this.className = "dg_"
		}
	}
	scr.mfun = function(e) {
		_s.wheel.call(_s, e, e.delta)
	};
	scr.sfun = function(e) {
		_s.tiaoDong.call(_s, e)
	};
	$(_s.quyu).scroll(scr.sfun);
	$([_s.quyu, _s.tiao]).wheel(scr.mfun)
}
$.extend(scr, {
	f: 0,
	first: 1,
	dm: function(dObj, area, handler) {
		$(dObj).mousedown(function(e) {
			var _mY, minY = area.minY,
			maxY = area.maxY,
			_tY = dObj.offsetTop,
			_sY = e.pageY,
			doc = $(document);
			if (handler.down != null) handler.down.call(handler.obj);
			document.onselectstart = function() {
				return false
			};
			doc.mousemove(function(e) {
				_mY = _tY + e.pageY - _sY;
				_mY = Math.min(Math.max(minY, _mY), maxY);
				dObj.style.top = _mY + 'px';
				if (handler.move != null) handler.move.call(handler.obj, _mY)
			});
			doc.mouseup(function() {
				doc.unbind("mousemove mouseup");
				document.onselectstart = null;
				if (handler.up != null) handler.up.call(handler.obj)
			});
			return false
		})
	},
	prototype: {
		draging: function(y) {
			this.raito = y / this._maxY;
			this.quyu.scrollTop = parseInt(this._s_o * this.raito)
		},
		tiaoDong: function() {
			if (this._Ding || scr.f) return;
			this.raito = this.quyu.scrollTop / this._s_o;
			this._Dg.style.top = parseInt(this._maxY * this.raito) + "px"
		},
		Startdrag: function() {
			this._Ding = true
		},
		Dragstop: function() {
			this._Ding = false;
			this._Dg.className = "dg_"
		},
		wheel: function(e, dl) {
			this.quyu.scrollTop -= this.step *this.rorig*dl;
			return false
		}
	}
})
