function swapImages() { var $active = $('#myGallery .active'); var $next = ($active.next().length > 0) ? $active.next() : $('#myGallery img:first'); while ($next && !$next.hasClass('ready')) { if ($next.next().length > 0) $next = $next.next(); else $next = false; } if ($next && $active) { $next.fadeIn( 2000 ).addClass('active'); $active.fadeOut( 2000 ).removeClass('active'); } } function SetImgReady( $ImgObj ) { $ImgObj.className += "ready"; }