/*****

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

modified by Jeff Fohl on March 29, 2007

added buttons and ability to exit slide show loop

*****/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, slides = new Array(), zInterval = null, current=0, pause=false, nextSlideIncrement = null, slideIncrement = null, fadeIncrement = null;

function so_init() {
	slides = d.getElementById("photoFader-contentcontainer").getElementsByTagName("div");
	for(i=1;i<slides.length;i++)
	slides[i].xOpacity = 0;
	slides[0].style.display = "block";
	slides[0].xOpacity = .99;
	slideIncrement = setTimeout(so_xfade,5000);
}

function so_xfade() {
	cOpacity = slides[current].xOpacity;
	nIndex = slides[current+1]?current+1:0;
	nOpacity = slides[nIndex].xOpacity;
	
	cOpacity-=.05;
	nOpacity+=.05;
	
	slides[nIndex].style.display = "block";
	slides[current].xOpacity = cOpacity;
	slides[nIndex].xOpacity = nOpacity;
	
	setOpacity(slides[current]);
	setOpacity(slides[nIndex]);
	if(cOpacity<=0) {
		slides[current].style.display = "none";
	
		var tabID = slides[current].getAttribute("id");
		for (var n=0; n < links.length; n++) {
			var tabTitle = links[n].getAttribute("title");
			if (tabID == tabTitle) {
				clearTab(links[n]);
			}
		}
		
		current = nIndex;
		
		var tabID = slides[current].getAttribute("id");
		for (var n=0; n < links.length; n++) {
			var tabTitle = links[n].getAttribute("title");
			if (tabID == tabTitle) {
				showTab(links[n]);
			}
		}

		slideIncrement = setTimeout(so_xfade,5000);
	} else {
		fadeIncrement = 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 prepareMenus() {
	menu = d.getElementById("photoFaderMenu");
	links = menu.getElementsByTagName("a");	
	for (var m=0; m < links.length; m++) {
		links[m].onclick = function() {
			showItem(this);
			return false;
		}
	}
	showTab(links[0]);
}

function clearTab(tab) {
	tab.className = "unselected";
	tab.parentNode.className = "unselected";
}

function showTab(tab) {
	tab.className = "selected";
	tab.parentNode.className = "selected";
}

function showItem(menuItem) {
	if (fadeIncrement) {
	clearTimeout(fadeIncrement);
	};
	if (slideIncrement) {
	clearTimeout(slideIncrement);
	};
	for (var a=0; a < links.length; a++) {
		clearTab(links[a]);
		var titles = links[a].getAttribute("title");
		hiddenContent = document.getElementById(titles);
		hiddenContent.style.display = "none";
		hiddenContent.style.opacity = 0;
		hiddenContent.style.MozOpacity = 0;
		hiddenContent.style.filter = "alpha(opacity=0)";
		}
	showTab(menuItem);
	var thisSlide = menuItem.getAttribute("title");
	visibleContent = document.getElementById(thisSlide);
	visibleContent.style.display = "block";
	visibleContent.style.opacity = 1;
	visibleContent.style.MozOpacity = 1;
	visibleContent.style.filter = "alpha(opacity=100)";
}

addLoadEvent(prepareMenus);
