﻿  //
  // CSS Photo Shuffler v1.0 by
  //   Carl Camera
  //   http://iamacamera.org 
  //
  // 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/
  //
  
  // Modifications by
  //   Sam Fish
  //   http://www.sjfweb.net
  //   Includes 2nd set of slideshow to simultaneously shuffle with the main one.
  //   Also wrote a randomization which will furthur shuffle the order in which the images
  //   appear.

  // 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 and final photo displayed is in the html <img> tag
  // * The array contains paths to photos you want in the rotation. 
  //   If you want the first photo in the rotation, then it's best to
  //   put it as the final array image.  All photos must be same dimension
  // * The rotations variable specifies how many times to repeat array.
  //   images. zero is a valid rotation value.

  var gblPhotoShufflerDivId = "photodiv";
  var gblPhotoShufflerImgId = "photoimg";
  var gblPhotoShufflerDivId2 = "photodiv_bl";
  var gblPhotoShufflerImgId2 = "photoimg_bl";

//  var gblImg = new Array(
//    "img/slide_01.jpg",
//    "img/slide_02.jpg",
//    "img/slide_03.jpg",
//    "img/slide_04.jpg",
//    "img/slide_05.jpg",
//    "img/slide_06.jpg",
//    "img/slide_07.jpg",
//    "img/slide_08.jpg",
//    "img/slide_09.jpg",
//    "img/slide_11.jpg",
//    "img/slide_11.jpg",
//    "img/slide_12.jpg"
//    );

  var gblImg = new Array(
    "img/banners/ban10.png",
    "img/banners/ban01.png",
    "img/banners/ban02.png",
    "img/banners/ban03.png",
    "img/banners/ban04.png",
    "img/banners/ban05.png",
    "img/banners/ban06.png",
    "img/banners/ban07.png",
    "img/banners/ban08.png",
    "img/banners/ban09.png"
    );

  var gblImg2 = new Array(
    "img/botL_01.jpg",
    "img/botL_02.jpg",
    "img/botL_03.jpg",
    "img/botL_04.jpg",
    "img/botL_05.jpg",
    "img/botL_06.jpg",
    "img/botL_07.jpg",
    "img/botL_08.jpg",
    "img/botL_09.jpg",
    "img/botL_10.jpg"
    );
//  var gblPauseSeconds = 4.25;
  var gblPauseSeconds = 10.25;
  var gblFadeSeconds = 1.10;
  var gblRotations = 50;

  // End Customization section
  
  var gblDeckSize = gblImg.length;
  var gblOpacity = 100;
  var gblOnDeck = 0;
  var gblStartImg;
  var gblStartImg2;
  var gblImageRotations = gblDeckSize * (gblRotations+1);
  
  // Shuffle Image Array
  for (i=0; i<gblImg.length; i++) {
    pos = Math.floor(Math.random()*(gblImg.length));
    temp = gblImg[i];
    gblImg[i] = gblImg[pos];
    gblImg[pos] = temp;
  }
  for (i=0; i<gblImg2.length; i++) {
    pos = Math.floor(Math.random()*(gblImg2.length));
    temp = gblImg2[i];
    gblImg2[i] = gblImg2[pos];
    gblImg2[pos] = temp;
  }
  
  window.onload = photoShufflerLaunch;

  function photoShufflerLaunch()
  {
    var theimg = document.getElementById(gblPhotoShufflerImgId);
    var theimg2 = document.getElementById(gblPhotoShufflerImgId2);
    var init1 = document.getElementById(gblPhotoShufflerDivId);
    var init2 = document.getElementById(gblPhotoShufflerDivId2);
    init1.style.backgroundImage='url(' + gblImg[0] + ')';
    init2.style.backgroundImage='url(' + gblImg2[0] + ')';

    gblStartImg = theimg.src; // save away to show as final image
    gblStartImg2 = theimg2.src; // save away to show as final image

    setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
  }

  function photoShufflerFade()
  {
  	var theimg = document.getElementById(gblPhotoShufflerImgId);
  	var theimg2 = 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 (gblOpacity < 2*fadeDelta ) 
	{
	  gblOpacity = 100;
	  // stop the rotation if we're done
	  if (gblImageRotations < 1) return;
	  photoShufflerShuffle();
	  // pause before next fade
          setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
	}
	else
	{
	  gblOpacity -= fadeDelta;
	  setOpacity(theimg,gblOpacity);
	  setOpacity(theimg2,gblOpacity);
//	  setTimeout("setOpacity(theimg2,gblOpacity)", 7000);
	  setTimeout("photoShufflerFade()",30);  // 1/30th of a second
	}
  }

  function photoShufflerShuffle()
  {
	var thediv = document.getElementById(gblPhotoShufflerDivId);
	var thediv2 = document.getElementById(gblPhotoShufflerDivId2);
	var theimg = document.getElementById(gblPhotoShufflerImgId);
	var theimg2 = document.getElementById(gblPhotoShufflerImgId2);
	
	if (theimg.src == gblImg[gblOnDeck]) { gblOnDeck = ((gblOnDeck+1)%gblImg.length); }
	if (theimg2.src == gblImg2[gblOnDeck]) { gblOnDeck = ((gblOnDeck+1)%gblImg2.length); }
	
	// copy div background-image to img.src
	theimg.src = gblImg[gblOnDeck];
	theimg2.src = gblImg2[gblOnDeck];
	// set img opacity to 100
	setOpacity(theimg,100);
	setOpacity(theimg2,100);

  // shuffle the deck
	gblOnDeck = ++gblOnDeck % gblDeckSize;
	// decrement rotation counter
	if (--gblImageRotations < 1)
	{
	  // insert start/final image if we're done
	  gblImg[gblOnDeck] = gblStartImg;
	  gblImg2[gblOnDeck] = gblStartImg2;
	}

	// slide next image underneath
	thediv.style.backgroundImage='url(' + gblImg[gblOnDeck] + ')';
	thediv2.style.backgroundImage='url(' + gblImg2[gblOnDeck] + ')';
  }

  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;
  }