/*	Image Animate - We don't need no Flash!
	Copyright Chris Amor 2004
	Enjoy indentation a la Chris - The way it should have been K&R!
	Please do not remove if you want to use it!
	
*/

/* Uses a hidden div positioned right: 0px; bottom: 0px; to descover internal window size */
var winHeight = 1024;
var winWidth = 1024;

function GetWindowSize()
{
var fix = document.getElementById('winSize');
if (fix && fix.offsetTop && fix.clientHeight)
	{
	winHeight = fix.offsetTop + fix.clientHeight;
	//winHeight = fix.offsetParent.clientHeight;
	winWidth = fix.offsetLeft + fix.clientWidth;
	//alert("" +fix.offsetTop + "\n" +fix.clientHeight +"\n" +fix.offsetParent.clientHeight);
	return true;
	}

if (window.innerHeight)
	{
	winHeight = window.innerHeight;
	winWidth = window.innerWidth;
	}
return false;
}


function getInternetExplorerVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}


function LoadImages()
{
this.SwapImage = new Array();
var i,arg=LoadImages.arguments;
var path = arg[0];
for (i=1;i<arg.length;i++)
	{
	this.SwapImage[i-1] = new Image;
	this.SwapImage[i-1].src = path + arg[i];
	//this.SwapImage[i-1].CAOnScreen = true;
	}	

}

function LoadOrigImages()
{
// Get the images from the parent
var CurrentImage,j;
i = this.SawapImages.length;

for (j=0;j<this.AllDisplayImages.length;j++)
	{
	CurrentImage = this.AllDisplayImages[j];
	this.SwapImage[i] = new Image;
	this.SwapImage[i].src = CurrentImage.src;
	++i;
	}

//alert("Is "+this.SwapImage.length+","+ this.AllDisplayImages.length);
}




// Build DisplayImages array of onscreen images from AllDisplayImages array
function CheckOnScreen()
{

if (!this.AllDisplayImages)
	return;
var i,n=0;
var CurrentImage, OnScreenImage = new Array();
//alert("W:"+ winWidth +"\nC:"+ this.ParentDiv.clientWidth);
for (i=0;i<this.AllDisplayImages.length;i++)
	{
	CurrentImage = this.AllDisplayImages[i];
	if (CurrentImage != this.ImageObj)
		{
		this.FindObjectPositionRel(CurrentImage);
		if (this.top < this.ParentDiv.clientHeight && winWidth - this.ParentDiv.offsetLeft > this.left)
			{
			//alert(winHeight +","+ winWidth +"\n"+ this.top  +","+ this.left);
			OnScreenImage[n++] = CurrentImage;
			}
		}
	}

this.DisplayImages = OnScreenImage;
if (this.DisplayImages.length != this.oldTotal)
	{
	this.oldTotal = this.DisplayImages.length;
//	alert("Images total="+this.oldTotal
//		+"\nPD:"+ this.ParentDiv.clientHeight + ","+ this.ParentDiv.clientWidth
//		);
	}
}




// Randomly select an image that isn't already displayed
function GetImage()
{
var j,i,n,er;
var limit = this.DisplayImages.length;
for (j=0;j<this.SwapImage.length;j++)
	{
	er = false;
	i = Math.floor(Math.random() * this.SwapImage.length);
	if (!this.SwapImage[i].complete)
		er = true;
	else
		{
		for (var n=0;!er && n < limit;n++)	// Check if not loaded or already displayed
			{
			if (this.DisplayImages[n].src.indexOf(this.SwapImage[i].src) > -1)
				er = true;
			}
		}
	if (!er)
		return this.SwapImage[i].src; 
	}
		
return null;
}

// Find the Abs position of the given object
function FindObjectPosition(obj)
{
this.left = 0;
this.top = 0;

do	{
	this.left += obj.offsetLeft;
	this.top += obj.offsetTop;
//	if (obj == PictureObj)
//		alert(this.left + "," + PictureObj.style.height+ "," + PictureObj.style.right);
	obj = obj.offsetParent;
	} while(obj);

}

function FindObjectPositionRel(obj)
{
this.left = 0;
this.top = 0;

do	{
	if (obj == this.ParentDiv)
		{
//		alert(obj.clientHeight +"\n"+ this.left +","+ this.top+"\n"+obj.offsetLeft+","+obj.offsetTop);
		break;
		}
	this.left += obj.offsetLeft;
	this.top += obj.offsetTop;
	obj = obj.offsetParent;
	} while(obj);

}


