﻿  var MouseIsOver = false;
  $(document).ready(function () {

      $(window).bind("focus", function (event) {
          MouseIsOver = false;
      });

      $(window).bind("blur", function (event) {
          MouseIsOver = true;
      });
      $("#RotatorContainer .RotatorButton").mouseover(function () {
          MouseIsOver = true;
          setActive($(this));
      });

      $("#RotatorContainer .RotatorButton").mouseleave(function () {
          MouseIsOver = false;
      });

      setActive($("#RotatorContainer .RotatorButton").get(0));

      setTimeout('RoatateRoatator()', 5000);
  });

  function setActive(Item) {
      if (!$(Item).hasClass("RotatorButtonActive")) {
          $(Item).parent().parent().children(".Rotator").children(".RotatorButtonActive").addClass("RotatorButton");
          $(Item).parent().parent().children(".Rotator").children(".RotatorButtonActive").removeClass("RotatorButtonActive");
          $(Item).removeClass("RotatorButton");
          $(Item).addClass("RotatorButtonActive");

          $(Item).parent().parent().children(".Rotator").children(".RotatorImage").css("z-index", "0");
          $(Item).parent().parent().children(".Rotator").children(".RotatorText").css("z-index", "0");
          $(Item).parent().parent().children(".Rotator").children(".RotatorLinkButton").css("z-index", "0");

          $(Item).parent().parent().children(".Rotator").children(".RotatorText").css("display", "none");
          $(Item).parent().parent().children(".Rotator").children(".RotatorLinkButton").css("display", "none");

          $(Item).parent().parent().children(".Rotator").children(".RotatorImage").stop(true, true).fadeOut(200);
          $(Item).parent().parent().children(".Rotator").children(".RotatorImage").fadeOut(200);
          $(Item).parent().children(".RotatorImage").css("z-index", "5");
          $(Item).parent().children(".RotatorText").css("z-index", "5");
          $(Item).parent().children(".RotatorLinkButton").css("z-index", "5");

          $(Item).parent().children(".RotatorText").fadeIn(200);
          $(Item).parent().children(".RotatorImage").fadeIn(200);
          $(Item).parent().children(".RotatorLinkButton").fadeIn(200);
      }
  }
  //var activeIndex = 0;
  function RoatateRoatator() {
      if (!MouseIsOver) {
          var ListItems = $("#RotatorContainer .Rotator").get();
          var ActiveIndex = -1;

          for (var i = 0; i < ListItems.length + 1; i++) {
              if ($(ListItems[i]).children('.RotatorButtonActive').hasClass('RotatorButtonActive')) {
                  ActiveIndex = i;
              }
          }

          if (ActiveIndex == ListItems.length - 1) {
              ActiveIndex = 0;
          }
          else {
              ActiveIndex = ActiveIndex + 1;
          }

          setActive($(ListItems[ActiveIndex]).children('.RotatorButton'));
      }

      setTimeout('RoatateRoatator()', 5000);
  }
