var feedindexmax = 2;
var feedindex = 0;
var adTimeoutObj = "";
var nullads = new Array();
var attemptedads = new Array();
var timerset = 0;


function loadad(zoneid,adhead,html,alerthtml)
{
	html = trimad(html);
	if(zoneid)
	{	
		//if(alerthtml)
		//	alert(html);
			
		if(html)
		{		
			if(document.getElementById('adbody_'+zoneid))
			{
				if(adhead)
				{
					document.getElementById('adhead_'+zoneid).innerHTML = adhead;			
					document.getElementById('adhead_'+zoneid).style.display='block';
				}
				document.getElementById('adbody_'+zoneid).innerHTML =  html + "<div>&nbsp;</div>";
				document.getElementById('adbody_'+zoneid).style.display='block';	
					
				if(alerthtml)
					attemptedads.push(zoneid);
			}
		}
		else
			nullads.push(zoneid);		
	}
}

function intiateAdTimer()
{
	//Set a timer to check all the ads and make sure they loaded
	adTimeoutObj = setTimeout("checkTimedOutAds()",1000);
}

function checkTimedOutAds()
{
	//Clear the timer
	clearTimeout(adTimeoutObj);
	
	//This checks the clientHeight to see if and adLoaded
	for(var key in attemptedads)
	{
		zoneid = attemptedads[key];
		if(document.getElementById('adbody_'+zoneid).offsetHeight < 50)
			nullads.push(zoneid);	
	}
	
	if(nullads.length)
		rotatenullads();
		
	attemptedads = new Array();
	nullads = new Array();	
}

function rotatenullads()
{
	feedindex = feedindex + 1;
	if(feedindex <= feedindexmax)
	{	
		zonestr = nullads.join(",");
		ad_iframe = document.getElementById('ad_iframe');
		ad_iframe_location = "/reviews/sitemapping/display_ads2.php?default=outside-forum&adhead=1"+ "&zone=" + zonestr + "&feedindex="+feedindex;

		if(ad_iframe.src)
			ad_iframe.src = ad_iframe_location;	
		else if(ad_iframe.location)
			ad_iframe.location = ad_iframe_location;
		else if(ad_iframe.location.href)
			ad_iframe.location.href = ad_iframe_location;
	}
}


function rotatead(zone,feedindex)
{
	alert("Function not finished - zone:"+zone+", feedindex:"+feedindex);
}


function trimad(html)
{
	return html.replace(/^\s+|\s+$/g, '');
}