// Can be called from resize to update position of overlay DIV
// Not required now that relative positioning is used.
function	AdjustPosition()
{
if (ActiveImage)
	{	// Position activly (window resize makes a mess - need to trap event)
	this.FindObjectPosition(ActiveImage);
	//	Clip Overlay area - to stop scrollbar activity
	//	alert("top is" +this.top);
	if ((winHeight <= this.top || winWidth <= this.left))
		{	// Has become offscreen - abort
// To correctly clean up must set state so state machine can do the abort operation
		State = 0;
		return;
		}
	OverObjS.height = Math.min(ActiveImage.clientHeight,winHeight - this.top)+"px";	// Limit Size for clipped image
	OverObjS.width  = Math.min(ActiveImage.clientWidth, winWidth - this.left)+"px";	// Limit Size for clipped image
	OverObjS.left = this.left+"px";													// Position Overlay over original image
	//if (this.top < 0 || this.top > 4)
	OverObjS.top = this.top+"px";
	}
}





function	IESetOpacity(pct)
{
this.OpacityObj.opacity = pct;
}

function	MozSetOpacity(pct)
{
this.OpacityObj.MozOpacity = pct / 100;
}
function	SafSetOpacity(pct)
{
this.OpacityObj.opacity = pct / 100;
}

function	NoOpacity(pct)
{
}


var AnimateObjects = new Array();	// Used to provide referance for SetTimer()

