
$(function () {
    $('ul.spy').simpleSpy().bind('mouseenter', function () {
        $(this).trigger('stop');
    }).bind('mouseleave', function () {
        $(this).trigger('start');
    });
});

(function ($) {
    
$.fn.simpleSpy = function (limit, interval) {
    limit = limit || 12;
    interval = interval || 4000;
    
    function getSpyItem($source) {
        var $items = $source.find('> li');
        
        if ($items.length == 1) { } 
		else if ($items.length == 0) {
            return false;
        }
        
        return $items.filter(':first').remove();
    }
    
    return this.each(function () {
	
		//show hidden div
		document.getElementById('rightcontent').style.display = 'block';
		
        var $list = $(this),
            running = true,
            height = $list.find('> li:first').height();
            
        var $source = $('<ul />').hide().appendTo('body');
                    
        $list.wrap('<div class="spyWrapper" />').parent().css({ height : height * limit });
        
        $list.find('> li').filter(':gt(' + (limit - 1) + ')').appendTo($source);

        $list.bind('stop', function () {
            running = false;
        }).bind('start', function () {
            running = true;
        });

        // 2. effect
        function spy() {
            if (running) {
                var $item = getSpyItem($source);

                if ($item != false) {
                    var $insert = $item.css({
                        height : 0,
                        opacity : 0,
                        display : 'none'
                    }).prependTo($list);

                    $list.find('> li:last').animate({ opacity : 0}, 100, function () {
                        $insert.animate({ height : height }, 100).animate({ opacity : 1 }, 100);
                            $(this).remove();
                    });             
                }                
            }
            
            setTimeout(spy, interval);
        }
    
        spy();
    });
};
    
})(jQuery);
