﻿//Global variable adList - created in function initializeAd() in code behind on default.aspx.vb
//	-list of objects, containing information for ad rotation

var randomList = new Array();
var adCount = -1;

function adRotator(lnkID, imgID)
{
	if(adCount == -1 || adCount == adList.length-1)
	{
		if(adCount == -1)
		{
			randomList.length = 0;
			createRandomList();
		}
		adCount = 0;		
	}
	else
	{ adCount++; }

	updateAd(lnkID,imgID);


	setTimeout(function(){adRotator(lnkID, imgID);},14000);
}


function updateAd(lnkID, imgID)
{
	var lnk = document.getElementById(lnkID);
	var img = document.getElementById(imgID);
	
	lnk.href = adList[randomList[adCount]].NavigateUrl;
	img.src = adList[randomList[adCount]].ImageUrl;
	img.alt = adList[randomList[adCount]].AlternateText;
}

function createRandomList()
{
	while(randomList.length < adList.length)
	{
		var rNumber = Math.floor(Math.random()* adList.length);
		var inArray = false;
		
		var leng = randomList.length;
		
		if(leng == 0)
		{
			randomList.push(rNumber)
			continue;
		}
		
		for(i=0;i<leng;i++)
		{
			if(randomList[i] == rNumber)
			{ inArray = true; }
		}
		
		if(!inArray)
		{ randomList.push(rNumber); }
	}
	
}

