function initSideAds()
{

	rotateSideAds();
	
	// Now that all ad images are loaded, make them visible
	getElement('sideAd0').style.display='block';
	getElement('sideAd1').style.display='block';
	getElement('sideAd2').style.display='block';
}

function randomSort()
{
	return Math.random() - 0.5;
}


function rotateSideAds()
{
	// Randomly sort side ads
	sideAds.ads.sort(randomSort);
	tickles.carousels[0].ads.sort(randomSort);
	tickles.carousels[1].ads.sort(randomSort);
	
	if(sideAds.ticklesEnabled)
		rotateSideAdsHorz();
	else
		rotateSideAdsVert(sideAds);
}

// Rotate the 3 horizontal carousels of side ads
function rotateSideAdsHorz()
{

	var img, a, carousel, ix, now = new Date().getTime();
	
	// Array of indexes to the new set of ads to be displayed.
	var newAds = new Array();

	// For each row
	for(i = 0; i < 3; ++i)
	{
		// Set reference to the array of ads
		if(i == 0)
			carousel = sideAds;
		else
			carousel = tickles.carousels[i - 1];
	
		// Get index to currently displayed ad
		ix = sideAds.currAds[i];
		if(ix < 0)
			ix = 0;

		if(carousel.ads[ix].startTime == 0 || (carousel.ads[ix].startTime + carousel.ads[ix].timeout) <= sideAds.nextRotateTime)
		{
			// HTML elements making up ad
			img = getElement('sideAd' + i + 'Img');
			a   = getElement('sideAd' + i + 'Href');

			// Show ad
			showSideAd(img, a, carousel.ads[carousel.nextAd]);
	
			// Update currently displayed ad index
			sideAds.currAds[i] = carousel.nextAd;
	
			// Note time at which ad was displayed
			carousel.ads[carousel.nextAd].startTime = now;

			// Increment next ad index
			if(++carousel.nextAd >= carousel.ads.length)
				carousel.nextAd = 0;
		}
	}


	// Finally, Set next rotation time.
	for(i = 0; i < 3; ++i)
	{
		// Set reference to the array of ads
		if(i == 0)
			carousel = sideAds;
		else
			carousel = tickles.carousels[i - 1];
		// Index to currently displayed ad
		ix = sideAds.currAds[i];

		// If first of 3 ads, or display due earlier than already set rotation time
		if(i == 0 || (carousel.ads[ix].startTime + carousel.ads[ix].timeout) < (sideAds.nextRotateTime))
			sideAds.nextRotateTime = carousel.ads[ix].startTime + carousel.ads[ix].timeout;
	}

 if(getVersion()==6)
 	return;

	setTimeout('rotateSideAdsHorz()', sideAds.nextRotateTime - now);

}

// Rotate vertical ads carousel
function rotateSideAdsVert()
{
	var img, a, ix, now = new Date().getTime();

	// Array of indexes to the new set of ads to be displayed.
	var newAds = new Array();

	// Scan array of current ad indexes, adding unexpired ones to newAds array.
	for(i = 0; i < 3; ++i)
	{
		if(sideAds.currAds[i] >=0)
		{
			ix = sideAds.currAds[i];
			if((sideAds.ads[ix].startTime + sideAds.ads[ix].timeout) > now )
				newAds[newAds.length] = ix;
		}
	}
	
	// Fill up newAds array with next ads in line for display
	for(i = newAds.length; i < 3; ++i)
	{
		newAds[newAds.length] = sideAds.nextAd;		
		// Increment next ad index to
		if(++sideAds.nextAd >= sideAds.ads.length)
			sideAds.nextAd = 0;
	}	
	
	// If the number of ads is a multiple of 3, and they all have the same timeout (most likely) then the same
	// ads will always appear in the same vertival slot, so just to jazz it up visually, randomly sort them.
	// randomly sort them.
	if((sideAds.ads.length % 3) == 0)
		newAds.sort(randomSort);
	
	// Show new set of ads
	for(i = 0; i < 3; ++i)
	{	
		// HTML elements making up ad
		img = getElement('sideAd' + i + 'Img');
		a   = getElement('sideAd' + i + 'Href');
	
		// Index to ad to be shown
		ix = newAds[i];
		
		// Show ad
		showSideAd(img, a, sideAds.ads[ix])

		// Note currently displayed index
		sideAds.currAds[i] = ix;

		// Note time at which ad was displayed
		sideAds.ads[ix].startTime = now;

		// Finally, if new ad is due for show earlier than current value for next rotation time, update rotation time.
		if(sideAds.nextRotateTime == 0 || (sideAds.ads[ix].startTime + sideAds.ads[ix].timeout) < sideAds.nextRotateTime){
			if(sideAds.ads[ix].timeout < 3000 )			
				sideAds.ads[ix].timeout = 3000;		
			sideAds.nextRotateTime = sideAds.ads[ix].startTime + sideAds.ads[ix].timeout;
		}
	}
 if(getVersion()==6)
 	return;
 	
	setTimeout('rotateSideAdsVert()', sideAds.nextRotateTime - now);
}
function getVersion(){
var version = 999;

if(navigator.appVersion.indexOf("MSIE") != -1)
	version = parseFloat(navigator.appVersion.split("MSIE")[1]);
return version;
}

