// this file is a re-write of the code Garth wrote that was based on Prototype and Effects
// this depends only on jQuery, which is, long story short, incompatible with Prototype in my experience
// Thanks to Garth for the heads up on jQuery, and for the model upon which this is based.

// To use this, put something like this in the javascript for the page

/*
$(document).ready(function() { // stuff to do when DOM is ready
   sos.enr.loadAndStartTicker("#ticker", &APP_ID., 803);
});
*/

// Create namespace
if (sos == undefined) var sos = {};
sos.enr = {};

// load the ticker with content via AJAX and start it running
sos.enr.loadAndStartTicker = function(tickerSelector, appId, pageId)
{
   $(tickerSelector).load("f?p=" + appId + ":" + pageId + ":0", null
      , function (responseText, textStatus, XMLHttpRequest) {
           enrSosTicker = new sos.enr.ticker();
           $(tickerSelector).show();
           enrSosTicker.start("enrSosTicker");
        });
}

// create a ticker
sos.enr.ticker = function ()
{
   return {
      tickerItemSelector: "div#ticker li"
      , itemIndex: 0
      , interval: 8000
      , transitionTime: 4000
      // starts the ticker animations
      , start: function(tickerVar)
   	{
   	   this.tickerVar = tickerVar;
         this.itemCount = jQuery.makeArray($(this.tickerItemSelector)).length;
   		this.hideTickerItems();
   		this.itemIndex = 0;
   	   this.fadeInCurrent();
   		setTimeout(this.tickerVar + '.showNext();', this.interval);// Install timer
     	}
     	// shows the next ticker item
     	, showNext: function()
   	{
   	   this.fadeOutCurrent();
   	   this.incrementItem();
   	   setTimeout(this.tickerVar + ".fadeInCurrent()", this.transitionTime + 1000);
   		setTimeout(this.tickerVar + '.showNext();', this.interval);// Install timer
     }
     // shows the previous ticker item, called by a button
     , showPrev: function()
     {
   	   this.fadeOutCurrent();
   	   this.decrementItem();
   	   setTimeout(this.tickerVar + ".fadeInCurrent()",  this.transitionTime + 1000);
   		setTimeout(this.tickerVar + '.showNext();', this.interval);// Install timer
     }
     // fades in the current ticker item
     	, fadeInCurrent: function()
     	{
         var item = $(this.tickerItemSelector + ":eq(" + this.itemIndex + ")");
         var posBottom = 25;
         item.css({opacity: 0, top: posBottom}); // make them invisible
         item.show(); // show them
     	   item.animate({ // then bring up their opacity slowly and slide them into position
     	         opacity: 1
     	         , top: 10
     	      }, this.transitionTime);
     	}
     	// fades out the current ticker item
     	, fadeOutCurrent: function()
     	{
         var item = $(this.tickerItemSelector + ":eq(" + this.itemIndex + ")");
         item.animate({
            opacity: 0
            , top: -10
     	      }, this.transitionTime);
     	   item.hide(this.transitionTime);
     	}
     	// hides all the ticker items
      , hideTickerItems: function()
      {
         $(this.tickerItemSelector).hide();
      }
     	, incrementItem: function()
     	{
     	   this.itemIndex += 1;
     	   if (this.itemIndex > this.itemCount - 1) {
     	      this.itemIndex = 0;
     	   }
     	}
     	, decrementItem: function()
     	{
     	   this.itemIndex -= 1;
     	   if (this.itemIndex < 0) {
     	      this.itemIndex = this.itemCount - 1;
     	   }
     	}
   }
}

