  //
  // CSS Photo Shuffler v1.0 by
  //   Carl Camera
  //   http://iamacamera.org 
  //

  // Mike Adams: this version simply fades in one pair of images
  // 4/3/2010

  // modified (crudely) by Mike Adams to support a slow ripple effect
  // 12/3/2009

  // modified (crudely) by Mike Adams to support more than one slideshow
  // 2/7/2008

  // modified (crudely) by Mike Adams to support just two image panels
  // 3/8/2009


  // SetOpacity Function and inpiration from Photo Fade by
  //   Richard Rutter
  //   http://clagnut.com
  //
  // License: Creative Commons Attribution 2.5  License
  //   http://creativecommons.org/licenses/by/2.5/
  //

  // Customize your photo shuffle settings
  // 
  // * Surround the target <img> with a <div>. specify id= in both
  // * set background-repeat:no-repeat in CSS for the div
  // * The first image displayed is in the html <img> tag
  // * The array contains paths to images you want in the rotation. 
  // * All photos must be the same size.
  // * The rotations variable specifies how many times to repeat array
  //   images. In this case it is default zero.

  var gblPhotoShufflerDivId1 = "photodiv1";
  var gblPhotoShufflerImgId1 = "photoimg1"; 
  var gblPhotoShufflerDivId2 = "photodiv2";
  var gblPhotoShufflerImgId2 = "photoimg2"; 

  var gblImg1 = new Array(
    "scans/main_1a.jpg?v=0"
    );
  var gblImg2 = new Array(
    "scans/main_4a.jpg?v=0"
    );

  // time parameters are same for all
  var gblStartDelaySeconds = 0.5;
  var gblPauseSeconds = 2.5;
  var gblFadeSeconds = 2.0;
  var gblRotations = 0;

  // End Customization section
  
  var gblDeckSize1 = gblImg1.length;
  var gblDeckSize2 = gblImg2.length;

  var gblOpacity1 = 100;
  var gblOpacity2 = 100;

  var gblOnDeck1 = 0;
  var gblOnDeck2 = 0;

  var gblFinalImg1;
  var gblFinalImg2;

  var gblImageRotations1 = gblDeckSize1 * (gblRotations+1);
  var gblImageRotations2 = gblDeckSize2 * (gblRotations+1);

  window.onload = photoShufflerLaunch;
  
  function photoShufflerLaunch()
  {
  	var theimg1 = document.getElementById(gblPhotoShufflerImgId1);
  	var theimg2 = document.getElementById(gblPhotoShufflerImgId2);

      gblFinalImg1 = gblImg1[0]; // set final image as first in array
      gblFinalImg2 = gblImg2[0]; // set final image as first in array
        
      var CycleTime = gblPauseSeconds;

	document.getElementById(gblPhotoShufflerDivId1).style.backgroundImage='url(' + gblImg1[gblOnDeck1] + ')';
	setTimeout("photoShufflerFade1()",gblStartDelaySeconds*1000);

	document.getElementById(gblPhotoShufflerDivId2).style.backgroundImage='url(' + gblImg2[gblOnDeck2] + ')';
	setTimeout("photoShufflerFade2()",gblStartDelaySeconds*1000 + CycleTime*1000);
  }

  function photoShufflerFade1()
  {
  	var theimg = document.getElementById(gblPhotoShufflerImgId1);
	
  	// determine delta based on number of fade seconds
	// the slower the fade the more increments needed
        var fadeDelta = 100 / (30 * gblFadeSeconds);

	// fade top out to reveal bottom image
	if (gblOpacity1 < 2*fadeDelta ) 
	{
	  gblOpacity1 = 100;
	  // stop the rotation if we're done
	  if (gblImageRotations1 < 1) return;
	  photoShufflerShuffle1();
	  // pause before next fade
        setTimeout("photoShufflerFade1()",(2*gblPauseSeconds-gblFadeSeconds)*1000);
	}
	else
	{
	  gblOpacity1 -= fadeDelta;
	  setOpacity(theimg,gblOpacity1);
	  setTimeout("photoShufflerFade1()",30);  // 1/30th of a second
	}
  }

  function photoShufflerFade2()
  {
  	var theimg = document.getElementById(gblPhotoShufflerImgId2);
	
  	// determine delta based on number of fade seconds
	// the slower the fade the more increments needed
        var fadeDelta = 100 / (30 * gblFadeSeconds);

	// fade top out to reveal bottom image
	if (gblOpacity2 < 2*fadeDelta ) 
	{
	  gblOpacity2 = 100;
	  // stop the rotation if we're done
	  if (gblImageRotations2 < 1) return;
	  photoShufflerShuffle2();
	  // pause before next fade
        setTimeout("photoShufflerFade2()",(2*gblPauseSeconds-gblFadeSeconds)*1000);
	}
	else
	{
	  gblOpacity2 -= fadeDelta;
	  setOpacity(theimg,gblOpacity2);
	  setTimeout("photoShufflerFade2()",30);  // 1/30th of a second
	}
  }

  function photoShufflerShuffle1()
  {
	var thediv = document.getElementById(gblPhotoShufflerDivId1);
	var theimg = document.getElementById(gblPhotoShufflerImgId1);
	
	// copy div background-image to img.src
	theimg.src = gblImg1[gblOnDeck1];
	// set img opacity to 100
	setOpacity(theimg,100);

        // shuffle the deck
	gblOnDeck1 = ++gblOnDeck1 % gblDeckSize1;
	// decrement rotation counter
	if (--gblImageRotations1 < 1)
	{
	  // insert final image if we're done
	  gblImg1[gblOnDeck1] = gblFinalImg1;
	}

	// slide next image underneath
	thediv.style.backgroundImage='url(' + gblImg1[gblOnDeck1] + ')';
  }

  function photoShufflerShuffle2()
  {
	var thediv = document.getElementById(gblPhotoShufflerDivId2);
	var theimg = document.getElementById(gblPhotoShufflerImgId2);
	
	// copy div background-image to img.src
	theimg.src = gblImg2[gblOnDeck2];
	// set img opacity to 100
	setOpacity(theimg,100);

      // shuffle the deck
	gblOnDeck2 = ++gblOnDeck2 % gblDeckSize2;
	// decrement rotation counter
	if (--gblImageRotations2 < 1)
	{
	  // insert final image if we're done
	  gblImg2[gblOnDeck2] = gblFinalImg2;
	}

	// slide next image underneath
	thediv.style.backgroundImage='url(' + gblImg2[gblOnDeck2] + ')';
  }

  function setOpacity(obj, opacity) {
    opacity = (opacity == 100)?99.999:opacity;
    
    // IE/Win
    obj.style.filter = "alpha(opacity:"+opacity+")";
    
    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity/100;

    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity/100;

    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;
  }

