function slideSwitch(s, random) { var $next, random = random || 0, $slideshow = $('#slideshow'), $active = $slideshow.find('DIV.active'); if ( ! $active.length ) $active = $slideshow.find('DIV:last'); if ( random ) { var $sibs = $active.siblings(), rndNum = Math.floor(Math.random() * $sibs.length), $next = $($sibs[rndNum]); } else { $next = $active.next().length ? $active.next() : $slideshow.find('DIV:first'); } $active.addClass('last-active'); $next.css({ opacity: 0 }) .addClass('active') .animate({ opacity: 1 }, s, function() { $active.removeClass('active last-active'); }); } function slideSwitchInit(delay, random) { var speed = 1500; delay = delay >= speed ? delay : 5; setInterval(function() { slideSwitch(speed, random); }, (delay * 1000) + 50); }