
var kp_iToggle = function() {
    var _this = this,
		_direct = 'crt';
    this.n = 0;
    this.direct = function(s) {
        clearTimeout(_this._auto);
        if (typeof s === 'string') {
            var gid = document.getElementById(this.ids[2]);
            if (s == 'lvl') {
                gid.className = this.iCls;
            } else {
                gid.className = '';
                s = 'crt';
            }
            _direct = s;
        }
        if (this.isAuto) { this.auto(); }
        return _direct;
    };
    this.isAuto = false;
    this.init = function(o) {
        if (o.boxId && o.numId && o.imgId) {
            this.ids = [o.boxId, o.numId, o.imgId];
        } else {
            alert('È±ÉÙID'); return;
        }
        this.isAuto = !!o.isAuto;
        this.aCls = '' + o.aCls;
        this.iCls = '' + o.iCls;
        this.evtNum();
        this.direct(o.direct);
    }
    this.evtNum = function() {
        var ls = document.getElementById(_this.ids[1]).getElementsByTagName('li'),
					l = ls.length;
        _this.numLen = l;
        for (var i = 0; i < l; i++) {
            ls[i].onclick = function() {
                ls[_this.n].className = '';
                this.className = _this.aCls;
                _this.n = this.id.replace('numli', '') * 1 - 1; //this.innerHTML.replace(/[^\d]/g, '') * 1 - 1;
                clearTimeout(_this._auto);
                _this.t_isAuto = _this.isAuto;
                _this.isAuto = false;
                _this.run();
                
                _this.isAuto = _this.t_isAuto;
                _this.auto();
            }
//            ls[i].onmouseout = function() {
//                _this.isAuto = _this.t_isAuto;
//                _this.auto();
//            }
        }
    }
    this.Tween = function(t, b, c, d) {
        return c * ((t = t / d - 1) * t * t + 1) + b;
    }
    this.auto = function() {
        if (_this.isAuto) {
            clearTimeout(_this._auto);
            _this._auto = setTimeout(function() {
                var o = document.getElementById(_this.ids[1]).getElementsByTagName('li');
                o[_this.n].className = '';
                if (_this.n < _this.numLen - 1) { _this.n++; } else { _this.n = 0; }
                o[_this.n].className = _this.aCls;
                _this.run();
            }, 5000);
        }
    }
    this.run = function() {
        var _demo = document.getElementById(_this.ids[0]),
				sn = _direct == 'crt' ? _demo.scrollTop : _demo.scrollLeft,
				sl = _direct == 'crt' ? _demo.scrollHeight : _demo.scrollWidth,
				sc = parseInt(sl * 1 * _this.n / _this.numLen) - sn,
				t = 0, b = sn, c = sc, d = 60;
        clearTimeout(_this._t);
        (function _move() {
            if (t < d) {
                t++;
                if (_direct == 'crt') {
                    _demo.scrollTop = _this.Tween(t, b, c, d);
                } else {
                    _demo.scrollLeft = _this.Tween(t, b, c, d);
                }
                _this._t = setTimeout(_move, 5);
            } else {
                _this.auto();
            }
        })();
    }
}
