/*************************************************************************
  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!
*************************************************************************/

/*************************************************************************
EXAMPLE OF HOW TO USE CODE
Simple "copy & paste" the the "script" section into your page where
the random-images are to be diplayed.

<script type="text/javascript">
  // images array, delay, width and height of images
  randImgObj.setUpImg(randImgObj.set1, 7000, 150, 150);
</script>
*************************************************************************/

// image file names go in these arrays
randImgObj.set1 = new Array("birthday_children_cakes/birthday_04_sm.gif","birthday_children_cakes/birthday_06_sm.gif","birthday_children_cakes/birthday_19_sm.gif");

// second set of rotating images
randImgObj.set2 = new Array("corporate_cakes/corporate_21_sm.gif","corporate_cakes/corporate_07_sm.gif","corporate_cakes/corporate_03_sm.gif");

// third set of rotating images
randImgObj.set3 = new Array("wedding_cakes/wedding_03_sm.gif","wedding_cakes/wedding_18_sm.gif","wedding_cakes/wedding_10_sm.gif","wedding_cakes/wedding_07_sm.gif","wedding_cakes/wedding_08_sm.gif");

// fourth set of rotating images
randImgObj.set4 = new Array("holiday_cakes/holiday_04_sm.gif","holiday_cakes/holiday_11_sm.gif","holiday_cakes/holiday_09_sm.gif");

// fifth set of rotating images
randImgObj.set5 = new Array("religious_cakes/religious_01_sm.gif","religious_cakes/religious_03_sm.gif","religious_cakes/religious_05_sm.gif");

// six set of rotating images
randImgObj.set6 = new Array("shower_cakes/showers_06_sm.gif","shower_cakes/showers_04_sm.gif","shower_cakes/showers_02_sm.gif");

// seventh set of rotating images
randImgObj.set7 = new Array("birthday_adults_cakes/birthday_adult_01_sm.gif","birthday_adults_cakes/birthday_adult_04_sm.gif","birthday_adults_cakes/birthday_adult_17_sm.gif");

// eighth set of rotating images
randImgObj.set8 = new Array("specialty_cakes/special_02_sm.gif","specialty_cakes/special_01_sm.gif","specialty_cakes/special_17_sm.gif","specialty_cakes/special_99_sm.gif");

// homepage set of rotating images
randImgObj.set99 = new Array("wedding_cakes/wedding_14_sm.gif","birthday_adults_cakes/birthday_adult_01_sm.gif","corporate_cakes/corporate_023_sm.jpg","birthday_children_cakes/birthday_15_sm.gif","shower_cakes/showers_06_sm.gif","specialty_cakes/special_18_sm.gif","holiday_cakes/holiday_04_sm.gif","corporate_cakes/corporate_07_sm.gif","wedding_cakes/wedding_10_sm.gif","specialty_cakes/special_028_sm.jpg","holiday_cakes/holiday_11_sm.gif","specialty_cakes/special_027_sm.jpg","holiday_cakes/holiday_02_sm.gif","wedding_cakes/wedding_15_sm.gif","corporate_cakes/corporate_022_sm.jpg");

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

// 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
 
function randImgObj(s) { // constructor
  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);                     
}

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 += '<img border="0" src="' + randImgObj.imagesPath + img + '" alt="" ';
  imgStr += 'name="img' + (randImgObjs.length-1) + '" width="' + w + '" height="' + h + '">';
  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();  
}