
function doScroll(me, speed) {

   //count the number of li's in the ul for this scroller
   var jQueryThis = jQuery(me);

   //is this forwards or backwards?
   var forwards = jQueryThis.hasClass('forward');

   var parent = jQueryThis.parent();
   var noOfStories = jQuery('ul', parent).children('li').length;
   //get the id of the current story
   var currentStory = jQuery('.current', parent);
   currentStory.removeClass('current');
   var currentID = currentStory.attr('id');
   var currentIDNumber = Number(currentID.replace('story', ''));
   //if it's last one, go to the first
   var newStory;
   var oldStory = jQuery('#story' + String(currentIDNumber), parent);
   if (forwards) {
      if (noOfStories == currentIDNumber) {
         newStory = jQuery('#story1', parent);
      }
      //else go to the next story
      else {
         newStory = jQuery('#story' + String(currentIDNumber + 1), parent);
      }
   }
   else {
      if (1 == currentIDNumber) {
         newStory = jQuery('#story' + String(noOfStories), parent);
      }
      //else go to the next story
      else {
         newStory = jQuery('#story' + String(currentIDNumber - 1), parent);
      }
   }


   oldStory.fadeOut(speed, function () {

      newStory.addClass('current');
      newStory.fadeIn(speed);
   });
}

function autoScroll(what) {
   var objs = jQuery(what + " .scroller > .forward");
   objs.each(function (index, element) {
      doScroll(element, 1000);
   });
}
var timers = {};


jQuery(function () {
   jQuery('.current').css('display', 'block');
   jQuery('.scroller > ul').css({ display: 'block', height: jQuery('.scroller').height() });
   jQuery('.scroller > .forward, .scroller > .backward').click(function () {
      var parentID = "#" + jQuery(this).parent().parent().attr("id");
      doScroll(this, 200);
      // But we also want to reset the auto timer
      clearInterval(timers[parentID]);
      timers[parentID] = setInterval("autoScroll('" + parentID + "')", 5000);
   });

   jQuery('.scroller > .pause').click(function () {
      var parentID = "#" + jQuery(this).parent().parent().attr("id");
      jQuery(this).fadeIn(1);
      alert("jQuery");
      /*      doScroll(this, 99999999999999999999);
      // But we also want to reset the auto timer
      clearInterval(timers[parentID]);
      timers[parentID] = setInterval("autoScroll('" + parentID + "')", 5000);
      */
   });

   timers["#latestnews"] = setInterval("autoScroll(\"#latestnews\")", 5000);
   timers["#clientsuccess"] = setInterval("autoScroll(\"#clientsuccess\")", 6000);
   timers["#promoLinks"] = setInterval("autoScroll(\"#promoLinks\")", 4100);
});

/* AF from here 

function pauseScroll(toPause) {
   timers["#latestnews"] = setInterval("autoScroll(\"#latestnews\")", 9999999999999);
   alert("asddf");

}
*/