function AnimateImage(sParentDiv,sOverDiv,GalleryUrl)	
{
// class locals
var State, Timeout;
var FadePercent, FadeStep;
var MoveOffset, MoveStep, MoveMax;
var ActiveIndex, ActiveImage;

// The Parent Div that Everything must be relative to
this.ParentDiv = document.getElementById(sParentDiv);
if (!this.ParentDiv)
	{
	alert("Animate Div not found");
	this.IsOK = 0;
	return;
	}

this.GalleryUrl = GalleryUrl;
this.AllDisplayImages = this.ParentDiv.getElementsByTagName("IMG");
if (typeof GalleryURL == "string" || typeof GalleryURL == "object" )
	for (i=0;i<this.AllDisplayImages.length;i++)
		{
		this.AllDisplayImages[i].onclick = function() { DisplayGallery(this.src);}
		var ver = getInternetExplorerVersion();
		if (ver > 0 && ver < 6)
			this.AllDisplayImages[i].style.cursor="hand";
		else
			this.AllDisplayImages[i].style.cursor="pointer"; 

		}

// These are all for the overlay Objects
var OverObj = document.getElementById(sOverDiv);		// Outer Div	 		(Used to overlay the original image - abs)
var MoveObj = OverObj.getElementsByTagName("DIV")[0];	// Use 1st nessed DIV	(Used to mask when sliding - relative)
this.ImageObj = MoveObj.getElementsByTagName("IMG")[0];	// Use Ist Image Object	(replacement image)
var OverObjS = OverObj.style;
var MoveObjS = MoveObj.style;
this.Start = function()
	{
	var i,maxImage;
	var count;
	
	switch (State)
	  {
	  case 0:
		if (!this.DisplayImages)
			return;
	
		Timeout = 2000;
		maxImage = this.DisplayImages.length;
		if (!maxImage || !this.SwapImage || !(this.src = this.GetImage()))
			break;
		maxImage *= 2;	
		do	{
			ActiveImage = this.DisplayImages[i = Math.floor(Math.random() * this.DisplayImages.length)];
	//		this.FindObjectPosition(ActiveImage);
	//		} while (!maxImage-- && (ActiveIndex == i || winHeight <= this.top || winWidth <= this.left));
			} while (maxImage-- && (ActiveIndex == i));
	

		if (maxImage < 0)
			{
			ActiveIndex = -1;
			break;
			}

		ActiveIndex = i;	
		this.FindObjectPositionRel(ActiveImage);

//	if (winWidth - this.left < 0)
//	alert (typeof ActiveImage.clientWidth +" and " + ActiveImage.clientWidth + "thisl " + this.left+ ","+winWidth);
// Added this - <0 means it's off screen so should not get selected!
		var w = winWidth - this.left;
		if (w<0)
			w = 0;
		OverObjS.width  = Math.min(ActiveImage.clientWidth, w)+"px";	// Limit Size for clipped image


		OverObjS.left = this.left+"px";													// Position Overlay over original image


		OverObjS.height = Math.min(ActiveImage.clientHeight,winHeight - this.top)+"px";	// Limit Size for clipped image


		OverObjS.top = this.top+"px";
	
		if ((i = Math.random()) > 0.6)
			{ // Slide & Fade
			this.ImageObj.src = ActiveImage.src;
			FadePercent = 100;
			FadeStep = -2;
			this.SetOpacity(100);
			MoveOffset = 0;
			if (i > 0.8)
				{
				MoveMax = ActiveImage.clientHeight;
				MoveStep = (i>0.9) ? 2 : -2; // Shift Left or Right
				State = 20;
				}
			else
				{
				MoveMax = ActiveImage.clientWidth;
				MoveStep = (i>0.7) ? 2 : -2; // Shift Left or Right
				State = 10;
				}
			}
		else
			{	// Fade
	//		this.ImageObj.src = ActiveImage.src;
	//		FadePercent = 100;
	//		this.SetOpacity(100);
			this.SetOpacity(0);
			this.ImageObj.src = this.src;
			FadePercent = 0;
			FadeStep = 2;
			State = 1;
			}
//
		Timeout = 80;
		break;
	// The startup states are to allow time for the image change to become active (Moz is slower then IE)	
	  case 1:	// Fade Start
		OverObjS.visibility = "visible";
		Timeout = 80;
		State++;
		break;
	  case 2:
	//	ActiveImage.src = this.src;
		Timeout = 80;
		State++;
		break;
	  case 3:
		FadePercent += FadeStep;
		if (FadePercent < 100)
			{
			this.SetOpacity(FadePercent);
			break;
			}
		ActiveImage.src = this.ImageObj.src;
		State++;
		Timeout = 100;
		break;
	  case 4:	
	//	this.SetOpacity(0);
		OverObjS.visibility = "hidden";
		State = 0;
		Timeout = 500;
		break;
	 case 10:	// Slide & Fade start 
	 case 20:
			OverObjS.visibility = "visible";
			Timeout = 80;
	//		this.SetOpacity(100);
			State++;
			break;
	  case 11:
	  case 21:
			ActiveImage.src = this.src;
			State++;
			count = 4;
			Timeout = 80;
			break;
	  case 12:
	  case 22:
			MoveOffset += MoveStep;
			if (MoveOffset >= -MoveMax && MoveOffset <= MoveMax)
				{
				if (State == 12)
					MoveObjS.left =  MoveOffset+"px";
				else
					MoveObjS.top =  MoveOffset+"px";
				if (FadePercent && !count)
					{
					count = 3;
					FadePercent += FadeStep;
					this.SetOpacity(FadePercent);
					}
				break;
				}
			this.SetOpacity(0);
			OverObjS.visibility = "hidden";
			MoveObjS.left =  "0px";
			MoveObjS.top =  "0px";
			State = 0;
			Timeout = 500;
			break;
	 
	  case -1:
		State = 0;
		Timeout = 4000;
		break;
	  default:
		alert(State );
	  }
	setTimeout('AnimateObjects['+this.index+'].Start()',Timeout);
	}

// Methods
this.LoadImages = LoadImages;
this.GetImage = GetImage;
this.FindObjectPosition = FindObjectPosition;
this.FindObjectPositionRel = FindObjectPositionRel;
this.CheckOnScreen = CheckOnScreen;
this.AdjustPosition = AdjustPosition;

State = -1;
this.posMode = 0;
this.CheckOnScreen();

if (OverObjS && MoveObjS && this.ImageObj)
	{
	FadePercent = 0;
	//this.FadeHeight = this.ImageObj.height;	// Must make dynamic! (have!)
	if (typeof this.ImageObj.filters == "object" && this.ImageObj.filters.alpha) //document.all)
		{
		this.OpacityObj = this.ImageObj.filters.alpha;
		this.SetOpacity = IESetOpacity;
		}
	else if (typeof this.ImageObj.style.opacity != "undefined")
		{
		this.OpacityObj = this.ImageObj.style;
		this.SetOpacity = SafSetOpacity;
		}
	else if (typeof this.ImageObj.style.MozOpacity != "undefined")
		{	// Firefox 1.5
		this.OpacityObj = this.ImageObj.style;
		this.SetOpacity = MozSetOpacity;
		}
	else
		this.SetOpacity = NoOpacity;
	
	this.index = AnimateObjects.length;
	AnimateObjects[AnimateObjects.length] = this;
	this.IsOK = 1;
	return this;
	}
return null;
}


function DisplayGallery(src)
{
var p = src.split("/")
var a = p.pop().split(".");
p.pop();	// Discard "thm"
var gal = p.pop();
a.pop();	// Discard "jpg" etc
var tag = a.join(".");

if (typeof GalleryURL == "object")
	{
	for(var i=0;i<GalleryURL.length;i +=2)
		{
		if (gal == GalleryURL[i])
			{
			location.href=GalleryURL[i+1]+"?show="+tag;
			break;
			}
		}
	}
else
	{
	if (typeof GalleryURL == "string")
		location.href=GalleryURL+"?show="+tag;
	}
//alert("Tag="+tag);
}

