fader.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. function smf_NewsFader(oOptions)
  2. {
  3. this.opt = oOptions;
  4. this.oFaderHandle = document.getElementById(this.opt.sFaderControlId);
  5. // Surround each item with... anything special?
  6. this.sItemTemplate = 'sItemTemplate' in this.opt ? this.opt.sItemTemplate : '%1$s';
  7. // Fade delay (in milliseconds).
  8. this.iFadeDelay = 'iFadeDelay' in this.opt ? this.opt.iFadeDelay : 5000;
  9. // The array that contains all the lines of the news for display.
  10. this.aFaderItems = 'aFaderItems' in this.opt ? this.opt.aFaderItems : [];
  11. // Should we look for fader data, still?
  12. this.bReceivedItemsOnConstruction = 'aFaderItems' in this.opt;
  13. // The current item in smfFadeContent.
  14. this.iFadeIndex = -1;
  15. // Just make sure the page is loaded before calling the init.
  16. var fader = this;
  17. $(document).ready(function() {fader.init();});
  18. }
  19. smf_NewsFader.prototype.init = function init()
  20. {
  21. var oForeEl, oForeColor, oBackEl, oBackColor;
  22. // Did we get our fader items on construction, or should we be gathering them instead?
  23. if (!this.bReceivedItemsOnConstruction)
  24. {
  25. // Get the news from the list in boardindex
  26. var oNewsItems = this.oFaderHandle.getElementsByTagName('li');
  27. // Fill the array that has previously been created
  28. for (var i = 0, n = oNewsItems.length; i < n; i ++)
  29. this.aFaderItems[i] = oNewsItems[i].innerHTML;
  30. }
  31. $('#' + this.opt.sFaderControlId).fadeOut('fast');
  32. // Start the fader!
  33. this.fade();
  34. }
  35. // Main fading function... called 50 times every second.
  36. smf_NewsFader.prototype.fade = function fade()
  37. {
  38. if (this.aFaderItems.length <= 1)
  39. return;
  40. var currentText;
  41. // Starting out? Set up the first item.
  42. if (this.iFadeIndex == -1)
  43. {
  44. currentText = this.sItemTemplate.replace('%1$s', this.aFaderItems[0]);
  45. this.iFadeIndex = 1;
  46. }
  47. else
  48. {
  49. // Go to the next item, or first if we're out of items.
  50. currentText = this.sItemTemplate.replace('%1$s', this.aFaderItems[this.iFadeIndex ++]);
  51. if (this.iFadeIndex >= this.aFaderItems.length)
  52. this.iFadeIndex = 0;
  53. }
  54. $('#' + this.opt.sFaderControlId).fadeOut('slow', function () {
  55. setInnerHTML(this, currentText);
  56. }).fadeIn('slow');
  57. // Keep going.
  58. window.setTimeout(this.opt.sSelf + '.fade();', this.iFadeDelay);
  59. }