// Photo Gallery Load script.
// With image cross fade effect for those browsers that support it.
// Script copyright (C) 2004-2008 http://www.cryer.co.uk/.
// Script is free to use provided this copyright header is included.
var currentGalleryLink = new Array();
var undoActions = new Array();
function PerformActions(actions)
{
	var undos=new Array();
	// Perform any actions
	for (i=0; i<actions.length; i++){
		var action=actions[i];
		var id=action[0];
		var attr=action[1];
		var nval=action[2];
		// Temporary?
		if ((action.length>3)&& !action[3])
		{
			var get='document.getElementById("'+id+'").' + attr;
			var undo=new Array();
			undo[0]=id;
			undo[1]=attr;
			undo[2]=eval(get);	
			undos.push(undo);	
		}
		var set='document.getElementById("'+id+'").' + attr + '="' + nval + '"';
		eval(set);
	}
	return undos;
}
// Opacity and Fade in script.
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 opacity = element.style.opacity * 100;
  var msNow = (new Date()).getTime();
  opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity<0) 
    SetOpacity(element,0)
  else if (opacity>100)
    SetOpacity(element,100)
  else
  {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
  }
}
function FadeIn(id)
{
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",0,100)",1);
}
function FadeOut(id)
{
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",100,0)",1);
}
function FadeInImage(foregroundID,newImage,backgroundID)
{
  var foreground=document.getElementById(foregroundID);
  if (backgroundID)
  {
    var background=document.getElementById(backgroundID);
    if (background)
    {
      background.style.backgroundImage = 'url(' + foreground.src + ')';
      background.style.backgroundRepeat = 'no-repeat';
    }
  }
  SetOpacity(foreground,0);
  foreground.src = newImage;
  if (foreground.timer) window.clearTimeout(foreground.timer); 
  var startMS = (new Date()).getTime();
  foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',1000," + startMS + ",0,100)",10);
}
function LoadPicture(pictureID,imageFile,backgroundID,actions)
{
	var foregnd = document.getElementById(pictureID);
	if (foregnd.auto) {} else StopAutomatic(pictureID);
	var backgnd = document.getElementById(backgroundID);
	if (backgnd) 
	{
		backgnd.style.backgroundImage = 'url(' + foregnd.src + ')';
		backgnd.style.backgroundRepeat = 'no-repeat';
		SetOpacity(foregnd,0);
		foregnd.src = imageFile;
		if (foregnd.timer) window.clearTimeout(foregnd.timer); 
		var startMS = (new Date()).getTime();
		foregnd.timer = window.setTimeout("ChangeOpacity('" + pictureID + "',1000," + startMS + ",0,100)",10);
	} else {
		foregnd.src = imageFile;
	}
	if (undoActions[pictureID]) PerformActions(undoActions[pictureID]);
	undoActions[pictureID]=PerformActions(actions);
	// Which link is currently selected?
	for (i=0; i<document.links.length;i++)
	{
	   	var l=document.links[i];
	   	var n=l.getAttributeNode('onclick');
		if (n)
		{
			var onclick = n.value;
	   		if ((onclick) &&
	   			(onclick.indexOf(pictureID) > 0) &&
	   			(onclick.indexOf(imageFile) > 0))
	   		{
			    currentGalleryLink[pictureID] = i;
			    break;
			}
		}
	}
}
function LoadPrev(pictureID)
{
	var current = currentGalleryLink[pictureID];
	if (current == null) current = document.links.length+1;
	var alt = null;
	for (l=document.links.length-1;l>0;l--)
	{
		var link=document.links[l];
		var node = link.getAttributeNode('onclick');
		if (node)
		{
			var onclick = node.nodeValue;
			if ((onclick) &&
				(onclick.indexOf('LoadPicture') >= 0) &&
				(onclick.indexOf(pictureID) > 0))
			{
				if (l < current)
				{
					eval(onclick);
					currentGalleryLink[pictureID] = l;	// It will aleady be (99%, rare exceptions)
					return;
				} else if (alt == null)
					alt = onclick;
			}
		}
	}
	if (alt) eval(alt);
}
function LoadNext(pictureID)
{
	var current = currentGalleryLink[pictureID];
	if (current == null) {
		currentGalleryLink[pictureID]=-1;
		LoadNext(pictureID);
		current = currentGalleryLink[pictureID];
	}
	var alt = null;
	for (l=0;l<document.links.length;l++) {
		var link = document.links[l];
		var node = link.getAttributeNode('onclick');
		if (node)
		{
			var onclick = node.value;
			if ((onclick) &&
				(onclick.indexOf('LoadPicture') >= 0) &&
				(onclick.indexOf(pictureID) > 0))
			{
				if (l > current)
				{
					eval(onclick);
					currentGalleryLink[pictureID] = l;	// It will aleady be (99%, rare exceptions)
					return;
				} else if (alt == null)
					alt = onclick;
			}
		}
	}
	if (alt) {
		eval(alt);
	}
}
function LoadNextAuto(pictureID)
{
	var picture = document.getElementById(pictureID);
	if (pictureID)
	{
		picture.auto = true;
		LoadNext(pictureID);
		picture.auto = null;
	}
}
function StopAutomatic(pictureID)
{
	// Is it on automatic?
	var picture = document.getElementById(pictureID);
	if (!picture) return;
	if (!picture.automatic) return;
	// Stop it.
	window.clearInterval(picture.automatic);
	picture.automatic = null;
}
function StartAutomatic(pictureID,period)
{
	// Do nothing if its already running on automatic.
	var picture = document.getElementById(pictureID);
	if (!picture) return;
	if (picture.automatic) return;
	// Set it going.
	picture.automatic = window.setInterval("LoadNextAuto('" + pictureID + "')",period*2000);
	// Move on one now to show responsiveness.
	LoadNextAuto(pictureID);
}
