jQuery.fn.factoids = function(settings) {
  
  settings = jQuery.extend({
    scrollClass: 'scroll', // This class will be applied to the div holding the next/prev links
    scrollTitle: '&nbsp;',       // This text will be shown between the next/prev links
    nextLabel: 'Next',     // The text of the "Next" link
	//paths are not managed in this file, so these paths are correct.
    prevLabel: 'Prev',     // The text of the "Prev" link
    showEffect: 'show',    // The effect to use to show a factoid.  Any jquery animation that takes a speed as its first param is acceptable
    hideEffect: 'hide',    // The effect to use to hide a factoid.  Any jquery animation that takes parameters speed and callback is acceptable
    effectSpeed: 'normal'  // The speed in ms of the show/hide animation.  Takes an int or slow/normal/fast
  }, settings);

  return this.each(function(){
    if (jQuery(this).is('ul')) {
      var factoids = $(this).find('li')
        .hide()
        .each(function() {
          // Attach the next/prev block
          jQuery('<div class="' + settings.scrollClass + '"><a href="#" class="prev">' + settings.prevLabel + '</a> <span>' + settings.scrollTitle + '<span> <a href="#" class="next">' + settings.nextLabel + '</a></div>')
            .appendTo(jQuery(this))
            .parents('ul:first')
              // Set up the event handlers, including special-case "end-post" handlers
              .find('li:first-child a.prev').bind('click', {settings: settings}, jQuery.factoids.firstPrevClick).end()
              .find('li:last-child a.next').bind('click', {settings: settings}, jQuery.factoids.lastNextClick).end()
              .find('li:not(:last-child) a.next').bind('click', {settings: settings}, jQuery.factoids.nextClick).end()
              .find('li:not(:first-child) a.prev').bind('click', {settings: settings}, jQuery.factoids.prevClick).end()
            ;
        });
      
      // Show one at random
      $(factoids.get(Math.floor(Math.random() * factoids.size()))).show();
    }
  });
};

jQuery.factoids = {
  nextClick : function(event) {
    event.preventDefault();
    var settings = event.data.settings;
    jQuery(this)
      // Run the hide effect on the visible element, then the show effect on the next element to show
      .parents('ul:first').find('li:visible')[settings.hideEffect](settings.effectSpeed, function() {
        jQuery(this).next()[settings.showEffect](settings.effectSpeed);
      });
  },
  prevClick : function(event) {
    event.preventDefault();
    var settings = event.data.settings;
    jQuery(this)
      .parents('ul:first').find('li:visible')[settings.hideEffect](settings.effectSpeed, function() {
        jQuery(this).prev()[settings.showEffect](settings.effectSpeed);
      });
  },
  firstPrevClick : function(event) {
    event.preventDefault();
    var settings = event.data.settings;
    jQuery(this)
      .parents('ul:first').find('li:visible')[settings.hideEffect](settings.effectSpeed, function() {
        jQuery(this).parents('ul:first').children(':last-child')[settings.showEffect](settings.effectSpeed);
      });
  },
  lastNextClick : function(event) {
    event.preventDefault();
    var settings = event.data.settings;
    jQuery(this)
      .parents('ul:first').find('li:visible')[settings.hideEffect](settings.effectSpeed, function() {
        jQuery(this).parents('ul:first').children(':first-child')[settings.showEffect](settings.effectSpeed);
      });
  }
};
