/*****
Modified by jmorelli@auragen.com
Parts of this script taken and modified from:

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact.

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html

*****/


window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, imgs = new Array(), zInterval = null, pause=false;

var nIndex = 0;
var current = 0;
var showtime = "";
var inTransition = false;
var userClicked = false;

var imageRotationSpeed=5000; // 1000 = ~1 second

function clickedSplash(splashPositionNew){
  userClicked = true;
  changeSplash(splashPositionNew);
}

function so_init() {
	if(!d.getElementById || !d.createElement)return;

	css = d.createElement("link");
	css.setAttribute("href","/wcsstore/ConsumerDirectStorefrontAssetStore/css/xFade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);

	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;

  slideshow();
}

function changeSplash(splashPositionNew) {
  if(!inTransition) {
    clearTimeout(showtime);
    if (splashPositionNew != current) {
      //alert("current="+current+" nIndex="+nIndex+" splashPositionNew="+splashPositionNew);
      document.getElementById("splash-"+splashPositionNew).style.display="inline";
      document.getElementById("splash-"+current).style.display="none";
      document.getElementById("splash-selector-"+splashPositionNew).className="splash-selector-"+splashPositionNew+"-over";
      document.getElementById("splash-selector-"+current).className="splash-selector-"+current;
      nIndex = splashPositionNew;
      so_xfade();
    } else {
      slideshow();
    }
  }
}

function so_xfade() {
	cOpacity = imgs[current].xOpacity;
	nOpacity = imgs[nIndex].xOpacity;

	cOpacity-=.15;
	nOpacity+=.15;

	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;

	setOpacity(imgs[current]);
	setOpacity(imgs[nIndex]);

	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		current = nIndex;
    if(!userClicked) slideshow();
    inTransition = false;
	} else {
    inTransition = true;
		setTimeout(so_xfade,50);
	}

	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}

}

function slideshow() {
  nIndex = imgs[current+1]?current+1:0;
  showtime = setTimeout("changeSplash("+nIndex+")",imageRotationSpeed);
}
