// JavaScript Document
/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2001-3 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

function initRotatePhotos() {
  // create rotating image objects here 
  // arguments: image name, rotation speed (milliseconds)
  var rotatorPhotos = new rotatePhotosObj('homePhotos',6500);
  // add the images to rotate into that image object
  rotatorPhotos.addImages("rotate_image.jpg", "rotate_image_01.jpg", "rotate_image_02.jpg", "rotate_image_03.jpg");
  // add the corresponding actions to take onclick of those images 
  // destination url or function pointer
  //rotatorPhotos.addActions("http://www.websiteone.com", "http://www.websitetwo.com", "MyPage.asp", "AnotherPage.aspx");
  rotatorPhotos.rotate();
  
  rotatePhotosObj.start();
}

// If all the images you wish to display are in the same location, you can specify the path here 
rotatePhotosObj.imagesPath = "/images/homePhotos/";
// if you want url's to open in a separate window, un-comment this line
rotatePhotosObj.tgt = "_blank"; 

// no need to edit code below 
/////////////////////////////////////////////////////////////////////
rotatePhotosObjs = []; // holds all rotating image objects defined
// constructor 
function rotatePhotosObj(nm,s) {
  this.speed=s; this.ctr=0; this.timer=0;  
  this.imgObj = document.images[nm]; // get reference to the image object
  this.index = rotatePhotosObjs.length; rotatePhotosObjs[this.index] = this;
  this.animString = "rotatePhotosObjs[" + this.index + "]";
}

rotatePhotosObj.prototype = {
  addImages: function() { // preloads images
    this.imgObj.imgs = [];
    for (var i=0; i<arguments.length; i++) {
      this.imgObj.imgs[i] = new Image();
      this.imgObj.imgs[i].src = rotatePhotosObj.imagesPath + arguments[i];
    }
  },
  
  addActions: function() {
    this.actions = [];
    for (var i=0; i<arguments.length; i++) { this.actions[i] = arguments[i]; }
  },
  
  rotate: function() {
    if (this.ctr < this.imgObj.imgs.length-1) this.ctr++;
    else this.ctr = 0;
    this.imgObj.src = this.imgObj.imgs[this.ctr].src;
  }
}

// sets up rotation for all defined rotatePhotosObjs
rotatePhotosObj.start = function() {
  for (var i=0; i<rotatePhotosObjs.length; i++) 
    rotatePhotosObjs[i].timer = setInterval(rotatePhotosObjs[i].animString + ".rotate()", rotatePhotosObjs[i].speed);                     
}

// called onclick of images
rotatePhotosObj.doClick = function(n) {
	if ( document.images && rotatePhotosObjs[n] ) {
    var obj = rotatePhotosObjs[n]; // shorten reference 
    if ( obj.actions && obj.actions[obj.ctr] ) {
  		if ( typeof obj.actions[obj.ctr] == "string" ) {
        if ( rotatePhotosObj.tgt == "_blank" ) {
          // open in separate window (add features here if you want, i.e., chrome, size, position, ...)
          var win = window.open(obj.actions[obj.ctr], "subwin");
          if ( win && !win.closed ) win.focus();
        } else window.location = obj.actions[obj.ctr];
      } else obj.actions[obj.ctr]();
    } 
    return false;
	} else return true;
}

// for stopping/starting onmouseover/out
rotatePhotosObj.pause = function(n) {	
  if (rotatePhotosObjs[n]) clearInterval(rotatePhotosObjs[n].timer); 
}

rotatePhotosObj.resume = function(n) {
  if ( rotatePhotosObjs[n] ) {
    var obj = rotatePhotosObjs[n]; // shorten reference 
    obj.rotate(); // rotate now and resume repeated calls
    obj.timer = setInterval( obj.animString + ".rotate()", obj.speed );
  }
}
