
var image_file_array = new Array();
var image_array = new Array();
var counter = 0;

/************************************************************************************************
This function stores the images associated with the JCC events to be displayed on the home page.
Because I don't know if you can pass arrays from PHP to javascript, the javascript array
image_file_array is built one entry at a time. Therefor, counter is a global variable.
************************************************************************************************/
function store_images(img_file)
{
	image_file_array[counter++] = img_file;
}

/************************************************************************************************
This function starts the rotating display of event images for the home page. There is already an
image displayed when this is called image_file_array[0], so counter is set to 1. hpEventImage is 
the id of the <img> tag. image_array is initialized to prefetch the images before the 
image rotation begins.
************************************************************************************************/
function start_event_display()
{
	for (i = 0; i < image_file_array.length; i++)
	{
		image_array[i] = new Image();
		image_array[i].src = image_file_array[i];
	}
		
	counter = 1;

	setTimeout("fadeOut('hpEventImage', 100)", 5 * 1000);	
}

/************************************************************************************************
This function continues the rotating display of event images for the home page. If state = 'in',
then the image has already been faded in and fadOut() is set to execute in 5 seconds. 
If state = 'out', then the image has been faded out and the next image for hpEventImage is set 
and fadeIn() is called.
************************************************************************************************/
function display_next_event(state)
{
	if (state == 'in')
	{
		setTimeout("fadeOut('hpEventImage', 100)", 5 * 1000);	
	}
	else	
	{
		document.images.hpEventImage.src = image_array[counter].src;
	
		if (++counter >= image_file_array.length)
			counter = 0;

		//alert('about to fade in');

		fadeIn('hpEventImage', 0);	
	}
}

/***********************************************************************************************
The fadeIn function uses a Timeout to call itself every 50ms with an object Id and an 
opacity. The opacity is specified as a percentage and increased 5% at a time. The loop 
stops once the opacity reaches 100%:
***********************************************************************************************/

function fadeIn(objId, opacity)
{
	if (document.getElementById)
	{	obj = document.getElementById(objId);
		if (opacity <= 100)
		{
			{
				setOpacity(obj, opacity);
				opacity += 5;
			}
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
		}
		else
		 	setTimeout("display_next_event('in')", 100);	
	}
}


function fadeOut(objId, opacity)
{
	if (document.getElementById)
	{	obj = document.getElementById(objId);
		if (opacity > 0) 
		{
			setOpacity(obj, opacity);
			opacity -= 5;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 50);
		}
		else
		 	setTimeout("display_next_event('out')", 100);	
	}
}

/***************************************************************************************************
This function removes all child nodes.
parameter id: ID of the node whose children should be removed.
***************************************************************************************************/
function remove_all_child_nodes(id)
{
	alert('in remove');
	
	while (document.getElementById(id).hasChildNodes())
		{															  
			var child = document.getElementById(id).firstChild;
			remove_element(child);
		}
}

/***************************************************************************************************
This function removes an element by referencing it from its parent.
parameter elem: should be the ID of the element to remove.
***************************************************************************************************/
function remove_element(elem)
{
	elem.parentNode.removeChild(elem);
}

/***************************************************************************************************
This function displays a testimonial quote. The parameters are:
index: Index into testimonial_array[]
***************************************************************************************************/
function display_quote(index)
{	var newElem; var newText;

	newElem = document.createElement("p");
	newText = document.createTextNode(testimonial_array[index].quote1);
	newElem.appendChild(newText);
	document.getElementById("quoteText").appendChild(newElem);

	if (testimonial_array[index].quote2 != undefined)
	{
		newElem = document.createElement("p");
		newText = document.createTextNode(testimonial_array[index].quote2);
		newElem.appendChild(newText);
		document.getElementById("quoteText").appendChild(newElem);
	}
	
	if (testimonial_array[index].quote3 != undefined)
	{
		newElem = document.createElement("p");
		newText = document.createTextNode(testimonial_array[index].quote3);
		newElem.appendChild(newText);
		document.getElementById("quoteText").appendChild(newElem);
	}

	// For some reason these next 2 lines do not work in IE
	//document.getElementById("quoteAuthor").firstChild.nodeValue = author;
	//document.getElementById("quoteCompany").firstChild.nodeValue = company;

	newElem = document.createElement("p");
	newText = document.createTextNode(testimonial_array[index].name);
	newElem.appendChild(newText);
	document.getElementById("quoteAuthor").appendChild(newElem);

	newElem = document.createElement("p");
	newText = document.createTextNode(testimonial_array[index].company);
	newElem.appendChild(newText);
	document.getElementById("quoteCompany").appendChild(newElem);
}

/*****************************************************************
The setOpacity function is passed an object and an opacity value. It then sets the opacity of 
the supplied object using four proprietary ways. It also prevents a flicker in Firefox 
caused when opacity is set to 100%, by setting the value to 99.999% instead.
*****************************************************************/

function setOpacity(obj, opacity)
{
	opacity = (opacity == 100)?99.999:opacity;

	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	 
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	 
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	 
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}


