/*
* vertical news ticker
* Tadas Juozapaitis ( kasp3rito@gmail.com )
* http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
*/
(function ($) {
    $.fn.sliding = function (options) {
        var defaults = {
            speed: 700,
            pause: 6000,
            showItems: 3,
            rowBinder: function (item) { return item.toString(); },
            height: 0,
            url: ''
        };

        var options = $.extend(defaults, options);

        moveDown = function (obj2, height, options) {

            var obj = obj2.children('ul');

            var clone = obj.children('li:last').clone(true);

            if (options.height > 0) {
                height = obj.children('li:first').height();
            }

            clone.removeClass("new");

            obj.css('top', '-' + height + 'px')
			.prepend(clone);

            obj.animate({ top: 0 }, options.speed, function () {
                $(this).children('li:last').remove();
                if (obj.children('li:last').hasClass("new")) {
                    moveDown(obj2, height, options);
                }
                else
                    while (obj.children('li').length > options.showItems + 1)
                        obj.children('li:last').remove();
            });

            if (options.animation == 'fade') {
                if (options.height == 0) {
                    obj.children('li:eq(' + options.showItems + ')').fadeOut(options.speed);
                }
                obj.children('li:first').hide().fadeIn(options.speed).show();
            }



        };

        return this.each(function () {
            var obj = $(this);
            var maxHeight = 0;

            obj.css({ overflow: 'hidden', position: 'relative' })
			.children('ul').css({ position: 'absolute', margin: 0, padding: 0 })
			.children('li').css({ margin: 0, padding: 0 });

            if (options.height == 0) {
                obj.children('ul').children('li').each(function () {
                    if ($(this).height() > maxHeight) {
                        maxHeight = $(this).height();
                    }
                });

                obj.children('ul').children('li').each(function () {
                    $(this).height(maxHeight);
                });

                obj.height(maxHeight * options.showItems);
            }
            else {
                obj.height(options.height);
            }

            var interval = setInterval(function () {

                $.post(options.url, function (res) {
                    if (res.Data.length > 0) {
                        for (var i = 0; i < res.Data.length; i++)
                            options.rowBinder && obj.children('ul').append(options.rowBinder(res.Data[i]));
                        moveDown(obj, maxHeight, options);
                    }
                }
                );
            }, options.pause);
        });
    };
})(jQuery);
