var interstitialSecondsRemaining;

function startInterstitialCountDown() {
	interstitialSecondsRemaining = 15;
	updateInterstitialCountDown();
	//setTimeout(updateInterstitialCountDown, 500); // Wait 1/2 second before starting the countdown
}

function updateInterstitialCountDown() {
	if ( interstitialSecondsRemaining > 0 ) {
		if ( document.getElementById ) {
			document.getElementById("interstitial_countdown").innerHTML = ( interstitialSecondsRemaining.toString().length < 2 ? "&nbsp;" : "") + interstitialSecondsRemaining;
		}
		setTimeout(updateInterstitialCountDown, 1000);
	} else hideInterstitial();
	interstitialSecondsRemaining--;
}

function showHideOtherAds(show) {
	var i;
	var j;
	var k;
	var tmp;
	var is_interstitial_child;
	var objs;
	if ( document.getElementById ) {
		// Hide objects and embeds that might show through (ex. Flash in Linux)

		var arrayIdsToHide = new Array('topleaderboard', 'bottomleaderboard', 'adbar', 'center_tile_1', 'center_tile_2', 'footer');
		for ( i = 0; i < arrayIdsToHide.length; i++ ) {
			if ( tmp = document.getElementById(arrayIdsToHide[i]) ) {
				tmp.style.visibility = ( show ? 'visible' : 'hidden');
				//tmp.style.display = ( show ? 'block' : 'none');
			}
		}

		var arrayTagNamesToHide = new Array('EMBED', 'OBJECT');
		for ( i = 0; i < arrayTagNamesToHide.length; i++ ) {
			objs = document.getElementsByTagName(arrayTagNamesToHide[i]);
			for ( j = 0; j < objs.length; j++ ) {
				// Search through parents to make sure it isn't part of the interstitial
				is_interstitial_child = false;
				tmp = objs[j].parentNode;
				while ( tmp && ( ! is_interstitial_child ) ) {
					if ( tmp.id == 'interstitial' ) is_interstitial_child = true;
					tmp = tmp.parentNode;
				}
				if ( ! is_interstitial_child ) objs[j].style.visibility = ( show ? 'visible' : 'hidden');
			}
		}
	}
}

function showHideInterstitial(show) {
	var interstitialOverlay;
	var mainContainer;
	if ( document.getElementById && ( interstitialOverlay = document.getElementById('interstitial_overlay') ) ) {

		if ( mainContainer = document.getElementById('interstitial_helper') ) {
			if ( show ) {
				mainContainer.style.overflow = 'hidden';
				mainContainer.style.height = '1200px';
				mainContainer.style.backgroundColor = "white";
				document.body.style.backgroundColor = "black";
				interstitialOverlay.style.display = 'block';
				hideOtherAds();
			} else {
				mainContainer.style.overflow = 'visible';
				mainContainer.style.height = 'auto';
				document.body.style.backgroundColor = "white";
				interstitialOverlay.style.display = 'none';
				showOtherAds();
				addEvent(window, 'load', showOtherAds); // In case the user dismisses the ad before page loads.
			}
			return true;
		}
	}
	return false;
}
// function showInterstitial() { showHideInterstitial(true); addEvent(window, 'load', startInterstitialCountDown); }
function showInterstitial() { showHideInterstitial(true); }
function hideInterstitial() { showHideInterstitial(false); }

function showOtherAds() { showHideOtherAds(true); }
function hideOtherAds() { showHideOtherAds(false); }
