/*
 * Code for Cycling The Photo in the site name
 * 
 * This code assumes the existance of a global variable that 
 * defines the relative path to the root of the document 
 * directory structure where the tools/ and headers/ directory
 * can be found.
 */

//boolean: whether or not we are fading
var doingFade = true;

//use as a handle on the timeout loop  
var timeout;

//variable to hold global opactiy
var globalOpacity = 0;

var imagePrefix;

//next image index to be faded
var indexValue = 0;

//variables for container/controller/target
var containerID = 'sitename';
var targetID    = 'siteHeader';
var container   = null;
var target      = null;

function setImageList()
{
	if(imagegetter.readyState == 4)
	{
		imageNames = eval(imagegetter.responseText);
		timeout = window.setTimeout("nextPhoto()", 4000);
	}
}

function startPhoto()
{
	if (!document.getElementById) return;
	
	imagePrefix = root + "headers/";
	
	//set references to container && target ** a href    
	container = document.getElementById(containerID);
	target = document.getElementById(targetID);
	
	//Dynamically get image list
	imagegetter = GetXMLHttpObject();  //defined in ajax.js
	if(imagegetter != null)
	{
		imagegetter.onreadystatechange = setImageList;
		imagegetter.open("GET", root +"tools/headerlist.php", true);
		imagegetter.send(null);
	}
		
	//setalpha(0) ;
	//timeout = window.setTimeout("nextPhoto()", 4000);
}

function nextPhoto() 

{
	if (!document.getElementById) return; 
	
	// only one transition at a time, please
	clearTimeout(timeout); 
	
	//flip image and container
	switchContainer();    
	
	//load next image   
	target.src = imagePrefix + imageNames[indexValue];
	
	//get new index differnet from current
	/*var newvalue = indexValue;
	while(newvalue == indexValue)
	{
		newvalue = Math.floor(Math.random()* imageNames.length);
	}
	indexValue = newvalue;*/
	indexValue = (indexValue + 1) % imageNames.length;
	
	//fade image in
	timeout = window.setTimeout("reveal('0')", 500);

}
    
function switchContainer()
{
	if (!document.getElementById) return;
	
	//make container background current image
	container.style.backgroundImage = 'url(' + target.src + ')';    
	
	//make image 0
	setalpha(0) ;
}

function reveal(opacity) 
{
  if (!document.getElementById) return;

  //alert("Reveal(" + opacity +')');

  if (opacity <= 100 ) 
  {
	setalpha(opacity); 
	opacity += 5;
	
	//store globally fo restart
	globalOpacity = opacity;
	
	//fade next step
	timeout = window.setTimeout("reveal(" + opacity + ')', 100);
  }
  else
  {
	//we are done .. load next photo in 5 seconds
	timeout = window.setTimeout("nextPhoto()", 5000);
	
	switchContainer();
  }
}

/*function stopPhoto()
{
	if (!document.getElementById) return;
	
	if(doingFade)
	{
		//stopfade
		control.innerHTML = "start fade";
		clearTimeout(timeout);
	}
	else
	{
		//start fade from where we left off
		control.innerHTML = "stop fade";
		timeout = window.setTimeout("reveal("+globalOpacity+")", 100);
	}
	//invert variable
	doingFade = !doingFade;
}*/

function setalpha(opacity) 
{
  if (document.getElementById ) 
  {    
    //fade next step based onbrowser compatibility
    if (target.style.MozOpacity!=null) {
       target.style.MozOpacity = (opacity/100.0) - 0.001; //patrick h. lauke (http://www.splintered.co.uk/) workaround for Mozilla 'flash' bug - I _never_ would have caught that
    } else if (target.style.opacity!=null) {
       target.style.opacity = opacity/100.0;
    } else if (target.style.filter!=null) {
       target.style.filter = "alpha(opacity=" + opacity + ")";
	} else if (target.style.KhtmlOpacity!=null) {
       target.style.KhtmlOpacity = opacity/100.0;
	}
  }
} 

window.onload = startPhoto;