/* 

Unobtrusive Javascript file for fading slideshow V1.0
Requires: global.js (must be loaded first!)

(CC) 2006 Mark Perkins. Some Rights Reserved.
Contact: mark@perkwebdesign.com

This script is licenced under the Creative Commons 
Attribution-ShareAlike 2.5 License. 
To view a copy of this licence, visit 
http://creativecommons.org/licenses/by-sa/2.5/

*/

/* CHANGE THESE VARIABLES
===================================================================*/

var timer = 5000;		// pause between images in millis- should be about 5000 longer than you would think to account for image fade time
var fps = 8;			// frames per second for the fade transition
var randomize = true;	// make false if you don't want random order of transitions

/* first the 'fade' functions...
===================================================================*/

var fps = 10;

function fade(img, time, dir){
  var steps = time * fps;
  if (typeof img.style.opacity != 'undefined') {
    var otype = 'w3c';
  } else if (typeof img.style.MozOpacity != 'undefined'){
    otype = 'moz';
  } else if (typeof img.style.KhtmlOpacity != 'undefined') {
    otype = 'khtml';
  } else if (typeof img.filters == 'object') {
    otype = (img.filters.length > 0
        && typeof img.filters.alpha == 'object'
        && typeof img.filters.alpha.opacity == 'number')
        ? 'ie' : 'none';
  } else { otype = 'none'; }
	if (otype != 'none') {
    if (dir == 'out') { dofade(steps, img, 1, false, otype); }
    else { dofade(steps, img, 0, true, otype); }
  }
}

function dofade(steps, img, value, targetvisibility, otype) {
  value += (targetvisibility ? 1 : -1) / steps;
  if (targetvisibility ? value > 1 : value < 0)
      value = targetvisibility ? 1 : 0;
  setfade(img, value, otype);
  if (targetvisibility ? value < 1 : value > 0) {
    setTimeout(function() {
      dofade(steps, img, value, targetvisibility, otype);
    }, 1000 / fps);
  }
}

function setfade(img, value, otype) {
  switch(otype) {
    case 'ie':
      img.filters.alpha.opacity = value * 100;
      break;
	case 'khtml':
      img.style.KhtmlOpacity = value;
      break;
    case 'moz':
      img.style.MozOpacity = (value == 1 ? 0.9999999 : value);
      break;
    default:
      img.style.opacity = (value == 1 ? 0.9999999 : value);
  }
}

/* functions to bring it all together....
===================================================================*/

// initswap function gets list of all the pics (or anything else(!) with the class 'swappic'
// note all pics must have additional class 'hidden' (ie class="swappic hidden") to begin with to make them invisible.

var picarray;
var len;

function initSwap() {
	visipics = getElementsByAttribute('class', 'swappic visible'); // get array of all visible swappics
	picarray = getElementsByAttribute('class', 'swappic hidden'); // get array of all swappics
	if (randomize) {
		picarray = picarray.sort(compare);  // randomize array
	}
	picarray = visipics.concat(picarray);
	len = picarray.length -1;
	setTimeout(function()
	{
	  doSwap(0, 1);
	}, 1000);
}

function doSwap(count, nextcount) {
	if (count > len) {count = 0;}
	fade(picarray[count], 5, 'out');
	fade(picarray[nextcount], 5, 'in');
	if (nextcount == len) {
		count = len;
		nextcount = 0;
		setTimeout(function()
		{
		  doSwap(count, nextcount);
		}, timer);
		
	} else {
		count++;
		nextcount++;
		setTimeout(function()
		{
		  doSwap(count, nextcount);
		}, timer);
	}
}

addLoadListener(function()
{
 initSwap();
});
