//note: ad2 is currently not displayed
var cacheImage1 = new Image();
var cacheImage2 = new Image();
var adDelay = 5000;	//in milliseconds
var pauseAdRotation = false;

displayAds();

function displayAds() {
	var i;
	var adMatch = false;
	
	if (exhibitorID > 0 && adID[ad1Index] != exhibitorID) {
		i = 0;
		while (!adMatch && i < adID.length) {
			if (adID[i] == exhibitorID) adMatch = true;
			i++;
		}
		if (adMatch) {
			ad1Index = i - 1;
			while (adID[ad2Index] == exhibitorID) {
				ad2Index++;
				if (ad2Index >= adID.length) ad2Index = 0;
			}
		}
	}
	
	//document.getElementById("ad2").src = baseDir + 'images/ads/normal/' + adFile[ad2Index];
	if (adID[ad1Index] != exhibitorID) {
		document.getElementById("ad1").src = baseDir + 'images/ads/normal/' + adFile[ad1Index];
		document.getElementById('ad_button').style.display = 'none';
	} else {
		document.getElementById("ad1").src = baseDir + 'images/ads/selected/' + adFile[ad1Index];
		document.getElementById('ad_button').style.display = 'inline';
	}
	
	i = ad2Index;
	i++;
	if (i >= adID.length) i = 0;
	cacheImage1.src = baseDir + 'images/ads/normal/' + adFile[i];
	cacheImage2.src = baseDir + 'images/ads/selected/' + adFile[i];
	
	setTimeout('rotateAds();', adDelay);
}

function rotateAds() {
	if (!pauseAdRotation) {
		if (exhibitorID > 0 && adID[ad1Index] == exhibitorID) {
			ad1Index++;
			if (ad1Index >= adID.length) ad1Index = 0;
			while (adID[ad1Index] != exhibitorID) {
				ad1Index++;
				if (ad1Index >= adID.length) ad1Index = 0;
			}
			
			ad2Index++;
			if (ad2Index >= adID.length) ad2Index = 0;
			while (adID[ad2Index] == exhibitorID) {
				ad2Index++;
				if (ad2Index >= adID.length) ad2Index = 0;
			}
		} else {
			ad2Index++;
			ad1Index = ad2Index - 1;
			if (ad2Index >= adID.length) ad2Index = 0;
			if (ad1Index < 0) ad1Index = 0;
		}
		
		displayAds();
	}
}

function ad(whichAd) {
	var s;

	if (whichAd == 1) {
		exhibitorID = adID[ad1Index];
	} else {
		exhibitorID = adID[ad2Index];
		ad1Index = ad2Index;
		ad2Index = ad1Index + 1;
		if (ad2Index >= adID.length) ad2Index = 0;
		while (adID[ad2Index] == exhibitorID) {
			ad2Index++;
			if (ad2Index >= adID.length) ad2Index = 0;
		}
	}
	
	s = clearSearch(location.href);
	s += '&id=' + showDirArticleID.toString();
	s += '&eid=' + exhibitorID.toString();
	s += '&ad1=' + ad1Index.toString();
	s += '&ad2=' + ad2Index.toString();
	//s += '#x';
	
	location = s;
}

function viewOtherAds() {
	var s;
	
	pauseAdRotation = true;
	
	exhibitorIndex = 0;
	ad2Index++;
	ad1Index = ad2Index - 1;
	if (ad2Index >= adID.length) ad2Index = 0;
	if (ad1Index < 0) ad1Index = 0;
	
	s = clearSearch(location.href);
	s += '&id=' + showDirArticleID.toString();
	s += '&ad1=' + ad1Index.toString();
	s += '&ad2=' + ad2Index.toString();
	//s += '#x' + exhibitorID.toString();
		
	location = s;
}

