// global variables
var adArray = null;
var imageArray = null;
var currentAd = 0;
var mainAd = null;
var counter = 0;
var currentTimeOutID = 0;

function parseLinkImage(adLinkArray, adImagePathArray, adCaptionArray, adIndex, adWidth, adHeight)
{
	if (adLinkArray[adIndex] != null)
		document.write("<div><a href=\"/adredir.asp?url=" + adLinkArray[adIndex] + "&adv="+adImagePathArray[adIndex]+"\"><img src=\"" + adImagePathArray[adIndex] + "\" alt=\"" + adCaptionArray[adIndex] + "\" border=0 width="+adWidth+" height="+adHeight+" ID=\"image" + adIndex + "\"></a></div>");
	else
		document.write("<div><img src=\"" + adImagePathArray[adIndex] + "\" ID=\"image" + adIndex + "\"></div>");
}

function parseNav(adImagePathArray2)
{
	document.write("<div align=\"right\"><font size=1 face=arial style=\"color: #b1b4b5\">");
	var selectedIndex = 0;
	for (j = 0; j < adImagePathArray2.length; j++)
	{
		selectedIndex = j + 1;
		document.write("<a href=\"#\" onclick=\"runEffect('" + selectedIndex + "');\" class=hrjs>" + selectedIndex + "</a> | ");
	}
	document.writeln("</font><a href=\"#\" onclick=\"runEffect('prev');\"><FONT face=webdings >3</FONT></a><img src=\"\/images\/spacer.gif\" width=\"6\" height=\"1\"><a href=\"#\" onclick=\"runEffect('next');\"><FONT face=webdings >4</FONT></a></div>");
}

function runEffect(whichAd)
{
	if (counter++ == 0)
	{
		// next loop after a preset period
		currentTimeOutID = setTimeout("runEffect()", hdDuration);
		return;
	}

	// define filter style
	mainAd.style.filter = "blendTrans(duration=1) revealTrans(duration=1,transition=5)";
	
	// freeze current state
	mainAd.filters(0).apply();
	mainAd.filters(1).apply();

	// turn off current one
	adArray[currentAd].display = "none";
	
	// find out the next one
	if (!isNaN(parseInt(whichAd)))
	{
		currentAd = parseInt(whichAd) - 1;
	}	
	else if (whichAd == "prev")
	{
		var previousAd = currentAd - 1;
		if (previousAd <= -1)
			previousAd = imageArray.length - 1;
		currentAd = previousAd;
	}
	else
	{
		var nextAd = (currentAd + 1) % imageArray.length;
		currentAd = nextAd;
	}
	
	// turn the next one visible
	adArray[currentAd].display = "block";

	// execute filter
	mainAd.filters(0).play();
	mainAd.filters(1).play();

	// clear previously set time out if previous and next buttons is clicked
	if ((!isNaN(parseInt(whichAd))) || whichAd == "prev" || whichAd == "next")
		clearTimeout(currentTimeOutID);
		
	// next loop after a preset period
	currentTimeOutID = setTimeout("runEffect()", hdDuration);
}

function filterEffect(adLinkArray, adImagePathArray, adCaptionArray, adWidth, adHeight, showNav)
{
	// generate random number
	// var randomChoice = Math.floor(Math.random() * adLinkArray.length);
	var randomChoice = 0;
	
	if (adImagePathArray.length <= 1 || !(isIE5plus()))
	{
		parseLinkImage(adLinkArray, adImagePathArray, adCaptionArray, randomChoice, adWidth, adHeight);
		return;
	}
	
	currentAd = randomChoice;
	document.writeln("<div id=\"mainAd\" style=\"width:" + adWidth + "px;height:" + adHeight + "px\">");

	for (i = 0; i < adImagePathArray.length; i++)
	{
		document.write("<div id=\"ad" + i + "\" style=\"display:");

		if (i == randomChoice)
			document.writeln("block\">");
		else
			document.writeln("none\">");

		parseLinkImage(adLinkArray, adImagePathArray, adCaptionArray, i, adWidth, adHeight);
		
		if (showNav != "")
			parseNav(adImagePathArray);

		document.writeln("</div>");
	}

	document.writeln("</div>");

	adArray = new Array(adImagePathArray.length);
	imageArray = new Array(adImagePathArray.length);

	for (i = 0; i < adImagePathArray.length; i++)
	{
		adArray[i]	= document.getElementById("ad" + i).style;
		imageArray[i]	= document.getElementById("image" + i);
	}

	mainAd = document.getElementById("mainAd");
	
	runEffect();
}

function isLAN()
{
	try
	{
		document.body.addBehavior ("#default#clientCaps");
		var isLAN = (typeof(document.body.connectionType) != "undefined" && document.body.connectionType == "lan") ? true : false;
		return isLAN
	}
	catch(e)
	{
		return false;
	}
}

function isIE5plus()
{
	var browser = navigator.userAgent;
	var startPos = browser.indexOf("MSIE");
	if (startPos < 0)
		return false;

	var IEversion = parseInt(browser.substring(startPos+5, browser.indexOf(".", startPos)));
	
	if (IEversion < 5)
		return false;
	else
		return true;
}
	
	