$(function () { var $listItems = $('#rm_container > ul > li'), totalItems = $listItems.length, $rm_next = $('#rm_next'), $rm_prev = $('#rm_prev'), ieLte8 = ($.browser.msie && parseInt($.browser.version) <= 8), RotateImageMenu = (function () { var timeDiff = 500, slideshowTime = 6000, slideshowInterval, isRotating = false, completed = 0, init = function () { configure(); setTimeout(startSlideshow, slideshowTime); initEventsHandler() }, initEventsHandler = function () { $rm_next.bind('click', function (e) { rotateImages(1) }); $rm_prev.bind('click', function (e) { rotateImages(-1) }); $(document).keydown(function (e) { switch (e.which) { case 37: stopSlideshow(); rotateImages(0); break; case 39: stopSlideshow(); rotateImages(1); break } }) }, rotateImages = function (dir) { if (isRotating) return false; isRotating = true; $listItems.each(function (i) { var $item = $(this), interval = (dir === 1) ? i * timeDiff : (totalItems - 1 - i) * timeDiff; setTimeout(function () { var $otherImages = $('#' + $item.data('images')).children('img'), totalOtherImages = $otherImages.length; $img = $item.children('img:last'), current = $item.data('current'); if (current > totalOtherImages - 1) current = 0; else if (current < 0) current = totalOtherImages - 1; var $other = $otherImages.eq(current).clone(); (dir === 1) ? ++current : --current; $item.data('current', current).prepend($other); $img.fadeOut(1200, function () { $(this).remove(); ++completed; if (completed === totalItems) { completed = 0; isRotating = false } }) }, interval) }) }, configure = function () { $listItems.each(function (i) { var $item = $(this).data('current', 1) }) }, startSlideshow = function () { clearInterval(slideshowInterval); rotateImages(1); slideshowInterval = setInterval(function () { rotateImages(1) }, slideshowTime) }, stopSlideshow = function () { clearInterval(slideshowInterval) }; return { init: init} })(); RotateImageMenu.init() });
