  //
  // CSS Photo Shuffler v1.0 by
  //   Carl Camera
  //   http://iamacamera.org 

  var gblPhotoShufflerDivId = "photodiv";
  var gblPhotoShufflerImgId = "photoimg"; 
  var gblImg = null;	//created later
  var gblPauseSeconds = 4;
  var gblFadeSeconds = 1;
  var gblRotations = 99999;

  // End Customization section
  
  var gblDeckSize = null;
  var gblOpacity = 100;
  var gblOnDeck = 0;
  var gblStartImg = null;
  var gblImageRotations = null;

  Array.prototype.shuffle = arrayShuffle;

  window.onload = photoShufflerPrepare;
  


  // from brain4.de
  function arrayShuffle(){
    var tmp, rand;
    for(var i =0; i < this.length; i++){
      rand = Math.floor(Math.random() * this.length);
      tmp = this[i]; 
      this[i] = this[rand]; 
      this[rand] =tmp;
    }
  }



  function photoShufflerPrepare()
  {
	// fetch header images
	var array = new Array();
	var data = document.getElementsByName("headerImage");
	for (var i = 0; i < data.length; i++) {
		array[i] = data[i].value;
		
	}
	array.shuffle();
	gblImg = array;
	gblDeckSize = array.length;
	gblImageRotations = gblDeckSize * (gblRotations+1);

	//go
	photoShufflerLaunch();
  }

  function photoShufflerLaunch()
  {
  	var theimg = document.getElementById(gblPhotoShufflerImgId);
        gblStartImg = theimg.src;
	document.getElementById(gblPhotoShufflerDivId).style.backgroundImage='url(' + gblImg[gblOnDeck] + ')';
	setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
  }

  function photoShufflerFade()
  {
  	var theimg = document.getElementById(gblPhotoShufflerImgId)
        var fadeDelta = 100 / (30 * gblFadeSeconds);
	if (gblOpacity < 2*fadeDelta ) 
	{
	  gblOpacity = 100;
	  if (gblImageRotations < 1) return;
	  photoShufflerShuffle();
          setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
	}
	else
	{
	  gblOpacity -= fadeDelta;
	  setOpacity(theimg,gblOpacity);
	  setTimeout("photoShufflerFade()",30); 
	}
  }

  function photoShufflerShuffle()
  {
	var thediv = document.getElementById(gblPhotoShufflerDivId);
	var theimg = document.getElementById(gblPhotoShufflerImgId);
	theimg.src = gblImg[gblOnDeck];
	setOpacity(theimg,100);
	gblOnDeck = ++gblOnDeck % gblDeckSize;
	if (--gblImageRotations < 1)
	{
	  gblImg[gblOnDeck] = gblStartImg;
	}
	thediv.style.backgroundImage='url(' + gblImg[gblOnDeck] + ')';
  }

  function setOpacity(obj, opacity) {
    opacity = (opacity == 100)?99.999:opacity;
    obj.style.filter = "alpha(opacity:"+opacity+")";
    obj.style.KHTMLOpacity = opacity/100;
    obj.style.MozOpacity = opacity/100;
    obj.style.opacity = opacity/100;
  }
