jQuery(document).ready(
      function () {
         jQuery('.before-image').hover(
            function () {
               var tThis = jQuery(this);
               tThis.animation({ top: '-=25px'}, 10000);
            },
            function () {
            }
         );

         /* Add Slider */
         var tSlider = jQuery('<div class="mega-selected"><a href="#">&nbsp;</a></div>');
         jQuery('.rotate_box .rotate_menu').prepend(tSlider);

         tSlider.css('left', -tSlider.width());
         tSlider.show();

         function moveSlider(fToElement, fFromElement, fTime, fPostBack) {
            var tMenuItem = fToElement.get(0).menuItem;
            tRotatingMenuItems.removeClass('selected');
            tMenuItem.addClass('selected');
            tSlider.animate({ left: tMenuItem.position().left, width: tMenuItem.width() }, fTime);
            (jQuery.isFunction(fPostBack) ? fPostBack : jQuery.noop)(fToElement, fFromElement);
         }

         /* Link the slides with the menu items */
         var tRotatingMenuItems = jQuery('.rotate_box .rotate_menu ul li');
         var tRotatingItems = jQuery('.rotate_box .rotate_img').each(function (fIndex, fElement) {
            fElement.orderIndex = fIndex;
            fElement.menuItem = tRotatingMenuItems.eq(fIndex);
            fElement.menuItem.find('a').get(0).slideNumber = fIndex;
         })

         tRotatingMenuItems.find('a').click(function () { tRotator.doSelect(this.slideNumber, true); return false; });

         /* Rotator */
         var tRotator = (function (fContainer, fItemsForRotate, fCycleTime, fAnimationTime, fEvents) {
            var tRotatorItems = fItemsForRotate.map(function () { return jQuery(this); }).get();
            var tRotatorItemCurrentPosition = 0;
            var tRotatorCurrent = false;

            var doEventWrapper = function (fFn) { return jQuery.isFunction(fFn) ? fFn : jQuery.noop };
            var tEvents = { beforeChangeSlides: doEventWrapper(fEvents.beforeChangeSlides)
                           , afterChangeSlides: doEventWrapper(fEvents.afterChangeSlides)
            };

            var doChange = function (fIndex) {
               var tCurrentElement = tRotatorCurrent;
               tRotatorItemCurrentPosition = (fIndex || 0) % tRotatorItems.length;
               var tNewElement = tRotatorItems[tRotatorItemCurrentPosition];
               tEvents.beforeChangeSlides(tNewElement, tCurrentElement, fAnimationTime);

               fContainer.prepend(tNewElement);
               tNewElement.css('position', 'absolute');
               tNewElement.fadeIn(fAnimationTime, function () {
                  tCurrentElement.css('display', 'none');
                  tNewElement.css('position', 'static');
                  tEvents.afterChangeSlides(tNewElement, tCurrentElement, fAnimationTime);
                  tRotatorCurrent = tNewElement
               });
            }

            var tCycleTimeout = false;
            this.doStart = function (fPosition) {
               tRotatorItemCurrentPosition = (fPosition || tRotatorItemCurrentPosition) % tRotatorItems.length;
               tRotatorCurrent = tRotatorItems[tRotatorItemCurrentPosition];
               clearTimeout(tCycleTimeout);
               tCycleTimeout = setTimeout(doCycle, fCycleTime);
            }

            this.doSelect = function (fPosition, fStop) {
               if (!!fStop) clearTimeout(tCycleTimeout);
               doChange(fPosition);
            }

            var doCycle = function () {
               doChange(tRotatorItemCurrentPosition + 1);
               doStart();
            }

            return this;
         })(jQuery('.rotate_box .images-list'), tRotatingItems, 6000, 1000, { beforeChangeSlides: moveSlider });

         /* Prepare Fist Show */
         var tRandom = Math.random() * 10000 % tRotatingItems.length;
         moveSlider(tRotatingItems.eq(tRandom).show(), null, 0, function () { tRotator.doStart(parseInt(tRandom)) })
      }
    );



