// Browser Slide-Show script. With image cross fade effect for those browsers
// that support it.
// Script copyright (C) 2004-2010 www.cryer.co.uk.
// Script is free to use provided this copyright header is included.
var FadeDurationMS=1000;
function SetOpacity(object,opacityPct)
{
	// IE.
	object.style.filter = 'alpha(opacity=' + opacityPct + ')';
	// Old mozilla and firefox
	object.style.MozOpacity = opacityPct/100;
	// Everything else.
	object.style.opacity = opacityPct/100;
}
function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
	var element=document.getElementById(id);
	var msNow = (new Date()).getTime();
	var opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
	if (opacity>=100)
	{
		SetOpacity(element,100);
		element.timer = undefined;
	}
	else if (opacity<=0)
	{
		SetOpacity(element,0);
		element.timer = undefined;
	}
	else 
	{
		SetOpacity(element,opacity);
		element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",10);
	}
}
function FadeInImage(foregroundID,newImage,backgroundID, id)
{
	var foreground=document.getElementById(foregroundID);
	if (foreground.timer) window.clearTimeout(foreground.timer);

	if (backgroundID)
	{
		var background=document.getElementById(backgroundID);
		if (background)
		{
			if(background.src)
			{
				foreground.src = background.src;	
				SetOpacity(foreground,100);
			}
			background.src = newImage;
			background.style.backgroundImage = 'url(' + newImage + ')';
			background.style.backgroundRepeat = 'no-repeat';

			if(stopped)
			{
				foreground.src = newImage;
				foreground.style.backgroundImage = 'url(' + newImage + ')';
				foreground.style.backgroundRepeat = 'no-repeat';			
			}
			else
			{			
				var startMS = (new Date()).getTime();
				foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "'," + FadeDurationMS + "," + startMS + ",100,0)",10);
			}
			
			anchor = document.getElementById(foregroundID + '_link');
			
			if(hrefs[id])
				anchor.setAttribute('href', hrefs[id]);
			else
				anchor.removeAttribute('href');										
			
			//clear params
			
			alt = foreground.getAttribute('alt');
			src = foreground.getAttribute('src');
			width = foreground.getAttribute('width');
			height = foreground.getAttribute('height');
			style =	foreground.getAttribute('style');
			the_id = foreground.getAttribute('id');					
												
			
			try{
			      if(params[id] == undefined)
			      	anchor.innerHTML = '<img id="'+the_id+'" />';
			      else
			      	anchor.innerHTML = '<img id="'+the_id+'" ' + params[id] + ' />';
			}
			catch(e)
			{
				anchor.innerHTML = '<img id="'+the_id+'" />';
			}
			
			theimg = document.getElementById(the_id);
						
			theimg.setAttribute('alt', alt);
			theimg.setAttribute('style', style);
			theimg.setAttribute('src', src);
			theimg.setAttribute('width', width);
			theimg.setAttribute('height', height);
						
		}
	} else {		
		foreground.src = newImage;
	}
}

var stopped = false;
var slideCache = new Array();

function FreezeOnSlide(id, pictureID, backgroundID)
{
	StopSlide();
	
	for(i=0; i<slides.length; i++)
	{
		tmp_ctrl = document.getElementById('slidectrl_' + i);
		tmp_ctrl.src = tmp_ctrl.src.replace("high","grey");	
	}
			
	tmp_ctrl = document.getElementById('slidectrl_' + id);
	tmp_ctrl.src = tmp_ctrl.src.replace("grey","high");
			
	FadeInImage(pictureID,slides[id],backgroundID, id);
}	

function StopSlide()
{
	stopped = true;
}

function RunSlideShow(pictureID, backgroundID, displaySecs, curr_ctrl)
{			
	
	if(stopped == true)
		return;
				
	var nextImage = slides[curr_ctrl];	
	
	if (slideCache[nextImage] && slideCache[nextImage].loaded)
	{		
		FadeInImage(pictureID,nextImage,backgroundID, curr_ctrl);
						
		//grey out last one
		curr_ctrl = (curr_ctrl + slides.length - 1) % slides.length;						
		tmp_ctrl = document.getElementById('slidectrl_' + curr_ctrl);
		tmp_ctrl.src = tmp_ctrl.src.replace("high","grey");
		
		//high light this last one
		curr_ctrl = (curr_ctrl + 1) % slides.length;				
		tmp_ctrl = document.getElementById('slidectrl_' + curr_ctrl);		
		tmp_ctrl.src = tmp_ctrl.src.replace("grey","high");
		
		//increment
		curr_ctrl = (curr_ctrl + 1) % slides.length;			
		
		setTimeout("RunSlideShow('"+pictureID+"','"+backgroundID+"',"+displaySecs+","+curr_ctrl+")",displaySecs*1000);
				
		var nextImage = slides[curr_ctrl];
		
	} else
	{		
		setTimeout("RunSlideShow('"+pictureID+"','"+backgroundID+"',"+displaySecs+","+curr_ctrl+")",250);
	}
	// Cache the next image to improve performance.
	if (slideCache[nextImage] == null)
	{
		slideCache[nextImage] = new Image;
		slideCache[nextImage].loaded = false;
		slideCache[nextImage].onload = function(){this.loaded=true};
		slideCache[nextImage].src = nextImage;
	}
}

