var g_hdl_moduleAdvertismentRotateTimeout = null; //!< Handle for the timeout on the rotation.
var ads = new Array(); //!< Array of ads to be displayed on the page.
var newAd = 0; //!< Counter to keep track of the ad that is currently displayed.
var totalAds = 0; //!< the number of ads for this page.
var g_moduleAdvertisementTimeToDisplay = 5000; //!< The time, in milliseconds, the rotation should 'pause'.

/**
	@brief Used to change the advertisement which is currently displayed.
	This method performs the "rotation" service and it is set to run 
	at an interval (in seconds) that is configured by the user.
 */
function Ad(image, link, altText, description) {
	this.image = image; // String containing the location of the image.
	this.link = link; // String containing the URI link for the ad.
	this.altText = altText; // String containing the alt-text for the image.
	this.description = description; // String containing a description of the ad.
}

/**
	@brief Used to change the advertisement which is currently displayed.
	This method performs the "rotation" service and it is set to run 
	at an interval (in seconds) that is configured by the user.
 */
function cycleAds() {

	var sponsorTag = null; // temp variable that stores the sponsor link element.
	var adTag = null; // temp variable that stores the avertisement link element.
	var imageTag = null; // temp variable that stores the ad's image element.

	if (0 < totalAds) {

		newAd++;

		// determine if advertisements are ready to be repeated.
		if (newAd == totalAds) {
			newAd = 0;
		}
		sponsorTag = document.getElementById('module-advertisement-sponsor');
		if (sponsorTag != null) {
			sponsorTag.href = ads[newAd].link;
		}
		adTag = document.getElementById('module-advertisement-link');
		if (adTag != null) {
			adTag.href = ads[newAd].link;
		}
		imageTag = document.getElementById('module-advertisement-image');
		if (imageTag != null) {
			imageTag.src = ads[newAd].image;
			imageTag.alt = ads[newAd].altText;
		}
		// set the time below for length of image display
		/* Verify that the hdl for the timeout isn't currently being used. */
		if(g_hdl_moduleAdvertismentRotateTimeout != null) {
			clearTimeout(g_hdl_moduleAdvertismentRotateTimeout);
		}
		g_hdl_moduleAdvertismentRotateTimeout = setTimeout("cycleAds()", g_moduleAdvertisementTimeToDisplay);
	}
}

/**
	@brief Used to start the 'rotation' of the advertisements.
	This method also correctly formats the configurable rotation time
	into milliseconds.

	@param displayTime Int containing the number of seconds the rotation
	should 'pause' for an advertisement.
 */
function startAdCycle(displayTime) {
    if (null != displayTime) { 
    	g_moduleAdvertisementTimeToDisplay = displayTime * 1000;
    }
	cycleAds();
}

