/* List Ticker by Alex Fish 
// www.alexefish.com
//
// options:
//
// effect: fade/slide
// speed: milliseconds
*/

(function($){
  $.fn.list_ticker = function(options){
    
    var defaults = {
      speed:4000,
      effect:'slide'
    };
    
    var options = $.extend(defaults, options);

    return this.each(function(){
      
      var obj = $(this);
      var list = obj.children();
      list.not(':first').hide();
      
      setInterval(function(){
        
        list = obj.children();
        list.not(':first').hide();
        
        var first_li = list.eq(0)
        var second_li = list.eq(1)
		
		if(options.effect == 'slide'){
			first_li.slideUp();
			second_li.slideDown(function(){
				first_li.remove().appendTo(obj);
			});
		} else if(options.effect == 'fade'){
			first_li.fadeOut(function(){
				second_li.fadeIn();
				first_li.remove().appendTo(obj);
			});
		}
      }, options.speed)
    });
  };



  $.fn.list_ticker_rnd = function(options) {

    var defaults = {
        speed: 1000,
        fadeSpeed: 1000
    }
    options = $.extend(defaults, options)

    var size = $(this).children().length
    var u = $("<ul></ul>")
    u.append($(this).children())
    var th = $(this)
    var last = undefined

    var rand = function() {
        var rnd = Math.round(Math.random() * (size - 1))
        if(last != undefined && rnd == last)
            return rand()
        last = rnd 
        return rnd
    }

    var startAnim = function() {
        setTimeout(function() {
            th.children().remove() 
            th.append(u.find("li:eq("+rand()+")").clone().css("display", "list-item").hide().fadeIn(options.fadeSpeed, startAnim)) 
        }, options.speed) 
    }
    $(this).append(u.find("li:eq("+rand()+")").clone()) 
    startAnim() 
  }


})(jQuery);