// Set hyperlink and tooltip attributes for ad
function showSideAd(img, a, ad)
{	
	try{
	// Update ad <a> and <img> elements
	img.src = ad.src;
	a.title = ad.title;
	a.href = ad.href;
	a.target = '_blank';

	// If <a> element's href or title attributes not given, set default values
	if(!a.href)
	{
		if(!a.title)
		{
			// No hyperlink or title. Use default values for both.
			a.href = 'mailto:info@leaguemanager.biz?subject=Advertising on ' + self.location.hostname;
			a.title = 'Click here to enquire about advertising on ' +  self.location.hostname;
		}
		else
		{
			// There is a tooltip, but no hyperlink. We can't assume the default click action
			// of opening an email to us, so set dummy hyperlink to nullify click.
			a.href = '#';
			a.target = '_self';
		}
	}
	else
	{
		// Hyperlink, but no tooltip. Set default value referring to given href.
		if(!a.title)
			a.title = 'Click here to visit ' + a.href
	}
	}catch(e){}
}


function initHeaderAds()
{
	rotateHeaderAds();
}

function rotateHeaderAds()
{
	var img = getElement('imgHeaderAdLarge');
	var a = getElement('aHeaderAdLarge');
	var ix = headerAd.nextAd;
	var div = getElement('pageHeaderAdLarge');
	var timeout = headerAd.ads[ix].timeout;
	if(timeout < 1)
		timeout = 10000;
	var title = headerAd.ads[ix].tooltip;
	var href  = headerAd.ads[ix].href;
	var src  =  headerAd.ads[ix].src;
	var target = '_blank';
	//alert(href);
	if(!href)
	{
		if(!title)
		{
			// No hyperlink or title. Use default values for both.
			href = 'mailto:info@leaguemanager.biz?subject=Advertising on ' + self.location.hostname;
			title = 'Click here to enquire about advertising on ' +  self.location.hostname;
		}
		else
		{
			// There is a tooltip, but no hyperlink. We can't assume the default click action
			// of opening an email to us, so set dummy hyperlink to nullify click.
			href = '#';
			target = '_self';
		}
	}
	else
	{
		// Hyperlink, but no tooltip. Set default value referring to given href.
		if(!title)
			title = 'Click here to visit ' + href
	}

	// Increment index to point to next image
	if(++headerAd.nextAd >= headerAd.ads.length)
		headerAd.nextAd = 0;
	if(headerAd.ads[ix].html)
	{
		div.innerHTML = headerAd.ads[ix].html;
		alert('HTML/r' + div.innerHTML);
	}
	else
	{
		div.innerHTML = '<a href="' + href + '" target="_blank"><img src="' + src + '"></a>';
	}
 if(getVersion()==6)
 	return;
	// Set timer for next rotation
	setTimeout('rotateHeaderAds()', timeout);
}




