$(document).ready(function(){
    $('.tips ul').each(function(){
        var target = $('li', this);
        var listPosition = new Array();
        var listHeight = 0;
        var listNumber = 2;
        var intervalTime = 7000;
        
        $(target).each(function(){
            listHeight = listHeight < $(this).height() ? $(this).height() : listHeight; 
            var pos = $(this).position().top - $(this).parent().position().top - parseInt($(this).parent().css('margin-top'), 10);
            listPosition.push(pos);   
        });
        
        var listFontSize = parseInt($(target).css('font-size'), 10);
        var counter = 0;
        var listEmHeight = listHeight / listFontSize;
        var parentHeight = (listEmHeight * listNumber)+'em';
        var topPosition = (listEmHeight * 2.5 * listNumber)+'em';
        
        $(this).css({'height': parentHeight, 'overflow': 'hidden', 'position':'relative'});
        $(target).css({'top':  topPosition, 'position':'absolute'});
        
        for(var i=0; i<listNumber; i++){
            $(target).filter(':eq('+ counter+i +')').css('top', (listPosition[counter+i] / listFontSize) +'em');
        }
        
        
        var rotate = function(){
            if(listNumber == 1){
                $(target).filter(':eq('+ counter +')').animate({top: '-' + topPosition}, 2000);
                counter = (counter + 1) < $(target).size() ? counter+1 : 0;
                $(target).filter(':eq('+ counter +')').css({'top': topPosition});
                $(target).filter(':eq('+ counter +')').animate({top: 0}, 1500);
            }
            else{
                $(target).filter(':eq('+ counter +')').animate({top: '-' + topPosition}, 2000);
                if(counter==(target).size()-1){
                    $(target).filter(':eq(0)').animate({top: 0}, 800);
                }
                else{
                    $(target).filter(':eq('+ counter +')').next().animate({top: 0}, 800);
                }
                counter = (counter + 1) < $(target).size() ? counter+1 : 0;
                if(counter==(target).size()-1){
                    $(target).filter(':eq(0)').css({'top': topPosition});
                    $(target).filter(':eq(0)').animate({top: $(target).filter(':last-child').height() / listFontSize +'em'}, 1500);
                }
                else{
                    $(target).filter(':eq('+ counter +')').next().css({'top': topPosition});
                    $(target).filter(':eq('+ counter +')').next().animate({top: $(target).filter(':eq('+ counter +')').height() / listFontSize +'em'}, 1500);
                }
                //counter = (counter + 1) < $(target).size() ? counter : 0;
            }
        }
        
        var interval = window.setInterval(function(){rotate()}, intervalTime);
        $(target).hover(function(){
            interval=window.clearInterval(interval)
        }, function(){
            interval = setInterval(function(){rotate();}, intervalTime);
        });
    });
});
