/*************************************************************************
  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 initImgRotation() {
  // create rotating image objects here 
  // arguments: image name, rotation speed (milliseconds)
  var rotator1 = new rotateImgObj('img1',12000);
  // add the images to rotate into that image object
  rotator1.addImages("1907.jpg", "1880.jpg", "2489.jpg", "3158.jpg", "3965.jpg");
  // add the corresponding actions to take onclick of those images 
  // (last one shows how to do nothing onclick)
  rotator1.addActions("http://www.dramatists.com/cgi-bin/db/single.asp?key=1907", "http://www.dramatists.com/cgi-bin/db/single.asp?key=1880", "http://www.dramatists.com/cgi-bin/db/single.asp?key=2489", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3158", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3965");
  rotator1.rotate();
  
  var rotator2 = new rotateImgObj('img2', 12000);
  rotator2.addImages("1892.jpg", "1904.jpg", "3788.jpg", "3117.jpg", "3729.jpg", "4097.jpg");
  // demonstrates how to call another function onclick
  rotator2.addActions("http://www.dramatists.com/cgi-bin/db/single.asp?key=1892", "http://www.dramatists.com/cgi-bin/db/single.asp?key=1904", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3788", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3117", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3729", "http://www.dramatists.com/cgi-bin/db/single.asp?key=4097");  
  rotator2.rotate();
  
  var rotator3 = new rotateImgObj('img3', 12000);
  rotator3.addImages("1890.jpg", "1881.jpg", "3013.jpg", "3116.jpg", "3968.jpg");
  // demonstrates how to call another function onclick
  rotator3.addActions("http://www.dramatists.com/cgi-bin/db/single.asp?key=1890", "http://www.dramatists.com/cgi-bin/db/single.asp?key=1881", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3013", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3116", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3968");  
  rotator3.rotate();
  
  var rotator4 = new rotateImgObj('img4', 12000);
  rotator4.addImages("3267.jpg", "1905.jpg", "3132.jpg", "3775.jpg", "3230.jpg");
  // demonstrates how to call another function onclick
  rotator4.addActions("http://www.dramatists.com/cgi-bin/db/single.asp?key=3267", "http://www.dramatists.com/cgi-bin/db/single.asp?key=1905", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3132", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3775", "http://www.dramatists.com/cgi-bin/db/single.asp?key=3230");  
  rotator4.rotate();
  
  rotateImgObj.start();  
}

// for example function call onclick 
// This function demonstrates how to obtain some of the information available about the image clicked on 
function doMsg() { 
  var obj = rotateImgObjs[1]; // refers to 2nd rotateImgObj (i.e., rotator2)
  var msg = "You clicked on " + obj.imgObj.src;
  msg += ". It's height is " + obj.imgObj.imgs[obj.ctr].height + " pixels.";
  alert(msg);
}

// If all the images you wish to display are in the same location, you can specify the path here 
rotateImgObj.imagesPath = "http://www.dramatists.com/images/header/";
// if you want url's to open in separate window, un-comment this line
//rotateImgObj.tgt = "_blank"; 

// no need to edit code below 
/////////////////////////////////////////////////////////////////////
Array.prototype.shuffle = function() { 
  var i, temp, i1, i2;
  for (i=0; i<this.length; i++) { 
    i1 = Math.floor( Math.random() * this.length );
    i2 = Math.floor( Math.random() * this.length );
    temp = this[i1];
    this[i1] = this[i2];
    this[i2] = temp;
  }
}

rotateImgObjs = []; // holds all rotating image objects defined
// constructor 
function rotateImgObj(nm,s) {
  this.speed=s; this.ctr=0; this.timer=0;  
  this.imgObj = document.images[nm]; // get reference to the image object
  this.index = rotateImgObjs.length; rotateImgObjs[this.index] = this;
  this.animString = "rotateImgObjs[" + this.index + "]";
  }

rotateImgObj.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 = rotateImgObj.imagesPath + arguments[i]; 
    }
  },
  
  addActions: function() {
    this.actions = [];
    for (var i=0; i<arguments.length; i++) { this.actions[i] = arguments[i]; }
  },
  
 rotate: function() { // controls rotation
    var ctr = Math.floor( Math.random() * this.imgObj.imgs.length );
    if (ctr == this.ctr) ctr = (ctr > 0)? --ctr: ++ctr;
    this.ctr = ctr;
    if ( typeof this.imgObj.filters != "undefined" ) {
   		this.imgObj.style.filter = 'blendTrans(duration=2)';
      if (this.imgObj.filters.blendTrans) this.imgObj.filters.blendTrans.Apply();
    }
    this.imgObj.src = this.imgObj.imgs[this.ctr].src;
    if ( typeof this.imgObj.filters != "undefined" && this.imgObj.filters.blendTrans )
      this.imgObj.filters.blendTrans.Play();    
  }
}


// sets up rotation for all defined rotateImgObjs
rotateImgObj.start = function() {
 for (var i=0; i<rotateImgObjs.length; i++) 
 rotateImgObjs[i].timer = setInterval(rotateImgObjs[i].animString + ".rotate()", rotateImgObjs[i].speed);                     
}

rotateImgObj.setUpImg = function(imgAr, sp, w, h) {
  var rotator, img, imgStr = "";
  rotator = new rotateImgObj(sp);
  rotateImgObjs[rotateImgObjs.length-1].imgAr = imgAr;
  imgAr.shuffle();
  img = imgAr[ Math.floor( Math.random() * imgAr.length ) ]; 
  imgStr += '<img src="' + rotateImgObj.imagesPath + img + '" alt="" ';
  imgStr += 'name="img' + (rotateImgObjs.length-1) + '" width="' + w + '" height="' + h + '">';
  document.write(imgStr); 
}

// called onclick of images
rotateImgObj.doClick = function(n) {
	if ( document.images && rotateImgObjs[n] ) {
    var obj = rotateImgObjs[n]; // shorten reference 
    if ( obj.actions && obj.actions[obj.ctr] ) {
  		if ( typeof obj.actions[obj.ctr] == "string" ) {
        if ( rotateImgObj.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
//rotateImgObj.pause = function(n) {	
 // if (rotateImgObjs[n]) clearInterval(rotateImgObjs[n].timer); 
//}

//rotateImgObj.resume = function(n) {
//  if ( rotateImgObjs[n] ) {
 //   var obj = rotateImgObjs[n]; // shorten reference 
//    obj.rotate(); // rotate now and resume repeated calls
//    obj.timer = setInterval( obj.animString + ".rotate()", obj.speed );
//  }
//}