// JavaScript Document

//image rotator code
var ImageRotator = Class.create();


ImageRotator.prototype = {
  // Arguments:
  //   images - A list of the ids of elements to rotate
  //   duration - the length of time an element to should show for
  initialize: function(args) {
    this.images = args['images'];
    this.duration = args['duration'] || 7000
  },
  
  start: function() {
    this.show(imageRotator.images[0]);

    setTimeout("imageRotator.crossFade(0)", this.duration);
  },

  show: function(image) {
    new Effect.Appear(image);
  },

  hide: function(image) {
    new Effect.Fade(image);
  },

  crossFade: function(current_index) {

    if(current_index+1 == imageRotator.images.length) {
      next_index = 0
    }
    else {
      next_index = current_index + 1
    }

    this.hide(imageRotator.images[current_index]);
    this.show(imageRotator.images[next_index]);
    setTimeout("imageRotator.crossFade("+next_index+")", this.duration);
  }
}



// Function that deals with adding captions to images

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function captionizeImages() {
  if (!document.getElementsByTagName) return false;
  if (!document.createElement) return false;
  var images = document.getElementsByTagName("img");
  if (images.length < 1) return false; 
  for (var i=0; i<images.length; i++) {
    if (images[i].className.indexOf("captioned") != -1) {
      var title = images[i].getAttribute("alt");
      var divCaption = document.createElement("div");
      divCaption.className="caption";
      var divCaption_text = document.createTextNode(title);
      divCaption.appendChild(divCaption_text);
      var divContainer = document.createElement("div");
      divContainer.className="imgcontainer";
      images[i].parentNode.insertBefore(divContainer,images[i]);
      divContainer.appendChild(images[i]);
      insertAfter(divCaption,images[i]);
    }
  }
}