/* New Slideshow script - 5/16/2007 Lake BCC */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function pauseSShow() 
{
	if (indexSShow > 0) {
		bNoSShow = true;
		if (iTimeoutSShow != null)
		{
			clearTimeout(iTimeoutSShow);
			iTimeoutSShow = null;
		}
		sshow_playing = false;
	}
	var img_ = getChildNodeById('sshow_play_pause_button');
	var link_ = getChildNodeById('sshow_play_pause_link');
	//img_.src = imagedirectory_ + "interface/SShow-button-play.png";
	img_.src = "/images/slideshow/interface/sshow-button-play.png";
	img_.alt = "Play";
	link_.onclick = function() { playSShow(); return false; };
	//alert('Pausing SShow.');
	return false;
}

function playSShow() 
{ 
	//alert("Playing SShow.");
	if ( !sshow_playing ) {
		bNoSShow = false;
		if (iTimeoutSShow != null)
		{
			clearTimeout(iTimeoutSShow);
			iTimeoutSShow = null;
		}
		if (indexSShow < 0)
			indexSShow = 1;
		var img_ = getChildNodeById('sshow_play_pause_button');
		var link_ = getChildNodeById('sshow_play_pause_link');
		//img_.src = imagedirectory_ + "interface/SShow-button-pause.png";
		img_.src = "/images/slideshow/interface/sshow-button-pause.png";
		img_.alt = "Pause";
		link_.onclick = function() { pauseSShow(); return false; };
		sshow_playing = true;
		iTimeoutSShow = setTimeout("switchSShow(" + sshow_timeouts[indexSShow-1] + ")", sshow_timeouts[indexSShow-1]);
	}
	return false;
}

function moveToSShow( index_ )
{
	pauseSShow();
	switchIMGSShow(index_, indexSShow);
	indexSShow = index_;
	return false;
}

function moveToNextSShow() {
	pauseSShow();
	sshow_prevIndex = indexSShow;
	indexSShow += 1;
	if ( indexSShow > sshow_images.length ) 
		indexSShow = 1;
	switchIMGSShow(indexSShow, sshow_prevIndex);
	return false;
}

function moveToPreviousSShow() {
	pauseSShow();
	sshow_prevIndex = indexSShow;
	indexSShow -= 1;
	if ( indexSShow < 1 ) 
		indexSShow = sshow_images.length;
	switchIMGSShow(indexSShow, sshow_prevIndex);
	return false;
}

function switchSShow( timeout_ ) 
{
	//alert("switchSShow()");
	if ( indexSShow > 0 ) 
		sshow_prevIndex = indexSShow;
	indexSShow += 1;
	if ( indexSShow > sshow_images.length ) 
		indexSShow = 1;		
	if (switchIMGSShow(indexSShow, sshow_prevIndex)) {
		var timeout_ = sshow_timeouts[indexSShow-1];
		iTimeoutSShow = setTimeout("switchSShow(" + timeout_ + ")", timeout_);
	} else {
		// failure
		indexSlideShow = -1;
		sshow_playing = false;
	}	
}

function switchIMGSShow(index_, previndex_)
{
	if (index_ == null)
		index_ = 0;
	if (index_ < 0 )
		return false;
	var img_ = getChildNodeById('sshow_photo');
	var text_ = getChildNodeById('sshow_text');
	var title_ = getChildNodeById('sshow_title');
	var link_ = getChildNodeById('sshow_link');
	var link2_ = getChildNodeById('sshow_photo_link');
	// fade transition if we have switched images
	if ( previndex_ == index_ ) {
		img_.src = sshow_imagedirectory_ + sshow_images[index_-1];
	} else {
		sshow_doFade = true;
		fadeTimeout = setTimeout("fadeSwitchSShow(" + previndex_ + "," +  index_ + ", 20)", 20);
		
		// swap style for frames to make new one selected
		var curr_ = getChildNodeById('sshow_frame_' + index_);
		var prev_ = getChildNodeById('sshow_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 = sshow_text[index_-1];
	text_.innerHTML = sshow_text[index_-1];
	//title_.innerHTML = titles[index_-1];
	if ( sshow_links[index_-1] != null ) {
		link_.style.display = 'inline';
		link_.href = sshow_links[index_-1];
		link2_.href = sshow_links[index_-1];
	} else {
		link_.style.display = 'none';
		link2_.href = '';
	}
	
	//alert('Switching to image: ' + images[index_-1] + ' based on index: ' + index_);
	return true;
}

var sshow_opacity = 1.0;
var sshow_fadeOut = true;
var sshow_fadeTimeout = null;
var sshow_doFade = true;
function fadeSwitchSShow(curr_, next_, timeout_)
{
	//alert("Fading.");
	var img_ = getChildNodeById('sshow_photo');
	if ( sshow_fadeOut )
	{
		if ( sshow_opacity > 0 ) {
			sshow_opacity -= 0.1;
			sshow_opacity = Math.round(sshow_opacity*100)/100;
			img_.style.opacity = sshow_opacity;
			img_.style.MozOpacity = sshow_opacity;
			img_.style.KhtmlOpacity = sshow_opacity;
			img_.style.filter = "alpha(opacity=" + (sshow_opacity*100) + ")";
			//img_.setAttribute("style", "opacity: " + opacity + "; filter: alpha(opacity=" + (opacity*100) + ");");
			//alert("Reducing opacity to : " + sshow_opacity);
		} else { 
			sshow_fadeOut = false;
			img_.src = sshow_imagedirectory_ + sshow_images[next_-1];
			//alert("Switching image.");
		}			
	} else {
		if ( sshow_opacity >= 1 )  {
			// clear fade timeout
			clearTimeout(sshow_fadeTimeout);
			sshow_fadeTimeout = null;
			sshow_doFade = false;
			sshow_fadeOut = true;
		} else {
			sshow_opacity += 0.1;
			sshow_opacity = Math.round(sshow_opacity*100)/100;
			img_.style.opacity = sshow_opacity;
			img_.style.MozOpacity = sshow_opacity;
			img_.style.KhtmlOpacity = sshow_opacity;
			img_.style.filter = "alpha(opacity=" + (sshow_opacity*100) + ")";
			//img_.setAttribute("style", "opacity: " + opacity + "; filter: alpha(opacity=" + (opacity*100) + ");");
			//alert("Increasing opacity to : " + sshow_opacity);
		}
	}
	if ( sshow_doFade ) {
		//alert("Recreating timeout.");
		sshow_fadeTimeout = setTimeout("fadeSwitchSShow(" + 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 preLoadSShows( 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);
	}
}
preLoadSShows(sshow_images, sshow_imagedirectory_);
if ( autoplaySShow ) {
	addLoadEvent(playSShow);
}