﻿$.tween = function(startProps, endProps, timeSeconds, animType, delay) {
    var tw = new Tween();
    tw.start(startProps, endProps, timeSeconds, animType, delay);
    return tw
}
function Tween() {
    this._frame = 20;
    this._startProps = [];
    this._currentProps = [];
    this._endProps = [];
    this._startTimer = 0;
    this._timeSeconds = 0;
    this._animType = "linear";
    this._delay = 0;
    this._runID = -1;
    this.run = function() {}
    this.complete = function() {}
}
Tween.prototype.start = function(startProps, endProps, timeSeconds, animType, delay) {
    if (animType != undefined) this._animType = animType;
    if (delay != undefined) this._delay = delay;
    this._timeSeconds = timeSeconds;
    this._startTimer = new Date().getTime();
    this._startProps = startProps;
    this._endProps = endProps;
    this._currentProps = [];
    var $this = this;
    this._runID = setInterval(function() {
        $this._run()
    },
    this._frame)
}
Tween.prototype.stop = function() {
    clearInterval(this._runID)
}
Tween.prototype._run = function() {
    if (new Date().getTime() - this._startTimer - (this._delay * 1000) < 0) return;
    var isEnd = false;
    for (var i in this._startProps) {
        this._currentProps[i] = this.getValue(this._startProps[i], this._endProps[i], this._startTimer, new Date().getTime() - (this._delay * 1000), this._startTimer + (this._timeSeconds * 1000), this._animType);
        if (this._startTimer + (this._timeSeconds * 1000) + (this._delay * 1000) <= new Date().getTime()) {
            this._currentProps[i] = this._endProps[i];
            isEnd = true
        }
    }
    this.run(this._currentProps);
    if (isEnd) {
        this.stop();
        this.complete(this._currentProps)
    }
}
Tween.prototype.getValue = function(_propStart, _propDest, _timeStart, _timeNow, _timeDest, _animType, _extra) {
    var t = _timeNow - _timeStart;
    var b = _propStart;
    var c = _propDest - _propStart;
    var d = _timeDest - _timeStart;
    var s, a, p;
    if (_extra == undefined) {
        _extra = new Object();
        _extra["e1"] = 1;
        _extra["e2"] = 0;
        a = _extra["e1"];
        p = _extra["e2"];
        s = _extra["e1"]
    }
    switch (_animType.toLowerCase()) {
    case Tween.linear:
        return c * t / d + b;
    case Tween.easeinquad:
        return c * (t /= d) * t + b;
    case Tween.easeoutquad:
        return - c * (t /= d) * (t - 2) + b;
    case Tween.easeinoutquad:
        if ((t /= d / 2) < 1) return c / 2 * t * t + b;
        return - c / 2 * ((--t) * (t - 2) - 1) + b;
    case Tween.easeincubic:
        return c * (t /= d) * t * t + b;
    case Tween.easeoutcubic:
        return c * ((t = t / d - 1) * t * t + 1) + b;
    case Tween.easeinoutcubic:
        if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
        return c / 2 * ((t -= 2) * t * t + 2) + b;
    case Tween.easeinquart:
        return c * (t /= d) * t * t * t + b;
    case Tween.easeoutquart:
        return - c * ((t = t / d - 1) * t * t * t - 1) + b;
    case Tween.easeinoutquart:
        if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
        return - c / 2 * ((t -= 2) * t * t * t - 2) + b;
    case Tween.easeinquint:
        return c * (t /= d) * t * t * t * t + b;
    case Tween.easeoutquint:
        return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
    case Tween.easeinoutquint:
        if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
        return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
    case Tween.easeinsine:
        return - c * Math.cos(t / d * (Math.PI / 2)) + c + b;
    case Tween.easeoutsine:
        return c * Math.sin(t / d * (Math.PI / 2)) + b;
    case Tween.easeinoutsine:
        return - c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
    case Tween.easeinexpo:
        return (t == 0) ? b: c * Math.pow(2, 10 * (t / d - 1)) + b;
    case Tween.easeoutexpo:
        return (t == d) ? b + c: c * ( - Math.pow(2, -10 * t / d) + 1) + b;
    case Tween.easeinoutexpo:
        if (t == 0) return b;
        if (t == d) return b + c;
        if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
        return c / 2 * ( - Math.pow(2, -10 * --t) + 2) + b;
    case Tween.easeincirc:
        return - c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
    case Tween.easeoutcirc:
        return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
    case Tween.easeinoutcirc:
        if ((t /= d / 2) < 1) return - c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
        return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
    case Tween.easeinelastic:
        if (t == 0) return b;
        if ((t /= d) == 1) return b + c;
        if (!p) p = d * .3;
        if (a < Math.abs(c)) {
            a = c;
            s = p / 4
        } else s = p / (2 * Math.PI) * Math.asin(c / a);
        return - (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
    case Tween.easeoutelastic:
        if (t == 0) return b;
        if ((t /= d) == 1) return b + c;
        if (!p) p = d * .3;
        if (a < Math.abs(c)) {
            a = c;
            s = p / 4
        } else s = p / (2 * Math.PI) * Math.asin(c / a);
        return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
    case Tween.easeinoutelastic:
        if (t == 0) return b;
        if ((t /= d / 2) == 2) return b + c;
        if (!p) p = d * (.3 * 1.5);
        if (a < Math.abs(c)) {
            a = c;
            s = p / 4
        } else s = p / (2 * Math.PI) * Math.asin(c / a);
        if (t < 1) return - .5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
        return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
    case Tween.easeinback:
        if (s == undefined) s = 1.70158;
        return c * (t /= d) * t * ((s + 1) * t - s) + b;
    case Tween.easeoutback:
        if (s == undefined) s = 1.70158;
        return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
    case Tween.easeinoutback:
        if (s == undefined) s = 1.70158;
        if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
        return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
    case Tween.easeinbounce:
        return c - getValue(0, c, 0, d - t, d, Tween.easeoutbounce) + b;
    case Tween.easeoutbounce:
        if ((t /= d) < (1 / 2.75)) {
            return c * (7.5625 * t * t) + b
        } else if (t < (2 / 2.75)) {
            return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b
        } else if (t < (2.5 / 2.75)) {
            return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b
        } else {
            return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b
        }
    case Tween.easeinoutbounce:
        if (t < d / 2) return getValue(0, c, 0, t * 2, d, Tween.easeinbounce) * .5 + b;
        return getValue(0, c, 0, t * 2 - d, d, Tween.easeoutbounce) * .5 + c * .5 + b
    }
    return c * t / d + b
}
Tween.linear = "linear";
Tween.easeinquad = "easeinquad";
Tween.easeoutquad = "easeoutquad";
Tween.easeinoutquad = "easeinoutquad";
Tween.easeincubic = "easeincubic";
Tween.easeoutcubic = "easeoutcubic";
Tween.easeinoutcubic = "easeinoutcubic";
Tween.easeinquart = "easeinquart";
Tween.easeoutquart = "easeoutquart";
Tween.easeinoutquart = "easeinoutquart";
Tween.easeinquint = "easeinquint";
Tween.easeoutquint = "easeoutquint";
Tween.easeinoutquint = "easeinoutquint";
Tween.easeinsine = "easeinsine";
Tween.easeoutsine = "easeoutsine";
Tween.easeinoutsine = "easeinoutsine";
Tween.easeinexpo = "easeinexpo";
Tween.easeoutexpo = "easeoutexpo";
Tween.easeinoutexpo = "easeinoutexpo";
Tween.easeincirc = "easeincirc";
Tween.easeoutcirc = "easeoutcirc";
Tween.easeinoutcirc = "easeinoutcirc";
Tween.easeinelastic = "easeinelastic";
Tween.easeoutelastic = "easeoutelastic";
Tween.easeinoutelastic = "easeinoutelastic";
Tween.easeinback = "easeinback";
Tween.easeoutback = "easeoutback";
Tween.easeinoutback = "easeinoutback";
Tween.easeinbounce = "easeinbounce";
Tween.easeoutbounce = "easeoutbounce";
Tween.easeinoutbounce = "easeinoutbounce";
function initPicPlayer(pics, w, h) {
    var selectedItem;
    var selectedBtn;
    var playID;
    var selectedIndex;
    var p = $('#picplayer');
    p.text('');
    p.append('<div id="piccontent"></div>');
    var c = $('#piccontent');
    for (var i = 0; i < pics.length; i++) {
        c.append('<a href="' + pics[i].link + '" target="_blank"><img id="picitem' + i + '" style="display: none;z-index:' + i + '" src="' + pics[i].url + '" /></a>')
    }
    p.append('<div id="picbtnHolder" style="position:absolute;top:' + (h - 25) + 'px;width:' + w + 'px;height:20px;z-index:100;"></div>');
    var btnHolder = $('#picbtnHolder');
    btnHolder.append('<div id="picbtns" style="float:right;padding-right:1px;"></div>');
    var btns = $('#picbtns');
    for (var i = 0; i < pics.length; i++) {
        btns.append('<span id="picbtn' + i + '" style="cursor:pointer;font-family:Verdana;border:solid 1px #ccc;background-color:#eee;display:inline-block;height:18px;line-height:18px;">&nbsp;' + (i + 1) + '&nbsp;</span>&nbsp;');
        $('#picbtn' + i).data('index', i);
        $('#picbtn' + i).click(function(event) {
            if (selectedItem.attr('src') == $('#picitem' + $(this).data('index')).attr('src')) {
                return
            }
            setSelectedItem($(this).data('index'))
        })
    }
    btns.append('&nbsp;');
    setSelectedItem(0);
    function setSelectedItem(index) {
        selectedIndex = index;
        clearInterval(playID);
        if (selectedItem) selectedItem.fadeOut('fast');
        selectedItem = $('#picitem' + index);
        selectedItem.fadeIn('slow');
        if (selectedBtn) {
            selectedBtn.css('backgroundColor', '#eee');
            selectedBtn.css('color', '#000')
        }
        selectedBtn = $('#picbtn' + index);
        selectedBtn.css('backgroundColor', '#000');
        selectedBtn.css('color', '#fff');
        playID = setInterval(function() {
            var index = selectedIndex + 1;
            if (index > pics.length - 1) index = 0;
            setSelectedItem(index)
        },
        pics[index].time)
    }
}

function bbimg(o) {
    var zoom = parseInt(o.style.zoom, 10) || 100;
    zoom += event.wheelDelta / 12;
    if (zoom > 0) o.style.zoom = zoom + '%';
    return false
}
function imginit(o) {
    if (o.width > screen.width - screen.width / 2) o.style.width = screen.width - screen.width / 2
}
function fullview(o) {
    window.open(o.src)
}
