(function($) {
$.fn.newsTicker = $.fn.newsticker = function(delay) {
    firstTime = true;
    delay = delay || 4000;
    initTicker = function(el) {
	stopTicker(el);
	el.items = $("li", el);
	el.items.hide().end(); // Start will all hidden
	el.currentitem = 0;
	$(el.items[el.currentitem]).fadeIn("slow", function() {
	    el.pause = false; // Fade the first item in
	});
	startTicker(el);
    };
    startTicker = function(el) {el.tickfn = setInterval(function() { doTick(el) }, delay)};
    stopTicker = function(el) {clearInterval(el.tickfn);};
    pauseTicker = function(el) {el.pause = true;};
    resumeTicker = function(el) {el.pause = false;};
    doTick = function(el) {
	// don't run if paused
	if (el.pause) return;
	// pause until animation has finished
	el.pause = true;
	// hide current item
	$(el.items[el.currentitem]).fadeOut("slow", function() {
	    $(this).hide();
	    // move to next item and show
	    el.currentitem = ++el.currentitem % (el.items.size());
	    $(el.items[el.currentitem]).fadeIn("slow", function() {
		el.pause = false;
	    });
	});
    };
    this.each(function() {
	if (this.nodeName.toLowerCase()!= "ul") return;
	initTicker(this);
    }).addClass("newsticker").hover(function() {
	// pause if hovered over
	pauseTicker(this);
    }, function() {
	// resume when not hovered over
	resumeTicker(this);
    });
    return this;
};
})(jQuery);

