var ScreenWidth = 0;	// Keeps track of screen width for home page picture placement.

var FadeDuration = 1000;	// Time allowed for home page pictures cross-fade (1 second).

var FadeDelay = 6000;	// Time between fade sequences (6 seconds).

var CurrentHomePicture = 0;	// Index of starting home page picture.

var NewHomePicture = 1;	// Index of next picture to fade in.

var FadeTimeRemaining = FadeDuration;	// Time remaining in home page picture fade.

var NumHomePictures = 6;	// Number of pictures in home page rotation.

var Images = new Array();	// Array to hold reference to images for crossfading.

/************************************************************************************
Function:		SetHeaderTextHeight
Arguments:		PageType - type of page calling function
					1 - Home page
					2 - Anything else
Description:	For the Home page, this positions all the images and the banner text,
				then starts the cross-fading processes.  For the other pages, this
				positions the banner image and text.
************************************************************************************/
function SetHeaderTextPosition(PageType)
{
	// If it's the home page, do the following section of code.
	if (PageType == 1)
	{
		// Extract the main layer object.
		var MainLayer = document.getElementById("Layer_Main");
		
		// Extract the text layer object.
		var TextLayer = document.getElementById("Layer_HeaderText");
		
		// Set the text layer's height as a function of the height of the main layer.
		TextLayer.style.top = -((MainLayer.offsetHeight) - 120);
		
		// Set the side-to-side offset for the text layer.
		TextLayer.style.left = 0;
		
		// This loads the image array with references to each of the cross-fading images
		// and sets their heights according to the height of the navbar.
		for (ImageIndex=0; ImageIndex < NumHomePictures; ImageIndex++)
		{
			Images[ImageIndex] = document.getElementById('Home_MainPicture' + (ImageIndex + 1));
		
			Images[ImageIndex].style.top = document.getElementById('Cell_Navbar').offsetHeight + 20;
		}
		
		// Calls the function to get the current screen width and assign side-to-side position accordingly.
		NewWidth();
		
		// Randomly set the home picture index to a number from 0 to 5.
		CurrentHomePicture = Math.floor(Math.random() * NumHomePictures);
		
		// If the current picture index is the last one (5), set the next picture index to the first one (0).
		if (CurrentHomePicture >= (NumHomePictures - 1))
		{
			NewHomePicture = 0;
		}
		// Otherwise, set the next picture index to the next one.
		else
		{
			NewHomePicture = CurrentHomePicture + 1;
		}
		
		// Set the opacity of the current picture to fully opaque.
		Images[CurrentHomePicture].style.opacity = 1;
		Images[CurrentHomePicture].style.filter = 'alpha(opacity = 100)';
		
		// In six seconds, call the function to cross-fade the pictures.
		setTimeout("FadePictures(0, 1)", FadeDelay);
	}
	// If it's not the home page, do this instead.
	else if (PageType == 2)
	{
		// Set the source for the header image.
		document.getElementById('Image_Header').src = "Images/Common - Seminary Header Image.png";
		
		// Set the source for the text in the header image.
		document.getElementById('Image_HeaderText').src = "Images/Common - Seminary Header Text Black.png";
	
		// Extract the main layer object.
		var MainLayer = document.getElementById("Layer_Main");
		
		// Extract the text layer object.
		var TextLayer = document.getElementById("Layer_HeaderText");
		
		// Set the text layer's height as a function of the height of the main layer.
		TextLayer.style.top = -((MainLayer.offsetHeight) - 91);
		
		// Set the side-to-side offset for the text layer.
		TextLayer.style.left = 75;
	}
}

/*********************************************************************
Function:		NewWidth()
Description:	Gets the width of the browser window and positions the
				home page cross-fading pictures accordingly.
*********************************************************************/
function NewWidth()
{
	// Get the width using three different methods.
	// This is to account for different browser compatibilities.
	if (self.innerHeight)
	{
		ScreenWidth = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		ScreenWidth = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		ScreenWidth = document.body.clientWidth;
	}
	
	// Left offset position for cross-fading pictures.
	var NewPosition = (ScreenWidth / 2) - 350;
	
	// If it's within a certain distance of the left edge of the screen, don't go any further.
	if (NewPosition < 4)
	{
		NewPosition = 4;
	}
	
	// Set the left offset for each cross-fading picture to the new value.
	for (ImageIndex=0; ImageIndex < NumHomePictures; ImageIndex++)
	{
		Images[ImageIndex].style.left = NewPosition;
	}
}

