/**************************************
COPYRIGHT 2009  Dennis Robertson. All Rights Reserved.
email: id@domain; (for 'id' use: webmaster  and for 'domain' use: denrocs.ml1.net)

Image Change
  Change images, with transitions

Depends on:
  data-types.js
  css.js
***************************************/


/**************************************/
function zdr_ImageChangeData (images, changeDelayArg, transitionTypeArg, transitionTimeArg) {
  if (images == undefined)  this.imgList = new Array();
  else if (images == null)  this.imgList = new Array();
  else if (images == '')  this.imgList = new Array();
  else if ( ! isArray(images))  this.imgList = new Array(images);
  else  this.imgList = images;
  if (changeDelayArg == undefined)  changeDelayArg = 0;
  this.changeDelay = changeDelayArg;
  if (transitionTypeArg == undefined)  transitionTypeArg = null;
  this.transitionType = transitionTypeArg;
  if (transitionTimeArg == undefined)  transitionTimeArg = 0;
  this.transitionTime = transitionTimeArg;
  }

/**************************************/
function zdr_ImageChange (imageElement, repeatArg) {
  var self = this;
  if (imageElement == undefined)  imageElement = null;
  var imgEl = imageElement;
  if (repeatArg == undefined)  repeatArg = false;
  var repeat = repeatArg;
  var chgList = new Array();
  var chgListIdx = 0;
  var imgListIdx = 0;

  this.addImages = function (images, changeDelayArg, transitionTypeArg, transitionTimeArg) {
    chgList.push(new zdr_ImageChangeData(images, changeDelayArg, transitionTypeArg, transitionTimeArg));
    }  // function this.addImages

  this.start = function () {
    if (imgEl == null)  return;
    if (chgList.length == 0)  return;
    var chgDelay = chgList[chgListIdx].changeDelay;
    if (chgDelay > 0)  window.setTimeout(change, chgDelay);
    else  change();
    }  // function this.start

  function change () {
    var chgDelay = chgList[chgListIdx].changeDelay;
    var transType = chgList[chgListIdx].transitionType;
    var transTime = chgList[chgListIdx].transitionTime;
    var nextImgList = chgList[chgListIdx].imgList;
    var nextImg;
    if (nextImgList.length == 0)  nextImg = '';
    else  nextImg = nextImgList[imgListIdx];
    if (transType == null)  imgEl.src = nextImg;
    else  transType(imgEl, nextImg, transTime);
    imgListIdx++;
    if (imgListIdx < nextImgList.length) {
      if (chgDelay > 0) {
        window.setTimeout(change, chgDelay);
        return;
        }
      else for ( ; imgListIdx < nextImgList.length; imgListIdx++) {
        imgEl.src = nextImgList[imgListIdx];
        }  // else while more images
      }  // if more images
    chgListIdx++;
    imgListIdx = 0;
    if (chgListIdx < chgList.length)  self.start();
    else {
      chgListIdx = 0;
      if (repeat)  self.start();
      }
    }  // function change
  }  // function zdr_ImageChange

/**************************************/

function zdr_ImageTrans_fade(imgElement, newImgUri, transTime) {
  // create duplicate img over imgElement
  var elements = document.getElementsByTagName('body');
  var elBody = elements[0];
  var newImgElement = document.createElement('img');
  newImgElement.src = imgElement.src;
  newImgElement.style.zIndex = '9';
  newImgElement.style.position = 'absolute';
  imgPos = document.getElementPosPage(imgElement);
  newImgElement.style.top = imgPos.y + 'px';
  newImgElement.style.left = imgPos.x + 'px';
  elBody.appendChild(newImgElement);
  // change imgElement to newImgUri
  imgElement.src = newImgUri;
  // fade-out old image (duplicate) and remove
  zdr_css_adjustOpacity(newImgElement, 1, 0, transTime, true);
  }  // zdr_ImageTrans_fade

/**************************************/

function zdr_ImageTrans_fadeOutIn(imgElement, newImgUri, transTime) {
  transTime = Math.round(transTime / 2);
  // fade-out current img
  zdr_css_adjustOpacity(imgElement, 1, 0, transTime, false);
  // setup for fade-in of newImgUri
  window.setTimeout(function () {
    imgElement.src = newImgUri;
    zdr_css_adjustOpacity(imgElement, 0, 1, transTime, false);
    },
    transTime);
  }  // zdr_ImageTrans_fadeOutIn
