﻿  var MouseIsOver = false;
  $(document).ready(function () {

      $(window).blur(function () {
          MouseIsOver = false;
      });

      $(window).focus(function () {
          MouseIsOver = true;
      });

      $(window).bind("blur", function (event) {
          MouseIsOver = true;
      });
      $("#HeaderCollum .RotatorButton").click(function () {
          var url = $(this).parent().children(".RotatorText").children(".RotatorLinkButton").children(".readmore").attr("href");
          document.location = url;
      });

      $("#HeaderCollum .RotatorButton").mouseover(function () {
          MouseIsOver = true;
          setActive($(this));
      });

      $("#HeaderCollum .RotatorButton").mouseleave(function () {
          MouseIsOver = false;
      });

      $("#HeaderCollum .Rotator").mouseover(function () {
          MouseIsOver = true;
          setActive($(this));
      });

      $("#HeaderCollum .Rotator").mouseleave(function () {
          MouseIsOver = false;
      });

      setActive($("#HeaderCollum .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("#countdownerHolder").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");
          if ($(Item).parent().children("#countdownerHolder").length != -1) {
              $(Item).parent().children("#countdownerHolder").css("z-index", "5");
              $(Item).parent().children("#countdownerHolder").show();
          }

          $(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 = $("#HeaderCollum .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);
  }
