// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// ******************************************************
// global object
var isf = { 'clock' : null, 'count' : 1, 'duration' : 2, 'currImg' : 0, 'nextImg' : 1 }
/*******************************************************

/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

 isf.imgs = [
  'hover-17.jpg',
  'hover-1.jpg',
  'hover-2.jpg',
  'hover-3.jpg',
  'hover-4.jpg',
  'hover-20.jpg',
  'hover-6.jpg',
  'hover-7.jpg',
  'hover-8.jpg',
  'hover-19.jpg',
  'hover-10.jpg',
  'hover-11.jpg',
  'hover-12.jpg',
  'hover-13.jpg',
  'hover-14.jpg',
  'hover-19.jpg',
  'hover-16.jpg', 
  'hover-18.jpg',
  'hover-9.jpg',
  'hover-5.jpg',
  'hover-15.jpg',
];

/*****************************************************************************
*****************************************************************************/

// cache the images
isf.imgsLen = isf.imgs.length;
isf.cache = [];
for(var i=0; i<isf.imgsLen; i++) {
  isf.cache[i] = new Image;
  isf.cache[i].src = isf.imgs[i];
}

// swapfade setup function
function swapfade() {
  //if the timer is not already going
  if (isf.clock == null) {
    isf.obj = new Array();
    // copy the image objects
    for (var i=0; i<isf.imgsLen; i++) {
      isf.obj[i] = document.getElementById('isf'+i);
    }
		
    // store the supported form of opacity
    if (typeof isf.obj[0].style.opacity != 'undefined') {
      isf.type = 'w3c';
    } else if (typeof isf.obj[0].style.MozOpacity != 'undefined') {
      isf.type = 'moz';
    } else if (typeof isf.obj[0].style.KhtmlOpacity != 'undefined') {
      isf.type = 'khtml';
    } else if (typeof isf.obj[0].filters == 'object') {
      // weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
      // then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
      // then the returned value type, which should be a number, but in mac/ie5 is an empty string
      isf.type = (isf.obj[0].filters.length > 0 && typeof isf.obj[0].filters.alpha == 'object' && typeof isf.obj[0].filters.alpha.opacity == 'number') ? 'ie' : 'none';
    } else {
      isf.type = 'none';
    }
		
    // if any kind of opacity is supported
    if (isf.type != 'none') {
      // copy and convert fade duration argument
      isf.length = isf.duration * 500;
      isf.resolution = isf.duration * 10;

      //start the timer
      isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
    } else {
      // otherwise if opacity is not supported
      // just do the image swap
      isf.obj[isf.currImg].style.zIndex = -1;
      isf.obj[isf.nextImg].style.zIndex = 2;
      isf.currImg = isf.nextImg;
      if (++isf.nextImg >= isf.imgsLen) isf.nextImg = 0;
      isf.obj[isf.nextImg].style.zIndex = 1;
      switch (isf.type) {
        case 'ie':
          isf.obj[isf.nextImg].filters.alpha.opacity = 0;
          break;
        case 'khtml':
          isf.obj[isf.nextImg].style.KhtmlOpacity = 0;
          break;
        case 'moz':
          isf.obj[isf.nextImg].style.MozOpacity = 0;
          break;
        default:
          isf.obj[isf.nextImg].style.opacity = 0;
      }
    }
  }
};


// swapfade timer function
isf.swapfade = function() {
  // reduce counter
  isf.count = Math.round((isf.count-(1/(isf.duration*10)))*100)/100;

  // set new opacity value on elements

  // using whatever method is supported
  switch (isf.type) {
    case 'ie':
      isf.obj[isf.currImg].filters.alpha.opacity = isf.count * 100;
      isf.obj[isf.nextImg].filters.alpha.opacity = 100-isf.obj[isf.currImg].filters.alpha.opacity;
      break;
    case 'khtml':
      isf.obj[isf.currImg].style.KhtmlOpacity = isf.count;
      isf.obj[isf.nextImg].style.KhtmlOpacity = (1-isf.obj[isf.currImg].style.KhtmlOpacity);
      break;
    case 'moz':
      // restrict max opacity to prevent a visual popping effect in firefox
      isf.obj[isf.currImg].style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
      isf.obj[isf.nextImg].style.MozOpacity = 1-isf.obj[isf.currImg].style.MozOpacity;
      break;
    default:
      // restrict max opacity to prevent a visual popping effect in firefox
      isf.obj[isf.currImg].style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
      isf.obj[isf.nextImg].style.opacity = 1-isf.obj[isf.currImg].style.opacity;
      break;
  }

  // if the counter has reached the bottom
  if (isf.count <= 0) {
    // clear the timer
    clearInterval(isf.clock);
    isf.clock = null;

    // do the image swap
    isf.obj[isf.currImg].style.zIndex = -1;
    isf.obj[isf.nextImg].style.zIndex = 2;
    isf.currImg = isf.nextImg;
    if (++isf.nextImg >= isf.imgsLen) isf.nextImg = 0;
    isf.obj[isf.nextImg].style.zIndex = 1;
    switch (isf.type) {
      case 'ie':
        isf.obj[isf.nextImg].filters.alpha.opacity = 0;
        break;
      case 'khtml':
        isf.obj[isf.nextImg].style.KhtmlOpacity = 0;
        break;
      case 'moz':
        isf.obj[isf.nextImg].style.MozOpacity = 0;
        break;
      default:
        isf.obj[isf.nextImg].style.opacity = 0;
    }

    // wait a bit before retarting
    isf.clock = setTimeout('isf.restartClock()', 10000);
  }
};

// pause function
isf.restartClock = function() {
  // reset count
  isf.count = 1;

  // restart the timer
  isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
}