//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 imageNames = "img/blk/back01.jpg img/blk/back03.jpg img/blk/back04.jpg img/blk/back05.jpg img/blk/back06.jpg img/blk/back07.jpg img/blk/back08.jpg img/blk/back09.jpg img/blk/back11.jpg img/blk/back12.jpg img/blk/back13.jpg".split(" ");
	
//next image index to be faded
var indexValue = Math.floor(Math.random() * imageNames.length-1) + 1;

//variables for container/controller/target
var containerID = 'photocontainer';
var targetID    = 'photo';
var container   = null;
var target      = null;

//-----------------------------------------------------------------------
// start the photo fade show
function startPhoto()
{
   	if (!document.getElementById) return;
        
   	//set references to container && target ** a href    
   	container = document.getElementById(containerID);
   	target = document.getElementById(targetID);
            
   	setalpha(0) ;
   	timeout = window.setTimeout("nextPhoto()", 1);
}


//-----------------------------------------------------------------------
// set a timeout to start image fading
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 = imageNames[indexValue];
        
   	//get new index different from current
   	var newvalue = indexValue;
   	while(newvalue == indexValue)
   	{
       	newvalue = Math.floor(Math.random() * imageNames.length);
   	}
   	indexValue = newvalue;

   	//fade image in
    timeout = window.setTimeout("reveal('0')", 500);
}

//-----------------------------------------------------------------------
// make the background image the same as the target img, and then make the target alpha of 0
function switchContainer()
{
  	if (!document.getElementById) return;
        
   	//make container background current image
   	container.style.backgroundImage = 'url(' + target.src + ')';    

   	//make image 0
   	setalpha(0) ;
}

//-----------------------------------------------------------------------
// fade the img in
function reveal(opacity) 
{
 	if (!document.getElementById) return;
      
   	if (document.getElementById && opacity <= 100 ) 
   	{
      	setalpha(opacity); 
        
       	opacity += 2;
        
       	//store globally for restart
       	globalOpacity = opacity;
        
       	//fade next step
       	timeout = window.setTimeout("reveal("+opacity+")", 50);
  	}
   	else
   	{
      	//we are done .. load next photo in 5 seconds
       	timeout = window.setTimeout("nextPhoto()", 2000);
        
       	switchContainer();
   	}
}

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


//-----------------------------------------------------------------------
/** a derivation (or mangling) of a script from Scott Andrew (www.scottandrew.com) **/
function addEvent(obj, evType, fn)
{ 
   	if (obj.addEventListener)
   	{ 
       	obj.addEventListener(evType, fn, true); 
       	return true; 
   	} 
   	else if (obj.attachEvent)
   	{ 
       	var r = obj.attachEvent("on"+evType, fn); 
       	return r; 
   	} 
   	else 
   	{ 
       	return false; 
   	} 
}  
    

//-----------------------------------------------------------------------
// Set the alpha value   
function setalpha(opacity) 
{
  	if (document.getElementById ) 
   	{    
     	//fade next step based onbrowser compatibility
       	if (target.style.MozOpacity!=null)
		{
          	target.style.MozOpacity = (opacity/100) - 0.001;
       	}
		else if (target.style.opacity!=null)
		{
          	target.style.opacity = opacity/100;
       	}
		else if (target.style.filter!=null)
		{
          	target.style.filter = "alpha(opacity=" + opacity + ")";
    	}
		else if (target.style.KhtmlOpacity!=null)
		{
          	target.style.KhtmlOpacity = opacity/100;
    	}
	}
}

