function pauseHighlight() 
{
	if (indexHighlight > 0) {
		bNoHighlight = true;
		if (iTimeoutHighlight != null)
		{
			clearTimeout(iTimeoutHighlight);
			iTimeoutHighlight = null;
		}
		playing = false;
	}
	var img_ = getChildNodeById('highlight_play_pause_button');
	var link_ = getChildNodeById('highlight_play_pause_link');
	//img_.src = imagedirectory_ + "interface/highlight-button-play.png";
	img_.src = "/images/highlights/interface/highlight-button-play.png";
	img_.alt = "Play";
	link_.onclick = function() { playHighlight(); return false; };
	//alert('Pausing highlight.');
	return false;
}

function playHighlight() 
{ 
	//alert("Playing highlight.");
	if ( !playing ) {
		bNoHighlight = false;
		if (iTimeoutHighlight != null)
		{
			clearTimeout(iTimeoutHighlight);
			iTimeoutHighlight = null;
		}
		if (indexHighlight < 0)
			indexHighlight = 1;
		var img_ = getChildNodeById('highlight_play_pause_button');
		var link_ = getChildNodeById('highlight_play_pause_link');
		//img_.src = imagedirectory_ + "interface/highlight-button-pause.png";
		img_.src = "/images/highlights/interface/highlight-button-pause.png";
		img_.alt = "Pause";
		link_.onclick = function() { pauseHighlight(); return false; };
		playing = true;
		switchHighlight(timeouts[indexHighlight-1]);
	}
	return false;
}

function moveToHighlight( index_ )
{
	pauseHighlight();
	switchIMG(index_, indexHighlight);
	indexHighlight = index_;
	return false;
}

function moveToNext() {
	pauseHighlight();
	prevIndex = indexHighlight;
	indexHighlight += 1;
	if ( indexHighlight > images.length ) 
		indexHighlight = 1;
	switchIMG(indexHighlight, prevIndex);
	return false;
}

function moveToPrevious() {
	pauseHighlight();
	prevIndex = indexHighlight;
	indexHighlight -= 1;
	if ( indexHighlight < 1 ) 
		indexHighlight = images.length;
	switchIMG(indexHighlight, prevIndex);
	return false;
}

function switchHighlight( timeout_ ) 
{
	//alert("switchHighlight()");
	if ( indexHighlight > 0 ) 
		prevIndex = indexHighlight;
	indexHighlight += 1;
	if ( indexHighlight > images.length ) 
		indexHighlight = 1;		
	if (switchIMG(indexHighlight, prevIndex)) {
		var timeout_ = timeouts[indexHighlight-1];
		iTimeoutHighlight = setTimeout("switchHighlight(" + timeout_ + ")", timeout_);
	} else {
		// failure
		indexSlideShow = -1;
		playing = false;
	}	
}

function switchIMG(index_, previndex_)
{
	if (index_ == null)
		index_ = 0;
	if (index_ < 0 )
		return false;
	var img_ = getChildNodeById('highlight_photo');
	var text_ = getChildNodeById('highlight_text');
	var title_ = getChildNodeById('highlight_title');
	var link_ = getChildNodeById('highlight_link');
	var link2_ = getChildNodeById('highlight_photo_link');
	// fade transition if we have switched images
	if ( previndex_ == index_ ) {
		img_.src = imagedirectory_ + images[index_-1];
	} else {
		doFade = true;
		fadeTimeout = setTimeout("fadeSwitch(" + previndex_ + "," +  index_ + ", 20)", 20);
		
		// swap style for frames to make new one selected
		var curr_ = getChildNodeById('highlight_frame_' + index_);
		var prev_ = getChildNodeById('highlight_frame_' + previndex_);
		curr_.setAttribute("class", "sels");
		curr_.setAttribute("className", "sels");
		//alert("Current: " + index_ + ", Previous: " + previndex_);
		// and old one unselected
		if ( prev_ != null ) {
			prev_.setAttribute("class", "unsels");
			prev_.setAttribute("className", "unsels");
		}
	}
	img_.alt = titles[index_-1];
	text_.innerHTML = text[index_-1];
	title_.innerHTML = titles[index_-1];
	if ( links[index_-1] != null ) {
		link_.style.display = 'inline';
		link_.href = links[index_-1];
		link2_.href = links[index_-1];
	} else {
		link_.style.display = 'none';
		link2_.href = '';
	}
	
	//alert('Switching to image: ' + images[index_-1] + ' based on index: ' + index_);
	return true;
}

var opacity = 1.0;
var fadeOut = true;
var fadeTimeout = null;
var doFade = true;
function fadeSwitch(curr_, next_, timeout_)
{
	//alert("Fading.");
	var img_ = getChildNodeById('highlight_photo');
	if ( fadeOut )
	{
		if ( opacity > 0 ) {
			opacity -= 0.1;
			opacity = Math.round(opacity*100)/100;
			img_.style.opacity = opacity;
			img_.style.MozOpacity = opacity;
			img_.style.KhtmlOpacity = opacity;
			img_.style.filter = "alpha(opacity=" + (opacity*100) + ")";
			//img_.setAttribute("style", "opacity: " + opacity + "; filter: alpha(opacity=" + (opacity*100) + ");");
			//alert("Reducing opacity to : " + opacity);
		} else { 
			fadeOut = false;
			img_.src = imagedirectory_ + images[next_-1];
			//alert("Switching image.");
		}			
	} else {
		if ( opacity >= 1 )  {
			// clear fade timeout
			clearTimeout(fadeTimeout);
			fadeTimeout = null;
			doFade = false;
			fadeOut = true;
		} else {
			opacity += 0.1;
			opacity = Math.round(opacity*100)/100;
			img_.style.opacity = opacity;
			img_.style.MozOpacity = opacity;
			img_.style.KhtmlOpacity = opacity;
			img_.style.filter = "alpha(opacity=" + (opacity*100) + ")";
			//img_.setAttribute("style", "opacity: " + opacity + "; filter: alpha(opacity=" + (opacity*100) + ");");
			//alert("Increasing opacity to : " + opacity);
		}
	}
	if ( doFade ) {
		fadeTimeout = setTimeout("fadeSwitch(" + curr_ + "," +  next_ + ", 20)", 20);
	}
}

function getChildNodeById(sId, oElem)
{
	if (oElem == null)
	{
		oElem = document;
		if (document.getElementById)
			return document.getElementById(sId);
	}
	var childNodes = oElem.childNodes
	for (var i = 0; i < childNodes.length; i++)
	{
		var oElem = childNodes[i];
		if (oElem.id == sId)
			return(oElem);
		if ((oElem = getChildNodeById(sId, oElem)) != null)
			return(oElem);
	}
}

function preLoadHighlights( images, path )
{
	var preloaded = new Array();
	for ( i=0; i < images.length; i++ ) {
		var prel = new Image();
		prel.src = path + images[i];
		preloaded.push(prel);
	}
}
preLoadHighlights(images, imagedirectory_);
window.onload = playHighlight;