$(document).ready(function () {
    $('.cticker').each(function () {
        var $this = $(this);

        $this.children().each(function () {
            $this.append($(this).clone());
        });

        var padding = 0;
        var margin = 0;

        var lowestHeight = Number.MAX_VALUE;
        $this.children().each(function () {
            var $this = $(this);
            lowestHeight = Math.min(lowestHeight, $this.height());
            padding = parseInt($this.css('paddingTop').replace('px', '')) + parseInt($this.css('paddingBottom').replace('px', ''));
            margin = parseInt($this.css('marginTop').replace('px', '')) + parseInt($this.css('marginBottom').replace('px', ''));
        });

        var opt = {
            el: $this,
            lowestHeight: lowestHeight,
            padding: padding,
            margin: margin
        };

        $this.hover(function () {
            ctickerMove({
                el: $this,
                speed: 0
            });
        }, function () {
            ctickerMove(opt);
        });

        ctickerMove(opt);
    });
});

function ctickerMove(opt) {
    if (opt.speed == 0) {
        opt.el.children(':first').stop(true);
    }
    else {
        var h = (opt.el.children(':first').height()+opt.padding+opt.margin);
        var f = h / opt.lowestHeight;
        
        opt.el.children(':first').animate({
            marginTop: (h*-1)+'px'
        }, f*2500, 'linear', function() {
            opt.el.append(opt.el.children(':first'));
            opt.el.children(':last').attr('style', '');
            ctickerMove(opt);
        });
    }
}
