(function ($) {
    $.fn.PlayScroller = function () {
        this.children().stop();
        this.children().trigger('marquee', ['resume'])
    };
    $.fn.PauseScroller = function () {
        this.children().stop()
    };
    $.fn.ResetScroller = function (a) {
        this.children().stop();
        this.children().unbind('marquee');
        a = $.extend(this.data(), a);
        this.SetScroller(a)
    };
    $.fn.RemoveScroller = function () {
        this.children().stop();
        this.children().css({
            top: 0,
            left: 0
        });
        this.children().unbind('marquee')
    };
    $.fn.SetScroller = function (h) {
        var _ = {
            me: this
        };
        h = $.extend({
            velocity: 50,
            direction: 'horizontal',
            startfrom: 'left',
            loop: 'infinite',
            movetype: 'linear',
            onmouseover: 'pause',
            onmouseout: 'play',
            onstartup: 'play',
            cursor: 'pointer'
        }, h);
        var i = typeof h.velocity == 'number' ? parseInt(h.velocity) : 50;
        var j = h.direction.toLowerCase();
        var k = h.startfrom.toLowerCase();
        var l = typeof h.loop == 'number' && h.loop > 0 ? parseInt(h.loop) : -1;
        var m = h.movetype.toLowerCase();
        var n = h.onmouseover.toLowerCase();
        var o = h.onmouseout.toLowerCase();
        var p = h.onstartup.toLowerCase();
        var q = h.cursor.toLowerCase();
        _.me.data(h);
        main();

        function main() {
            _.me.css('cursor', q);
            if (j == 'horizontal') {
                if (k != 'right' && k != 'left') {
                    k = 'left'
                };
                scrollHorizontal()
            } else {
                if (k != 'bottom' && k != 'top') {
                    k = 'bottom'
                };
                scrollVertical()
            };
            if (n == 'play') {
                _.me.mouseover(function () {
                    $(this).children().stop();
                    $(this).children().trigger('marquee', ['resume'])
                })
            } else {
                _.me.mouseover(function () {
                    $(this).children().stop()
                })
            };
            if (o == 'play') {
                _.me.mouseout(function () {
                    $(this).children().stop();
                    $(this).children().trigger('marquee', ['resume'])
                })
            } else {
                _.me.mouseout(function () {
                    $(this).children().stop()
                })
            };
            if (p != 'play') {
                _.me.children().stop()
            }
        };

        function scrollVertical() {
            _.me.children().bind('marquee', function (a, c) {
                var b = $(this);
                var d = parseInt(b.parent().height());
                var e = parseInt(b.height());
                var f = parseInt(b.position().top);
                var v = i > 0 && i < 100 ? (100 - i) * 100 : 5000;
                var g = parseInt(v * e / 100) + v;
                if (m == 'pingpong') {
                    switch (k) {
                    case 'bottom':
                        if (typeof c == 'undefined') {
                            b.css({
                                top: d - e
                            })
                        };
                        d = f - (e + d);
                        break;
                    default:
                        if (typeof c == 'undefined') {
                            b.css({
                                top: 0
                            })
                        };
                        d += f + e
                    }
                } else {
                    switch (k) {
                    case 'bottom':
                        if (typeof c == 'undefined') {
                            b.css({
                                top: d
                            });
                            d = -e
                        } else {
                            d = f - (e + d)
                        };
                        break;
                    default:
                        if (typeof c == 'undefined') {
                            b.css({
                                top: -e
                            })
                        } else {
                            d += f + e
                        }
                    }
                };
                if (l < 0 || l > 0) {
                    if (l > 0) {
                        l--
                    };
                    b.animate({
                        top: d
                    }, {
                        duration: g,
                        easing: 'linear',
                        complete: function () {
                            b.trigger('marquee')
                        },
                        step: function () {
                            switch (m) {
                            case 'pingpong':
                                if (k == 'bottom') {
                                    if (parseInt(b.position().top) <= 0) {
                                        k = 'top';
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                } else {
                                    if (parseInt(b.position().top) + parseInt(b.height()) >= parseInt(b.parent().height())) {
                                        k = 'bottom';
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                };
                                break;
                            default:
                                if (k == 'bottom') {
                                    if (parseInt(b.position().top) < -parseInt(b.height())) {
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                } else {
                                    if (parseInt(b.position().top) > parseInt(b.parent().height())) {
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                }
                            }
                        }
                    })
                }
            }).trigger('marquee')
        };

        function scrollHorizontal() {
            _.me.children().bind('marquee', function (a, c) {
                var b = $(this);
                var d = parseInt(b.parent().width());
                var e = parseInt(b.width());
                var f = parseInt(b.position().left);
                var v = i > 0 && i < 100 ? (100 - i) * 100 : 5000;
                var g = parseInt(v * e / 100) + v;
                if (m == 'pingpong') {
                    switch (k) {
                    case 'right':
                        if (typeof c == 'undefined') {
                            b.css({
                                left: d - e
                            })
                        };
                        d = f - (e + d);
                        break;
                    default:
                        if (typeof c == 'undefined') {
                            b.css({
                                left: 0
                            })
                        };
                        d += f + e
                    }
                } else {
                    switch (k) {
                    case 'right':
                        if (typeof c == 'undefined') {
                            b.css({
                                left: d
                            });
                            d = -e
                        } else {
                            d = f - (e + d)
                        };
                        break;
                    default:
                        if (typeof c == 'undefined') {
                            b.css({
                                left: -e
                            })
                        } else {
                            d += f + e
                        }
                    }
                };
                if (l < 0 || l > 0) {
                    if (l > 0) {
                        l--
                    };
                    b.animate({
                        left: d
                    }, {
                        duration: g,
                        easing: 'linear',
                        complete: function () {
                            b.trigger('marquee')
                        },
                        step: function () {
                            switch (m) {
                            case 'pingpong':
                                if (k == 'right') {
                                    if (parseInt(b.position().left) <= 0) {
                                        k = 'left';
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                } else {
                                    if (parseInt(b.position().left) + parseInt(b.width()) >= parseInt(b.parent().width())) {
                                        k = 'right';
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                };
                                break;
                            default:
                                if (k == 'right') {
                                    if (parseInt(b.position().left) < -parseInt(b.width())) {
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                } else {
                                    if (parseInt(b.position().left) > parseInt(b.parent().width())) {
                                        b.stop();
                                        b.trigger('marquee')
                                    }
                                }
                            }
                        }
                    })
                }
            }).trigger('marquee')
        };
        return this
    }
})(jQuery);