/************************************************************************
Function:		Navbar_Mouseover
Arguments:		ButtonLabel - name of button that's calling this function
Description:	When the mouse hovers over one of the navbar buttons,
				this changes the image to the red rollover one.
************************************************************************/
function Navbar_Mouseover(ButtonLabel)
{
	// Extract the button object.
	var NavButton = document.getElementById("Navbar_" + ButtonLabel);
	
	// Set its source to the red (rollover) image.
	NavButton.src = "Images/Common - Navbar " + ButtonLabel + " Rollover.png";
}

/*******************************************************************************
Function:		Navbar_Mouseout
Arguments:		ButtonLabel - name of button that's calling this function
Description:	When the mouse was hovering over one of the navbar buttons
				and leaves, this changes the image back to the black normal one.
*******************************************************************************/
function Navbar_Mouseout(ButtonLabel)
{
	// Extract the button object.
	var NavButton = document.getElementById("Navbar_" + ButtonLabel);
	
	// Set its source back to the black (normal) image.
	NavButton.src = "Images/Common - Navbar " + ButtonLabel + " Normal.png";
}

/*******************************************************************************
Function:		FadePictures
Arguments:		PreviousTime - time since previous iteration
				NewFade - whether or not a new fade in/out is starting
						- 1 = new fade
						- 0 = fade already started
Description:	Cross-fades two pictures on the home page.
*******************************************************************************/
function FadePictures(PreviousTime, NewFade)
{
	// If this is a new fade sequence, reset the timer.
	if (NewFade == 1)
	{
		PreviousTime = new Date().getTime();
	}
	
	// Find out how much time has passed since the last iteration.
	var ElapsedTime = new Date().getTime() - PreviousTime;
	
	// If the time remaining in the fade sequence is less than the time
	// since the last iteration (i.e. fade is complete), go into this code segment.
	if (FadeTimeRemaining <= ElapsedTime)
	{
		// Set the opacity of the current image to fully transparent.
		Images[CurrentHomePicture].style.opacity = 0;
		Images[CurrentHomePicture].style.filter = 'alpha(opacity = 0)';
		
		// Set the opacity of the new image to fully opaque.
		Images[NewHomePicture].style.opacity = 1;
		Images[NewHomePicture].style.filter = 'alpha(opacity = 100)';
			
		// Reset the time remaining in the fade sequence to the full duration (hasn't started yet).
		FadeTimeRemaining = FadeDuration;
		
		// Set the current picture index to the nwe one.
		CurrentHomePicture = NewHomePicture;
		
		// If the (new) current picture index is the last one in the list,
		// set the new picture index to the first picture.
		if (CurrentHomePicture >= (NumHomePictures - 1))
		{
			NewHomePicture = 0;
		}
		// Otherwise, set the next picture index to the next picture in the list.
		else
		{
			NewHomePicture = CurrentHomePicture + 1;
		}
		
		// In six seconds, start another fade sequence.
		setTimeout("FadePictures(0, 1)", FadeDelay);
	}
	// If there is still time lieft in the fade sequences, go here instead.
	else
	{	
		// Subtract the elapsed time since the last iteration from the total
		// time remaining in the fade sequence.
		FadeTimeRemaining -= ElapsedTime;
		
		// The new opacity setting is a function of how much time is left in the fade
		// sequences compared to the total time allowed for the entire sequence
		// (i.e. as the time remaining shrinks, the opacity setting shrinks with it).
		var TempOpacity = FadeTimeRemaining / FadeDuration;
		
		// Since the current picture is fading out, set its opacity to the shrinking value.
		Images[CurrentHomePicture].style.opacity = TempOpacity;
		Images[CurrentHomePicture].style.filter = 'alpha(opacity = ' + (TempOpacity * 100) + ')';
		
		// Since the new picture is fading in, set its opacity to the inverse of the the shrinking value.
		Images[NewHomePicture].style.opacity = 1 - TempOpacity;
		Images[NewHomePicture].style.filter = 'alpha(opacity = ' + ((1 - TempOpacity) * 100) + ')';
		
		// In 50 milliseconds, call another iteration of this function.
		setTimeout("FadePictures(" + new Date().getTime() + ", 0)", 50);
	}
}