// Set up the image files to be used.
//var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

function initImgRotation() {
  // create rotating image objects here 
  // arguments: image name, rotation speed (milliseconds)
  var rotator1 = new rotateImgObj('img1',2000);
  // add the images to rotate into that image object
  rotator1.addImages("plimage1.jpg","plimage2.jpg","plimage3.jpg","plimage4.jpg","plimage5.jpg");
  // add the corresponding actions to take onclick of those images 
  // destination url or function pointer
  rotator1.addActions("#", "#", "#","#",doMsg );
  rotator1.rotate();
  
  rotateImgObj.start();
}

// for example function call onclick 
// This function demonstrates how to obtain some of the information available about the rotateImgObj clicked on 
function doMsg() { 
  var obj = rotateImgObjs[0];
  var msg = "You clicked on " + obj.imgObj.src;
  msg += ",\nnumber " + Number(obj.ctr+1) + " in the set. ";
  msg += "It's height is " + obj.imgObj.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 = "images/";
// if you want url's to open in a separate window, un-comment this line
//rotateImgObj.tgt = "_blank"; 

// no need to edit code below 
/////////////////////////////////////////////////////////////////////
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() {
    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 rotateImgObjs
rotateImgObj.start = function() {
  for (var i=0; i<rotateImgObjs.length; i++) 
    rotateImgObjs[i].timer = setInterval(rotateImgObjs[i].animString + ".rotate()", rotateImgObjs[i].speed);                     
	
}

// 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 );
  }
}
//**********************************************************************************************
// image file names go in these arrays
randImgObj.set1 = new Array("dc_img1.gif","dc_img2.gif","dc_img3.gif","dc_img4.gif");

// If all the images you wish to display are in the same location, you can specify the path here 
randImgObj.imagesPath = "../images/";

// No need to edit code below this line 
/////////////////////////////////////////////////////////////////////
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;
  }
}

randImgObjs = []; // holds all random rotating image objects defined
// constructor 
function randImgObj(s) {
  this.speed=s; this.ctr=0; this.timer=0;  
  this.index = randImgObjs.length; randImgObjs[this.index] = this;
  this.animString = "randImgObjs[" + this.index + "]";
}

randImgObj.prototype = {
  addImages: function(ar) { // preloads images
    this.imgObj.imgs = [];
    for (var i=0; ar[i]; i++) {
      this.imgObj.imgs[i] = new Image();
      this.imgObj.imgs[i].src = randImgObj.imagesPath + ar[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=1)';
      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 randImgObjs
randImgObj.start = function() {
  for (var i=0; i<randImgObjs.length; i++) 
  {
    randImgObjs[i].timer = setInterval(randImgObjs[i].animString + ".rotate()", randImgObjs[i].speed);                     
	//alert(randImgObjs[i].speed);
  }
}

randImgObj.setUpImg = function(imgAr, sp, w, h) {
  var rotator, img, imgStr = "";
  rotator = new randImgObj(sp);
  randImgObjs[randImgObjs.length-1].imgAr = imgAr;
  imgAr.shuffle();
  
  img = imgAr[ Math.floor( Math.random() * imgAr.length ) ]; 
  imgStr += '<a href="#"><img border=0 src="' + randImgObj.imagesPath + img + '" alt="" ';
  imgStr += 'name="img' + (randImgObjs.length-1) + '" width="' + w + '" height="' + h + '"></a>';
  document.write(imgStr); 
}

function initRandRotation() 
{
  for (var i=0; randImgObjs[i]; i++) 
  {
    var rotator = randImgObjs[i];
    rotator.imgObj = document.images["img" + i]; // get reference to the image object
    rotator.addImages(rotator.imgAr);
    rotator.rotate();
  }
  randImgObj.start();  
}
