﻿/*animation-2.0.js 2011-7-27 */
(function (window, undefined) {
    window.$animation = {};
    $animation.load = function (o, c, ap) { if (o.length && !o.nodeName) { for (var i in o) { ap[0] = o[i]; c.apply(null, ap); } return 1; } else { return 0; } };
    $animation.extend = function (destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; };
    $animation.bind = function (object, fun) { var args = Array.prototype.slice.call(arguments).slice(2); return function () { return fun.apply(object, args.concat(Array.prototype.slice.call(arguments))); } };
    $animation.gallery = function (id, options) {
        this.SetOptions = function (options) {
            this.options = { Vertical: false, /*是否垂直方向*/Auto: true, /*是否自动*/Change: 0, /*改变量*/Duration: 20, /*滑动持续时间*/Time: 20, /*滑动延时*/Pause: 2000, /*停顿时间(Auto为true时有效)*/onStart: function () { }, /*开始转换时执行*/onFinish: function () { } /*完成转换时执行*/ };
            $animation.extend(this.options, options || {});
        };
        this.Run = function (index) {
            index == undefined && (index = this.Index);
            index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);
            this._target = -Math.abs(this.Change) * (this.Index = index);
            this._t = 0;
            this._b = parseInt(this._slider.style[this.options.Vertical ? "top" : "left"]);
            this._c = this._target - this._b;
            this.onStart();
            this.Move();
        };
        this.Move = function () {
            clearTimeout(this._timer);
            if (this._c && this._t < this.Duration) {
                this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));
                this._timer = setTimeout($animation.bind(this, this.Move), this.Time);
            } else {
                this.MoveTo(this._target);
                this.Auto && (this._timer = setTimeout($animation.bind(this, this.Next), this.Pause));
            }
        };
        var box = $$(id); count = $$.by(box, 'a').length;
        this.Next = function () { this.Run(++this.Index); };
        this.Previous = function () { this.Run(--this.Index); };
        this.Stop = function () { clearTimeout(this._timer); this.MoveTo(this._target); };
        this._timer = null;
        this._count = Math.abs(count);
        this._target = 0;
        this._t = this._b = this._c = 0;
        this.Index = 0;
        this.SetOptions(options);
        this.Auto = !!this.options.Auto;
        this.Duration = Math.abs(this.options.Duration);
        this.Time = Math.abs(this.options.Time);
        this.Pause = Math.abs(this.options.Pause);
        this.Tween = function (t, b, c, d) { return -c * ((t = t / d - 1) * t * t * t - 1) + b; };
        this.onFinish = this.options.onFinish;
        var bVertical = !!this.options.Vertical;
        this._css = bVertical ? "top" : "left";
        function mover() { var o = this.o; o.Atuo = false; o.Run(this.i); }
        function mout() { var o = this.o; o.Atuo = true; o.Run(); }
        var a, idNum = $$.el('div'), bi, wh = $$.pos.wh(box);
        idNum.className = 'galleryNum';
        box.innerHTML = '<div style="overflow:hidden;position:absolute;width:' + wh.w * count + 'px;height:' + wh.h + 'px;">' + box.innerHTML + '</div>';
        for (var i = 0; i != count; ) {
            a = idNum.appendChild($$.el("a")); a.i = i; a.o = this; a.onmouseover = mover; a.onmouseout = mout; a.innerHTML = ++i;
        }
        bi = box.childNodes[0];
        box.appendChild(idNum);
        this.Change = this.options.Change ? this.options.Change : bi[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;
        this._container = box;
        this._slider = bi;
        this.onStart = function () { for (var i = 0; i != count; i++) { idNum.childNodes[i].className = this.Index == i ? "on" : ""; } };
        this.MoveTo = function (i) { this._slider.style[this._css] = i + "px"; };
        this.Run();
    };
    $animation.select = function (lab, div) {
        var _t = lab.web = this;
        this.visible = function () { div.style.visibility = 'visible'; };
        this.hidden = function () { div.style.visibility = 'hidden'; };
        this.click = function () { lab.innerHTML = this.innerHTML; _t.hidden(); };

        var i, n, arr = div.innerHTML.split(','), d = [];
        for (i = 0; n = arr[i++]; ) { d.push('<a>' + n + '</a>'); }
        div.innerHTML = d.join('');
        div.onmouseover = lab.onmouseover = this.visible;
        div.onmouseout = lab.onmouseout = this.hidden;
        arr = $$.by(div, 'a');
        for (i = 0; n = arr[i++]; ) { n.onclick = this.click; }
    };
    $animation.scroll = function (id, par) {
        !par && (par = {});
        var sptime = par.time || 50, oldsptime = sptime;
        var box = $$(id);
        $$.styles(box, 'position:relative;overflow:hidden;');
        var pos = $$.pos;
        var w = pos.wh(box).w, txt = $$.text.get(box), con = $$.styles($$.el('div', $$.text(box, '')), 'position:absolute;left:0;top:0;width:auto;'), w2 = 0;
        var k = 0, l0 = 0, skip = 1, conw, ccw, htag = {};
        txt = '<div style="float:left;">' + txt + '</div>';
        while (w2 < w) {
            $$.text.append(con, txt); !conw && (conw = pos.wh(con).w); w2 += conw;
            if (k === 0) { $$.each($$.by(con, 'a'), function (i, a) { if (!a.name) { return; } htag[a.name.toLowerCase()] = $$.pos.offset(a, con).x; }); }
            k++;
            if (k > 100) { break; }
        }
        ccw = k * conw;
        for (var i = 0, j = 2 * k; i < j; i++) { $$.text.append(con, txt); k++; }
        $$.styles(con, 'width:' + (k + 2) * conw + 'px;');
        var left = 0, outleft, outright, outt1, outt2, mr = k * conw, l0 = -k * conw, right = -(mr - w), r0 = -(mr - ccw * 2), mmr = right + 20, curdir = 0;
        for (var i = 0; i < 2; i++) { $$.text.append(con, txt); }
        var toleft = function () {
            if (this.isdir === curdir) { return; }
            outright && (clearTimeout(outright));
            left < l0 && (left = -1);
            left -= skip;
            con.style.left = left + 'px';
            outleft = setTimeout(toleft, sptime);
            curdir = 1;
            right = left + 1;
        }, toright = function () {
            if (this.isdir === curdir) { return; }
            outleft && (clearTimeout(outleft));
            right > r0 && (right = mmr);
            right += skip;
            con.style.left = right + 'px';
            outright = setTimeout(toright, sptime);
            curdir = 2;
            left = right - 1;
        }, tosptime = function () {
            clearTimeout(outt1); clearTimeout(outt2);
            sptime -= 10;
            skip = 10;
            if (sptime > 4) { outt1 = setTimeout(tosptime, 10); } else { tosptime2(); }
        }, tosptime2 = function () {
            if (sptime++ < oldsptime) { outt2 = setTimeout(tosptime2, 50); skip > 1 && (skip--); }
        }, run = function () {
            switch (curdir || par.direction) { case 'stop': break; case 1: case 'left': case undefined: toleft(); break; case 2: case 'right': toright(); }
        };
        box.onmouseover = this.stop = function () { curdir = 0; clearTimeout(outleft); clearTimeout(outright); };
        box.onmouseout = this.start = run;
        box.to = this.to = function (o) {
            o = o && o.nodeName ? o : this;
            var x = htag[o.innerHTML.toLowerCase()];
            if (x) { x = left = right = -(x); con.style.left = x + 'px'; box.onmouseover(); }
        };
        if (par.mouse) {
            var m = par.mouse[0];
            if (m) { m.isdir = 1; m.onmouseover = toleft; m.onclick = tosptime; }
            m = par.mouse[1];
            if (m) { m.isdir = 2; m.onmouseover = toright; m.onclick = tosptime; }
        }
        run();
    };
})(window);
