  var CommercialImages=new Array(
    "photos/commercial_rotate_1.jpg",
    "photos/commercial_rotate_2.jpg",
    "photos/commercial_rotate_3.jpg",
    "photos/commercial_rotate_4.jpg"
  );

  var gblCommercialDivId;
  var gblCommercialImgId;
  var gblComPauseSeconds = 5;
  var gblComFadeSeconds = .5;
  var gblComRotations = 1;

  var gblComDeckSize = CommercialImages.length;
  var gblComOpacity = 100;
  var gblComOnDeck = 0;
  var gblComStartImg;
  var gblComImageRotations = gblComDeckSize * (gblComRotations+1);

  CommercialLaunch("commercial_images", "commercialimg");
  
  function CommercialLaunch(divId, imgId)
  {
        gblCommercialDivId = divId;
        gblCommercialImgId = imgId;
        document.write("<img border='0' id='" + imgId + "' name='" + imgId + "' src='"+CommercialImages[gblComDeckSize - 1]+"'/>");

  	var theimg = document.getElementById(gblCommercialImgId);
        gblComStartImg = theimg.src; // save away to show as final image

	document.getElementById(gblCommercialDivId).style.backgroundImage='url(' + CommercialImages[gblComOnDeck] + ')';
	setTimeout("CommercialFade()",gblComPauseSeconds*1000);
  }

  function CommercialFade()
  {
  	var theimg = document.getElementById(gblCommercialImgId);
	
  	// determine delta based on number of fade seconds
	// the slower the fade the more increments needed
        var fadeDelta = 100 / (30 * gblComFadeSeconds);

	// fade top out to reveal bottom image
	if (gblComOpacity < 2*fadeDelta ) 
	{
	  gblComOpacity = 100;
	  // stop the rotation if we're done
	  if (gblComImageRotations < 1) return;
	  CommercialShuffle();
	  // pause before next fade
          setTimeout("CommercialFade()",gblComPauseSeconds*1000);
	}
	else
	{
	  gblComOpacity -= fadeDelta;
	  setOpacity(theimg,gblComOpacity);
	  setTimeout("CommercialFade()",30);  // 1/30th of a second
	}
  }

  function CommercialShuffle()
  {
	var thediv = document.getElementById(gblCommercialDivId);
	var theimg = document.getElementById(gblCommercialImgId);
	
	// copy div background-image to img.src
	theimg.src = CommercialImages[gblComOnDeck];
	// set img opacity to 100
	setOpacity(theimg,100);

        // shuffle the deck
	gblComOnDeck = ++gblComOnDeck % gblComDeckSize;
	// decrement rotation counter
	if (gblComImageRotations < 1)
	{
	  // insert start/final image if we're done
	  CommercialImages[gblComOnDeck] = gblComStartImg;
	}

	// slide next image underneath
	thediv.style.backgroundImage='url(' + CommercialImages[gblComOnDeck] + ')';
  }
