ITypeCounter = 0;
FrameCounter = 0;
AdRotateInterval = new Array();
RotateLimit = 3;

//--------------------------------------------------------------------------------------------------
function writeActionTag(dummy)
{
}

function getAd(insertion_type,ad_size,site,url,qt,tid)
{
	//---- last value is set to true (rotate). Set to false to turn rotating off. ---//
	doGetAd(insertion_type,ad_size,site,url,qt,tid,false);
}

//--------------------------------------------------------------------------------------------------
function getAdRotate(insertion_type,ad_size,site,url,qt,tid)
{
	doGetAd(insertion_type,ad_size,site,url,qt,tid,true);
}

//--------------------------------------------------------------------------------------------------
function doGetAd(insertion_type,ad_size,site,url,qt,tid,rotate)
{
	var seconds = new Array();
	seconds[0] = 75; //set 3 interval lengths
	seconds[1] = 80;
	seconds[2] = 85;
	
	var delay = 1;
	if (rotate)
	{	
		for (var i = 0; i < seconds.length; i++)  
		{
			seconds[i] = seconds[i] * 1000;
		}
		// vary the delay depending on which insertion type it is so they don't all rotate together.
		if (ITypeCounter == 0) {
			ITypeCounter = 1;
			delay = 1;
		} else if (ITypeCounter == 1) {
			ITypeCounter = 2;
			delay = 5;
		} else if (ITypeCounter == 2) {
			delay = 9;
		}			
	}
	
	var descString = 'itype=' + insertion_type + '&srvc=' + site;
	var httpString = "http";
	if (document.URL.indexOf("https") != -1) {
		httpString = "https";
	}
	var tpshost = "";
	if (site == "sz") {
		tpshost = "espn.go.com";
		descString = 'AdTypes=' + insertion_type + ';';
	} else if (site == "go") {
		tpshost = "www.go.com";
		descString = 'AdTypes=' + insertion_type + ';';
	}

	if (qt != null && qt != "") 
	{
		descString += '&qt=' + qt;
	}
	if (tid != null && tid != "") 
	{
		descString += '&tid=' + tid;
	}
	if (url != null && url != "") 
	{
		descString += '&url=' + url;
	} 
	else 
	{
		var idx = document.URL.indexOf("/", 9);
		var thisloc = document.URL.substring(idx, document.URL.length);
		descString += '&url=' + thisloc;
	}

	if ((navigator.appVersion.indexOf("MSIE") != -1 || navigator.appVersion.indexOf("5.0 (") != -1) && rotate)
	{
		var xLocation = ad_size.indexOf("x");
		var iFrameWidth = ad_size.substr(0,xLocation);

		iFrameWidth = (iFrameWidth - 1) + 15 ;

		var iFrameHeight = ad_size.substr(xLocation+1);

		var adurl = httpString + "://3ps.go.com/DynamicAd?"+descString;
		if (tpshost != "") {
			adurl = httpString + "://"+tpshost+"/Adserver?CallDown&"+descString;
		}
		document.write('<IFRAME NAME="'+insertion_type+'" WIDTH=' + iFrameWidth +' HEIGHT=' + iFrameHeight + ' MARGINWIDTH=0 MARGINHEIGHT=0 HSPACE=0 VSPACE=0 FRAMEBORDER=0 SCROLLING=no src="'+adurl+'">');
		document.writeln('</IFRAME>');

		var secs = seconds[ITypeCounter -1];
		delay *= 1000;
		str = 'startAdRotation(\"'+insertion_type+'\",\"'+adurl+'\",\"'+secs+'\")';
		setTimeout(str,delay);
	}
	else
	{
		var adurl = httpString + '://3ps.go.com/DynamicJSAd?js=1&'+descString;
		if (tpshost != "") {
			adurl = httpString + '://'+tpshost+'/Adserver?CallDown&js=1&'+descString;
		}
		document.write('<SCR'+'IPT language="javascript"');
		document.write('SRC='+adurl+' >\n </SCR'+'IPT>');
	}
}

function startAdRotation(insertion_type,adurl,start_seconds) {
	// already have waited for delay up above
	var str = 'reloadAdImage(\"'+insertion_type+'\",\"'+adurl+'\")';
	AdRotateInterval[AdRotateInterval.length] = setInterval(str,start_seconds);
}

function reloadAdImage(name,adurl) {
	document.frames["" + name].location.href=adurl;
	if (RotateLimit > 0) {
		++FrameCounter;  //After all the ads have rotated n times.
		if (FrameCounter >= RotateLimit * ITypeCounter) { //Set number to maximum number of times rotate desired.
			for(i=0;i<AdRotateInterval.length;i++) {
				clearInterval(AdRotateInterval[i]);
			}
		}
	}

}