
// template

var animSpeed = 500;
var animSpeedQuick = 250;
var nice = true;

$(document).ready(function() {
  nice = !($.browser.msie && $.browser.version < 9);
  navInit();
  rotatorInit();
});

// rotator

var interval = 5000;
var fadeTime = 500;
var rotators;

function Rotator(id, e, c, interval, fadeTime) {
  // methods
  this.pause = function() {
    if (this.timer == -1) {
      this.timer = window.setInterval("rotators[" + id + "].next();", interval);
    } else {
      window.clearInterval(this.timer);
      this.timer = -1;
    }
  }
  this.resetTimer = function() {
    if (this.timer > -1) {
      window.clearInterval(this.timer);
      this.timer = window.setInterval("rotators[" + id + "].next();", interval);
    }
  }
  this.jump = function(i) {
    if (i != this.index) {
      var current = $(this.images[this.index]);
      this.index = i;
      var next = $(this.images[this.index]);
      this.zIndex++;
      next.css({ opacity: 0, display: "block", zIndex: this.zIndex });
      $("#rotatorControls img").attr("src", "/_images/button-square-grey.png");
      $("#rotatorButton" + this.index).attr("src", "/_images/button-square-orange.png");
      next.fadeTo(this.fadeTime, 1, function() {
        current.css({ display: "none" });
      });
    }
    this.resetTimer();
  }
  this.next = function() {
    if (!this.paused) {
      var current = $(this.images[this.index]);
      this.index++;
      if (this.index == this.images.length) this.index = 0;
      var next = $(this.images[this.index]);
      this.zIndex++;
      next.css({ opacity: 0, display: "block", zIndex: this.zIndex });
      $("#rotatorControls img").attr("src", "/_images/button-square-grey.png");
      $("#rotatorButton" + this.index).attr("src", "/_images/button-square-orange.png");
      next.fadeTo(this.fadeTime, 1, function() {
        current.css({ display: "none" });
      });
    }
  }
  // init vars
  this.id = id;
  this.element = e;
  this.controls = c;
  this.interval = interval;
  this.fadeTime = fadeTime;
  this.timer = -1;
  this.zIndex = 3;
  this.index = 0;
  this.paused = false;
  // find images
  var images = new Array();
  this.element.find(".rotatorItem").each(function() {
    images.push(this);
  });
  this.images = images;
  if (this.images.length == 0) return;
  // show first
  var first = $(this.images[0]);
  first.css({ display: "block" });
  // init timer
  if (this.images.length > 1) {
    var html = "";
    for (var i = 0; i < this.images.length; i++) {
      html += "<img onclick=\"rotators[" + this.id + "].jump(" + i + ");\" id=\"rotatorButton" + i + "\" src=\"/_images/button-square-";
      if (i == 0) {
        html += "orange.png\"/>";
      } else {
        html += "grey.png\"/>";
      }
    }
    c.html(html);
    c.css("display", "block");
    this.pause();
  }
}

function rotatorInit() {
  rotators = new Array();
  rotators[0] = new Rotator(0, $("#rotator"), $("#rotatorControls"), interval, fadeTime);
  // add pause behaviour to parent control
  $("#navRight")
    .bind("mouseenter", function(ev) { rotators[0].paused = true; })
    .bind("mouseleave", function(ev) { rotators[0].paused = false; rotators[0].resetTimer(); });
}

// navigation

var navOffsetTop = 0;
var navHolderHeight = 0;
var navDelay = 1000;
var tabs;

function showNav(ev, quick) {
  var l = $(ev.currentTarget);
  var t = l.attr("class");
  var e = $(".navTab." + t);
  for (var i = 0; i < tabs.length; i++) {
    tabs[i] = false;
  }
  tabs[t] = true;
  var h = e.height();
  var y = l.offset().top - navOffsetTop;
  if((y + h) > navHolderHeight) y = (navHolderHeight - h - 20);
  e.stop()
    .css({ top: y })
    .animate({ left: 220 }, quick == null ? animSpeed : 0);
  $(".navTab").not(e)
    .stop()
    .animate({ left: -300 }, quick == null ? animSpeed : 0);
}

function hideNav(ev) {
  var l = $(ev.currentTarget);
  var t = l.attr("class");
  tabs[t] = false;
  window.setTimeout("hideNavCallback('" + t + "');", navDelay);
}

function hideNavCallback(t) {
  if (!tabs[t]) {
    var e = $(".navTab." + t);
    e.stop()
      .animate({ left: -300 }, animSpeed);
  }
}

function preShowNav(id) {
  var l = $("#nav" + id);
  var e = $(".navTab.tab" + id);
  var h = e.height();
  var y = l.offset().top - navOffsetTop;
  if ((y + h) > navHolderHeight) y = (navHolderHeight - h - 20);
  e.css({ top: y, left: 220 });
}

function navInit() {
  // cache a few dimensions
  var e = $("#navLeft");
  navOffsetTop = e.offset().top;
  navHolderHeight = e.height();
  // prepare rollovers
  if (nice) {
    $(".navRollover")
      .fadeTo(0, 0)
      .css("display", "block");
    $("#navigation li")
      .bind("mouseenter", function(ev) {
        $(ev.target)
          .find(".navRollover")
          .stop()
          .fadeTo(animSpeedQuick, 1.0);
        ev.stopPropagation();
      })
      .bind("mouseleave", function(ev) {
        $(ev.target)
          .find(".navRollover")
          .stop()
          .fadeTo(animSpeedQuick, 0.0);
        ev.stopPropagation();
      });
  } else {
    $("#navigation li")
      .bind("mouseenter", function(ev) {
        $(ev.target)
          .find(".navRollover")
          .css("display", "block");
        ev.stopPropagation();
      })
      .bind("mouseleave", function(ev) {
        $(ev.target)
          .find(".navRollover")
          .css("display", "none");
        ev.stopPropagation();
      });
  }
  // prepare tabs
  tabs = new Array();
  var navItems = $("#navLeft ul li");
  navItems.bind("mouseenter", showNav).bind("mouseleave", hideNav);
  navItems.each(function() {
    var t = $(this).attr("class");
    tabs[t] = false;
    $(".navTab." + t)
      .bind("mouseenter", function() { tabs[t] = true; })
      .bind("mouseleave", function() { tabs[t] = false; window.setTimeout("hideNavCallback('" + t + "');", navDelay);  });
  });
}

