/**
 * liscroll
 *
 * Scrolls a <UL> like a stock ticker
 *
 * Use it like this:
 *
 * $("ul#ticker01").show();
 * s = new liscroll($("ul#ticker01"), scroll speed [500], delay [2000]);
 * s.start();
 *
 * Define you <UL> like this:
 *
 * <ul id="ticker01" style="display: none">
 * etc.
 *
 * Expects styles called:
 * .tickercontainer
 * .tickercontainer mask
 * ul.newsticker
 * ul.newsticker li
 *
 * See the application.css file for examples of these
 */
function liscroll(object, scrolltime, waittime)
{
   this.obj = object;
   this.timer = null;
   this.width = 0;
   this.count = 0;
   this.scrollamount = 0;
   this.scrolltime = scrolltime;
   this.scrolltimedef = scrolltime;
   this.waittime = waittime;

   this.obj.addClass("newsticker");
   this.obj.wrap("<div class=\"mask\"></div>");
   this.obj.parent().wrap("<div class=\"tickercontainer\"></div>");
}

liscroll.prototype = {

   scroll: function(oInstance) {
      oInstance.obj.animate({ left: "-=" + oInstance.scrollamount }, oInstance.scrolltime, "linear", function()
      {
         oInstance.scrollamount = oInstance.width / oInstance.count;
         oInstance.scrolltime = oInstance.scrolltimedef;

         if (parseInt(oInstance.obj.css("left")) <= 0 - (oInstance.width / oInstance.count))
         {
            oInstance.obj.append($(oInstance.obj.selector + " li:first").clone());
            $(oInstance.obj.selector + " li:first").remove();
            oInstance.obj.css("left", 0);
         };
      });
   },

   start: function() {
      var oInstance = this;

      this.obj.find("li").each(
         function(i) {
            oInstance.width += jQuery(this, i).width();
            oInstance.count++;
         }
      );

      this.obj.width(this.width);

      this.obj.hover(
         function()
         {
            jQuery(this).stop();
            clearInterval(oInstance.timer);
         },
         function()
         {
            oInstance.scrollamount = parseInt(jQuery(this).css("left")) + (oInstance.width / oInstance.count);
            oInstance.scrolltime = (oInstance.scrolltimedef / (oInstance.width / oInstance.count)) * oInstance.scrollamount;
            oInstance.scroll(oInstance);
            oInstance.timer = setInterval(function() { oInstance.scroll(oInstance) }, oInstance.waittime);
         }
      );

      this.scrollamount = this.width / this.count;
      this.timer = setInterval(function() { oInstance.scroll(oInstance) }, this.waittime);
   }
};
