// JavaScript Document
function initImgSwap() {
  var swapImage1 = new swapImgObj('imgPortfolio',2000);
  // add the image names to swap
  swapImage1.addImages("portfolio_bestface_forward_makeup.jpg","cheeky-monkey-photography.jpg","maash-website2.jpg", "david-bailey-panoramic-photography.jpg", "epicpromo-website.jpg", "lisabailey-website.jpg", "kc-turner-engineering.jpg", "security-cameras-website.jpg", "the-aquarian-moon-candle-studio.jpg", "bushfire-hazard-solutions.jpg", "onlinelighting-website.jpg", "scentbox-website.jpg", "ifpix-website.jpg", "aprons-down-under-website.jpg");
  swapImage1.swap();  
  swapImgObj.start();
}

// Image location
swapImgObj.imagesPath = "images/";

swapImgObjs = []; 
// constructor 
function swapImgObj(nm,s) {
  this.speed=s; this.ctr=0; this.timer=0;  
  this.imgObj = document.images[nm]; 
  this.index = swapImgObjs.length; swapImgObjs[this.index] = this;
  this.animString = "swapImgObjs[" + this.index + "]";
}

swapImgObj.prototype = {
  addImages: function() { // preloads images
    this.imgObj.imgs = [];
    for (var i=0; arguments[i]; i++) {
      this.imgObj.imgs[i] = new Image();
      this.imgObj.imgs[i].src = swapImgObj.imagesPath + arguments[i];
    }
  },

  swap: 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 Swap for all defined swapImgObjs
swapImgObj.start = function() {
  for (var i=0; i<swapImgObjs.length; i++) 
    swapImgObjs[i].timer = setInterval(swapImgObjs[i].animString + ".swap()", swapImgObjs[i].speed);                     
}