// The following function cycles through each banner image.
// Notice the use of the variable thisAd to go from 0 to 2 and then
// back to 0. JavaScript has an object called document whose
// member adBanner can be set to the current banner to display.

function cycleAds(pause) {
	if (!pause) {
		if (document.images) {
	        if (document.adBanner.complete) {
	            if (++thisAd == adImages.length) thisAd = 0;
	            document.adBanner.src = adImages[thisAd];
	        }
	    }
		document.getElementById('captions').innerHTML = adCaptions[thisAd];
		switchAd(thisAd);
	    // change to next banner every 15 seconds if pause = false
	    lastTimeoutId = setTimeout("cycleAds(false)", 7200);
	} else {
		if (typeof(lastTimeoutId) !== 'undefined') clearTimeout(lastTimeoutId);
	}
}

function setAd(adId) {
    cycleAds(true);
	if (document.images) {
        if (document.adBanner.complete) {
            if (++adId == adImages.length) adId = 0;
            document.adBanner.src = adImages[adId];
        }
    }
	document.getElementById('captions').innerHTML = adCaptions[adId];
	switchAd(adId);
    thisAd = adId;
	// change to next banner every 15 seconds if pause = false
}

function switchAd(thisAd) {
	switch(thisAd) {
		case 0:
			document.one.src = "/assets/img/homeFlashSelected.gif";
			document.two.src = "/assets/img/homeFlashDeselected.gif";
			document.three.src = "/assets/img/homeFlashDeselected.gif";
			document.four.src = "/assets/img/homeFlashDeselected.gif";
			break;
		case 1:
			document.one.src = "/assets/img/homeFlashDeselected.gif";
			document.two.src = "/assets/img/homeFlashSelected.gif";
			document.three.src = "/assets/img/homeFlashDeselected.gif";
			document.four.src = "/assets/img/homeFlashDeselected.gif";
			break;
		case 2:
			document.one.src = "/assets/img/homeFlashDeselected.gif";
			document.two.src = "/assets/img/homeFlashDeselected.gif";
			document.three.src = "/assets/img/homeFlashSelected.gif";
			document.four.src = "/assets/img/homeFlashDeselected.gif";
			break;
		case 3:
			document.one.src = "/assets/img/homeFlashDeselected.gif";
			document.two.src = "/assets/img/homeFlashDeselected.gif";
			document.three.src = "/assets/img/homeFlashDeselected.gif";
			document.four.src = "/assets/img/homeFlashSelected.gif";
			break;
	}
}

function previousAd(currentAd) {
	if (currentAd <= 0) currentAd = (adImages.length);
	setAd(currentAd-3);
	cycleAds(false);
}

// This function is used to direct the user to the website when
// the user clicks on a particular banner image.

function gotoAd() {
    document.location.href = adURLs[thisAd];
}