/*	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	*/

/* 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;
	winWidth = fix.offsetLeft + fix.clientWidth;
	return true;
	}

if (window.innerHeight)
	{
	winHeight = window.innerHeight;
	winWidth = window.innerWidth;
	}
return false;
}


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;
	}	
}

// Build DisplayImages array of onscreen images from AllDisplayImages array
function CheckOnScreen()
{

if (!this.AllDisplayImages)
	return;
var i,n=0;
var CurrentImage, OnScreenImage = new Array();
for (i=0;i<this.AllDisplayImages.length;i++)
	{
	CurrentImage = this.AllDisplayImages[i];
	if (CurrentImage != this.ImageObj)
		{
		this.FindObjectPositionRel(CurrentImage);
		if (this.ParentDiv.clientHeight > this.top && this.ParentDiv.clientWidth > this.left)
	//	this.FindObjectPosition(CurrentImage);
	//	if (winHeight > this.top && winWidth > this.left))
			{
			//alert(winHeight +","+ winWidth +"\n"+ this.top  +","+ this.left);
			OnScreenImage[n++] = CurrentImage;
			}
		}
	}
this.DisplayImages = OnScreenImage;
}

// 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
	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
	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)	
{
// 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);	
this.AllDisplayImages = this.ParentDiv.getElementsByTagName("IMG");
// 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);
		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
		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 chage to become active (Moz is slower then IE)	
	  case 1:	// Fade Start
		OverObjS.visibility = "visible";
		State++;
		break;
	  case 2:
	//	ActiveImage.src = this.src;
		Timeout = 40;
		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";
	//		this.SetOpacity(100);
			State++;
			break;
	  case 11:
	  case 21:
			ActiveImage.src = this.src;
			State++;
			count = 4;
			Timeout = 18;
			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 = 113;	// Must make dynamic!
	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;
}
