﻿//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 randomList2 = new Array();
var adCount = -1;
var adCount2 = -1;
var speed = 10000;
var oldad1 = '';
var oldad2 = '';

function updateAd(lnkID, imgID, titleID, textID, rotatorno)
{
	var index;
	var lnk = document.getElementById(lnkID);
	var img = document.getElementById(imgID);
	var title = document.getElementById(titleID);
	var text = document.getElementById(textID);

	//-- get index number from appropriate list
	( rotatorno == 1 )? index = randomList[adCount] : index = randomList2[adCount2]; 
	( rotatorno == 1 )? oldad1 = adList[ index ].Title : oldad2 = adList[ index ].Title;


	//-- prevent ad 1 from being the same as ad 2
	if ( rotatorno == 1 )
	{ if ( oldad1 == oldad2 )
	  { 
	   if ( index++ > randomList.length )
	    { index = 0;  }
	   else if ( index-- <= 0 )
	    { index = randomList.length-1; }
	   else 
	    { index++; } 
	  }
	}

	//-- prevent ad 2 from being the same as ad 1
	if ( rotatorno == 2 )
	{ if ( oldad2 == oldad1 )
	  { 
	   if ( index++ > randomList2.length )
	    { index = 0;  }
	   else if ( index-- <= 0 )
	    { index = randomList2.length-1; }
	   else 
	    { index++; } 
	   
	  }
	}
	
	try
	{  ( rotatorno == 1 )? oldad1 = adList[ index ].Title : oldad2 = adList[ index ].Title; }
	catch(e)
	{ /*alert( rotatorno + '|' + index );*/ }
	
	try
	{
	//-- set attributes to new item		
	lnk.onclick = new Function( "window.location = '" + adList[ index ].NavigateUrl + "';" );
	img.src = adList[ index ].ImageUrl;
	img.alt = adList[ index ].AlternateText;
	title.innerHTML = adList[ index ].Title;
	text.innerHTML = adList[ index ].Text;
	}
	catch(e)
	{ /*alert( rotatorno + '|' + index );*/ }
	

} // [ end updateAd ]




function adRotator( lnkID, imgID, titleID, textID, rotatorno )
{   
	var cnt;
    ( rotatorno == 1 )? cnt = adCount : cnt = adCount2; 
	
	if( cnt == -1 || cnt == adList.length-1)
	{
		if( cnt  == -1)
		{  
		  ( rotatorno == 1 )? randomList.length = 0 : randomList2.length = 0; 
		  createRandomList( rotatorno ); 
		}
		
		cnt = 0;		
	}
	else
	{ cnt++; }

	//-- update count for appropriate item
	( rotatorno == 1 )? adCount = cnt : adCount2 = cnt;

	
	updateAd( lnkID, imgID, titleID, textID, rotatorno );

	setTimeout( function(){adRotator(lnkID, imgID, titleID, textID, rotatorno );}, speed );

} // [ end adRotator ]




function createRandomList( rotatorno )
{
    var list;
    ( rotatorno == 1 )? list = randomList : list = randomList2; 
    
	while( list.length < adList.length )
	{
		var rNumber = Math.floor(Math.random()* adList.length);
		var inArray = false;
		
		var len = list.length;
		
		if(len == 0)
		{
			list.push(rNumber)
			continue;
		}
		
		for( i=0; i<len; i++ )
		{
			if( list[i] == rNumber )
			{ inArray = true; }
		}
		
		if(!inArray)
		{ list.push(rNumber); }
	}
	
	( rotatorno == 1 )? randomList = list : randomList2 = list;

} // [ end createRandomList ]


