﻿var rotator = {};
rotator.conf = {};
var lastFrame = 0;

// the amount of wait time between rotation, in miliseconds (eg. 2.5 seconds => 2500)
rotator.conf.waitTime = 9000;


/**** DON'T MODIFY BELOW ****/

function addEventSimple(obj,evt,fn) {
  if (obj.addEventListener) {
    obj.addEventListener(evt,fn,false);
  } else if (obj.attachEvent) {
    obj.attachEvent('on'+evt,fn);
  }
}

rotator.impl = {};

// start rotation
function startRotate() {
  //rotator.impl.currentLinkIndex = 0;
  rotator.impl.nextTimeoutId = window.setTimeout("showNextLink()", rotator.conf.waitTime);
}

function showNextLink() {
  rotator.impl.currentLinkIndex++;
  if (rotator.impl.currentLinkIndex >= rotator.impl.linkEls.length) {
    rotator.impl.currentLinkIndex = 0;
  }

  highlightBtn(rotator.impl.currentLinkIndex);
  showLink(rotator.impl.currentLinkIndex);

  // schedule next image change
  rotator.impl.nextTimeoutId = window.setTimeout("showNextLink()", rotator.conf.waitTime);
}


// stop rotation
function stopRotate(IN_id)
{
    rotator.impl.currentLinkIndex = IN_id;
  window.clearTimeout(rotator.impl.nextTimeoutId);
}

var oldEl;
var linkEl;

function showLink(index) {
    if (index != lastFrame) {
        linkEl = $(rotator.impl.linkEls[index]);
        oldEl = $(rotator.impl.linkEls[lastFrame]);

        var fadeEffect = new Fx.Morph(linkEl, { duration: 600, transition: Fx.Transitions.Sine.easeOut });
        var fadeOutEffect = new Fx.Morph(oldEl, { duration: 600, transition: Fx.Transitions.Sine.easeOut });

        fadeOutEffect.start({ 'opacity': [1, 0] });
        fadeEffect.start({ 'opacity': [0, 1] });
        setTimeout('linkEl.style.display = "block";', 100); // this keeps the next frame from briefly flashing in ie

        lastFrame = index;
    }
}

// set the currently selected btn to class "rotatorBtnOn" and all others to "rotatorBtnOff"
function highlightBtn(index) {
  for (var i=0; i<rotator.impl.btnEls.length; i++) {
    var btnClass = rotator.impl.btnEls[i].className;
	if (i == index) {
      if (btnClass != "rotatorBtnOn") {
        rotator.impl.btnEls[i].className = "rotatorBtnOn";
	  }      
    } else {
      if (btnClass != "rotatorBtnOff") {
        rotator.impl.btnEls[i].className = "rotatorBtnOff";
      }
    }
  }
}

function selectBtn(index) {
  stopRotate();
  showLink(index);
  highlightBtn(index);
}

// starts the rotation
addEventSimple(window, "load", function()
{
    var linksDivEl = document.getElementById("rotatorLinks");

    rotator.impl.linkEls = linksDivEl.getElementsByTagName("div");
    
    var btnsDivEl = document.getElementById("rotatorBtns");
    rotator.impl.btnEls = btnsDivEl.getElementsByTagName("div");
    rotator.impl.currentLinkIndex = 0;
    startRotate();
});

// window.status
addEventSimple(window, "mousemove", function(){
  if (rotator.impl.currentLinkIndex) {
    window.status = rotator.impl.linkEls[rotator.impl.currentLinkIndex].href;
  }
});
 
