/*
//////////////////////////////////////////////////////////////////////////
Public Health Agency of Canada | Agence de sante publique du Canada


File:      featurette.js
Purporse:  This file contains javascript functions for handling the featurette on the page

Author:    Alfredo Scaini <alfredo.scaini@phac-aspc.gc.ca>
Date:      March 2009

Version:   1.0

//////////////////////////////////////////////////////////////////////////
*/

// Check to see that our array is actually there
try { 
	if (arrFeaturettes.length > 0) {}
} catch (err) {	
	var arrFeaturettes = new Array();
}

Array.prototype.previous_index = function(current_index) {
	if (current_index == 0) { 
		return (this.length-1); 
	}
	
	return (current_index-1);
}

Array.prototype.next_index = function(current_index) {
	if ((this.length - current_index) == 1) { // we've reached the end of the array
		return 0;
	}
	
	return (current_index+1);
}

var current_index  	= 0; // The starting point of our featurette box.
var next_index		= arrFeaturettes.next_index(current_index); 	
var previous_index 	= arrFeaturettes.previous_index(current_index);	

featuretteShow(current_index);
var interval_id = setInterval("featuretteSwitch()", 5000);

/*
 * featuretteShow
 *
 * This function will hide/unhide div boxes depending on the array index passed.
 *
 * @param	array_index		int		The array index that contains the value we want to display to the user
 * 
 * @return	nothing	 
 *
 */
function featuretteShow(array_index) {
	for (var i=0; i<arrFeaturettes.length; i++) {
		var featurette 					= document.getElementById(arrFeaturettes[i]);
		featurette.style.marginBottom 	= '-2px';			
		featurette.style.display 		=  (array_index == i)  ? 'block' : 'none' ;
	}
} // end of featuretteShow

/*
 * featuretteSwitch
 * 
 * This function will change the current image being displayed by changing the current index to the new one
 * 
 *
 */
function featuretteSwitch() {	
	featuretteShow();
	
	previous_index  = current_index;
	current_index   = arrFeaturettes.next_index(current_index);
	next_index		= arrFeaturettes.next_index(current_index);
	
	featuretteShow(current_index);
} // end of featuretteSwitch


/*
 * resetFeaturette
 * 
 * This function will reset the featurette image currently being displayed to the one that is passed in via the 
 * parameter listing
 *
 * @param	new_current_index	int		the array index of the image that is to be displayed
 * @return	nothing
 *
 */
function resetFeaturette (new_current_index) {
	clearInterval(interval_id);
	current_index  = new_current_index;
	previous_index = arrFeaturettes.previous_index(current_index);
	next_index	   = arrFeaturettes.next_index(current_index);
	featuretteShow(current_index);
	interval_id = setInterval("featuretteSwitch()", 5000);
} // end of resetFeaturette
