(function ($) {
    
$.fn.simpleSpy = function (limit, interval) {
    limit = limit || 7;
    interval = interval || 4200;
	animation = true;
	
    
    return this.each(function () {
        var $list = $(this),
            items = [], 
			
            currentItem = limit,
			backItem = 0,
            total = 0, 
            height = $list.find('> li:first').height();
		 //  height = 90;
			
        $list.find('> li').each(function () {
            items.push('<li>' + $(this).html() + '</li>');
        });
        
        total = items.length;
		backItem = total-1;
       // $list.wrap('<div class="spyWrapper" />').parent().css({ height : (height * limit)+550 });
      //  $list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();
	  
	  	myTimer = setTimeout(spy, interval);
	  
	  $list.find('> li').remove();
		for(i=0;i<limit;i++){
			$(items[i]).appendTo($list);
		}
        
        function spy() {
            if( animation ){
				animation = false;
			var $insert = $(items[currentItem]).css({
                opacity : 0,
				display : 'none'
            }).prependTo($list);
					           
            $list.find('> li:last').animate({ opacity : 0}, 350, function () {
                $insert.animate({ height : 'show' }, 350).animate({ opacity : 1 }, 350, function () {	
					myTimer = setTimeout(spy, interval);
					currentItem++;
					if (currentItem >= total) {
						currentItem = 0;
					}
					backItem++;
					if (backItem >= total) {
						backItem = 0;
					}
					
					animation = true;
				});
               $(this).remove();
       			
            });
			
			}
        }
		
        
		
		$("#down").click(function () {
  			if( animation ){
				animation = false;
				var $insert = $(items[currentItem]).css({
					opacity : 0,
					display : 'none'
				}).prependTo($list);
								   
				$list.find('> li:last').animate({ opacity : 0}, 350, function () {
					$insert.animate({ height : 'show' }, 350).animate({ opacity : 1 }, 350, function () {	
						myTimer = setTimeout(spy, interval);
						currentItem++;
						if (currentItem >= total) {
							currentItem = 0;
						}
						
						backItem++;
						if (backItem >= total) {
							backItem = 0;
						}
						animation = true;
					});
				   $(this).remove();
					
				});
				
	   
				clearTimeout(myTimer);
			}
			
			
		});
		
	
		$("#up").click(function () {
			
			if( animation ){
				var $insert = $(items[backItem]).css({
					opacity : 0
				}).appendTo($list);
				animation = false;
				$list.find('> li:first').animate({ opacity : 0}, 350).animate({ height : 'hide' }, 350, function () {
					$insert.animate({height:'show' }, 350).animate({ opacity : 1 }, 350, function () {
						currentItem--;
						if (currentItem < 0) {
							currentItem = total-1;
						}
						backItem--;
						if (backItem < 0) {
							backItem = total-1;
						}
						animation = true;
						myTimer = setTimeout(spy, interval);
					
					});
					$(this).remove();
				});
		
				clearTimeout(myTimer);	
			
			}
			
			
		});
    });
};
    
})(jQuery);


