// javascript for LouLouBell

// these functions will run when each page loads.
function onloads() {
 if (document.getElementById('slideshow')) {startslides();}
}



// generates e-mail link, hides address from spammers; linktext is optional
// <script type="text/javascript">mailto('user','domain.com');</script>
function mailto(user,domain,linktext,subject){
 address = user + '@' + domain;
 if (linktext == undefined) linktext = address;
 if (subject == undefined) {
  document.write('<a href="mailto:' + address + '">' + linktext + '</a>');
 } else {
  document.write('<a href="mailto:' + address + '?subject=' + subject + '">' + linktext + '</a>');
 }
}



// creates a slideshow from <ul id="slideshow">, which should contain <li>s with <img>s.
function startslides() {
 // number of seconds between slides; use 1 for quick testing, 2 - 4 for working website
 slideshow_seconds = 3;

 // controls fading speed (use 50-90) and smoothness; smoothness was .05, but slides did not quite reach 100% opacity.
 slideshow_fadingspeed = 50; // milliseconds
 slideshow_smoothness = .05;

 // build slide array and declare global variables.
 slides = new Array();
	slides = document.getElementById("slideshow").getElementsByTagName("li");
 currentslide = 0;
	slideshow_can_continue = true;

	// set up mouse events and initial slide opacity.
	for(i=1; i<slides.length; i++) {
  slides[i].xOpacity = 0;
  //slides[i].title = "Slideshow paused.";
  slides[i].onmouseover = function() { pauseslides(); };
  slides[i].onmouseout = function() { resumeslides(); };
 }
	slides[currentslide].style.display = "block";
	slides[currentslide].xOpacity = .99;

 //schedule fade to start after designated time
 slide_fading = setTimeout(fadeslide,(slideshow_seconds*1000));
}


function fadeslide() {
 nextslide = slides[currentslide+1] ? currentslide+1 : 0;
 slides[nextslide].style.display = "block";

 slides[currentslide].xOpacity -= slideshow_smoothness;
	slides[nextslide].xOpacity += slideshow_smoothness;

 setopacity(slides[currentslide]);
	setopacity(slides[nextslide]);

 // this if / else sequence provides an opportunity to pause the slideshow.
 // if it is okay to continue and the current slide has fully faded, undisplay it and continue fading the next slide.
 if (slideshow_can_continue && (slides[currentslide].xOpacity <= 0)) {
 	slides[currentslide].style.display = "none";
 	currentslide = nextslide;
 	slide_fading = setTimeout(fadeslide,(slideshow_seconds*1000));
 // else if it is not okay to continue and the current slide has fully faded, stop.
 } else if (!slideshow_can_continue && (slides[currentslide].xOpacity <= 0)) {
  return;
 // else the slide has not full faded, so continue to fade.
 } else {
	 slide_fading_out = setTimeout(fadeslide,slideshow_fadingspeed);
 }
}


// crop opacity at .99, and translate "xOpacity" variable into CSS values.
function setopacity(obj) {
	if(obj.xOpacity > .99) {	obj.xOpacity = .99; }
	obj.style.opacity = obj.xOpacity;
	obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	//obj.innerHTML = obj.style.opacity; // useful for testing
}


// pause slideshow
function pauseslides() {
 clearTimeout(slide_fading);
 //clearTimeout(slide_fading_out); // this allows slideshow to be paused mid-fade.
 slideshow_can_continue = false;
}


// resume slideshow
function resumeslides() {
 clearTimeout(slide_fading);
 clearTimeout(slide_fading_out);
 slideshow_can_continue = true;
 fadeslide();
}
