var Ex = function(o) {
    for (var k in o) this[k] = o[k];
    return this
}
var UI = function(id) {
    return document.getElementById(id)
}
var UIs = function(tag) {
    return Ex.call([], this.getElementsByTagName(tag))
}
var Each = function(a, fn) {
    for (var i = 0; i < a.length; i++) fn.call(a[i], i, a)
}
var dhooo = function(ini) {
    this.bind(ini, this);
    this.autoIndex = 0;
    this.firstCli = 0;
};
Ex.call(dhooo.prototype, {
    bind: function(ini, me) {
        var dir = ini.dir == 'top' ? 'scrollTop': 'scrollLeft',
        pan = UI(ini.contentID), 
        cLis = UIs.call(pan, 'li');
        Each(cLis,
        function(i) {
            this.className = 'content-'+i;
        });
        var start = function(o) {
            Each(ini.btns,
            function() {
                this.className = ''
            });
            o.className = ini.className;
            me.autoIndex = o.index;
            me.begin(o.index, pan, ini.len, dir, cLis);
        };
        pan.onmouseover = function() {
            me.stop = true
        };
        Each(ini.btns,
        function(i) {
            this.index = i;
            this.onmouseover = function() {
                me.stop = true;
                start(this)
            };
            pan.onmouseout = this.onmouseout = function() {
                me.stop = false
            }
        });
        var auto = function() {
            if (!me.stop) {
                me.autoIndex = me.autoIndex == 7 ? 0 : ++me.autoIndex;	/* +1 */
              
                start(ini.btns[me.autoIndex]);
            }
        };
        if (ini.auto) this.autoPlay = window.setInterval(auto, 4000);
    },
    begin: function(i, o, len, dir, clis) { (function(me) {
            clearInterval(me.only);
            me.only = setInterval(function() {
                var diff = ((i+8-me.firstCli)%8 * len - o[dir]) * 0.05;	/* +1 */
                o[dir] += Math[diff > 0 ? 'ceil': 'floor'](diff);
                if (diff == 0) {
                  clearInterval(me.only);
                  while (me.firstCli!=i) {
                    clis[0].parentNode.appendChild(clis[me.firstCli]);
                    me.firstCli = (me.firstCli+1)%8;	/* +1 */
                    o[dir] -= len;
                  }
                }
            },
            10)
        })(this)
    }
})

new dhooo({
    btns: UIs.call(UI('ScrollBanner_btn'), 'li'),
    className: 'hot',
    contentID: 'ScrollBanner_pic',
    len: 500
    /* 500px width */
    ,
    auto: true
});

