var timer;
var t;
var aCookies;

function rotateAf()
{
	var arrAf = 
		{
//		'www.pidplates.us/partner/banner/banner_234_60.swf?regid=0200917X'	: 'http://www.pidplates.us/?regid=0200917X',
//		'pub-auto.jpg'			: 'http://www.artcurial.com/en/asp/searchresults.asp?pg=1&ps=25&st=D&sale_no=1659+++',
		'5375-23_AM10_Anamera.gif'	: 'http://www.rmauctions.com/?LinkID=25&sc=681252020E6FEBEE80101FE0DF931681',
		'anamericaBanner_1.gif'		: 'http://www.GoodingCo.com/main.html',
		'Talacrest.gif'			: 'http://www.Talacrest.com',
//		'5011-51_LF09_Anamera2.gif'	: 'http://www.rmauctions.com/default.cfm?SaleCode=LF09',
		'MaranelloRosso01.swf'		: 'http://www.maranellorosso.com',
//		'rally_banner_c.gif'		: 'http://www.RallyNippon.org',
		'LeMay.jpg'			: 'http://www.LeMayMuseum.org',
//		'auto2.jpg'			: 'http://www.anamera.com/en/dealer-inventory/index.html?no_cache=1&tx_anamerashowroom_pi1%5BMODE%5D=dealer&tx_anamerashowroom_pi1%5BFILTER%5D%5Bdealer%5D=1485&request%5Bshow%5D=1',
// Filler Ads
		'180x150_anamera_04.png'	: 'http://www.Anamera.com',
//		'Rinkens.png'			: 'http://www.Rinkens.com',
//		'luzzago_151108_ad.png'		: 'http://www.anamera.com/index.php?id=269&no_cache=1&tx_anameraevents_pi2[showUid]=1483',
//		'CarPrice.jpg'			: 'http://www.CarPrice.com',
//		'127_anamera.png'		: 'http://www.Anamera.com',
		// Default Ad - DO NOT REMOVE
		'fad_default_ad.png'		: 'http://www.anamera.com/en/about/advertising/index.html'
		}

	var imgRoot = "/All.Ferraris/scripts/";
	var nAds;

	if ( !nAds )
		{
		var i = 0;
		for ( strImg in arrAf )	i++;
		nAds = i - 1;

		i = Math.floor( Math.random() * nAds );

		// prevent same ad from showing twice in a row
		var popNumber = cookieAf('fadnumber');
		if ( i == popNumber ) i--;
		if ( i < 0 )
			i = nAds;
		document.cookie = 'fadnumber=' + i + '; path=/;';	

		for ( strImg in arrAf )
			{
			if ( i <= 0 )
				{
				if ( strImg.substr( strImg.length-4, 4) == '.swf' || strImg.indexOf( ".swf?" > 6 ) )
					{
					// Embed Element
					var ndEmbed = document.createElement( 'embed' );
					with ( ndEmbed )
						{
						setAttribute( 'name', 'fa_ad_movie' );
						setAttribute( 'width', 156 );
						setAttribute( 'height', 312 );
						setAttribute( 'src', imgRoot + strImg );
						setAttribute( 'quality', 'high' );
						setAttribute( 'wmode', 'opaque' );
						setAttribute( 'type', 'application/x-shockwave-flash' );
						setAttribute( 'quality', 'high' );
						setAttribute( 'pluginspage', 'http://www.macromedia.com/go/getflashplayer' );
						setAttribute( 'swliveconnect', 'true' );
						}

					// Object Parameters
					var ndParam1 =  document.createElement( 'param' );
					with ( ndParam1 )
						{
						setAttribute( 'name', 'movie' );
						setAttribute( 'value', imgRoot + strImg );
						}
					var ndParam2 =  document.createElement( 'param' );
					with ( ndParam2 )
						{
						setAttribute( 'name', 'quality' );
						setAttribute( 'value', 'high' );
						}
					var ndParam3 =  document.createElement( 'param' );
					with ( ndParam3 )
						{
						setAttribute( 'name', 'wmode' );
						setAttribute( 'value', 'opaque' );
						}
					var ndParam4 =  document.createElement( 'param' );
					with ( ndParam4 )
						{
						setAttribute( 'name', 'menu' );
						setAttribute( 'value', 'true' );
						}

					// Assemble the object tag
					var ndObject =  document.createElement( 'object' );
					with ( ndObject )
						{
						setAttribute( 'id', 'fa_ad_movie' );
//						setAttribute( 'classid', 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' );
//						setAttribute( 'codebase', 'http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' );
						setAttribute( 'width', 156 );
						setAttribute( 'height', 312 );
						appendChild( ndParam1 );
						appendChild( ndParam2 );
						appendChild( ndParam3 );
						appendChild( ndParam4 );
						try { appendChild( ndEmbed ) } catch(e) {};
						}
						
					// replace the image with the object tag
					var ndImage = document.getElementsByName( "fa_ad_img" );
					ndImage = ndImage[0];
					var ndImageParent = ndImage.parentNode;
					ndImageParent.replaceChild( ndObject, ndImage );	
	
					// play the movie
					var ndMovie = document.getElementById( "fa_ad_movie" );
					if ( ndMovie ) 
						{
//						ndMovie.movie = imgRoot + strImg;
						ndMovie.setAttribute( 'classid', 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' );
						ndMovie.setAttribute( 'codebase', 'http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' );
						}
					}
				else
					{
					document.images["fa_ad_img"].src = imgRoot + strImg;
					document.images["fa_ad_img"].alt = "Advertisement";
					}
				var objHREF = document.getElementById("fa_ad_href");
				objHREF.href = arrAf[ strImg ];
				break;
				}
			i--;
			}
		}
}

function startAf() 
{
	var popThreshold = 3;

	var popCount = cookieAf('fadpopup');
	if ( popCount == null ) 
		popCount = 0;

	if ( popCount <= 1 ) 
		{
		popCount = popThreshold;
		if ( document.getElementById("fa_ad_href") )
			rotateAf();
		setLeft();
		showAf();
		}	
	else
		popCount--;

	document.cookie = 'fadpopup=' + popCount + '; path=/;';
}

function cookieAf(name) 
	{
	if ( !aCookies )
		aCookies = document.cookie.split(';');

	var ca = aCookies;
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function showAf() {
	t = 100;
	moveUp(100000);
	fade(0);	
}

function hideAf() {
	t = 0;
	fade(100);
}

function fade(amt) {
	if((t == 100 && amt <= t) || (t == 0 && amt >= t)) setFade(amt);

	if(amt == t && t==0) 
		{
		moveUp(-1);
		
		// Hide DIV after faded out and stop interval
		var obj;
	
		if (document.layers) obj = document.layers.fa;
		else if (document.all) obj = document.all.fa;
		else if (document.getElementById) obj = document.getElementById("fa");
	
		obj.style.visibility="hidden";

		return;
		}
    
	amt += (t > 0)? 10:-10;
	
    if(timer!=null) clearInterval(timer);
	timer = setTimeout("fade("+amt+")", 20);
}

function setFade(amt) {
	var obj;
	
	if (document.layers) obj = document.layers.fa;
	else if (document.all) obj = document.all.fa;
	else if (document.getElementById) obj = document.getElementById("fa");
	
	amt = (amt == 100)?99.999:amt;
  
	// IE
	obj.style.filter = "alpha(opacity:"+amt+")";
  
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = amt/100;
  
	// Mozilla and Firefox
	obj.style.MozOpacity = amt/100;
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = amt/100;
}

function setLeft() {
	var w = 250;	// set this to the same value of the width property for the pa div in the style sheet, 
					// smaller to move it to the right, larger to move it to the left

	if (document.layers) document.layers.fa.left = ((window.innerWidth / 2) - (w / 2))+"px";
	else if (document.all) document.all.fa.style.left = ((document.body.offsetWidth / 2) - (w / 2))+"px";
	else if (document.getElementById) document.getElementById("fa").style.left = ((window.innerWidth / 2) - (w / 2))+"px";
}

function moveUp(amt) {
	if (document.layers) document.layers.fa.zIndex = amt;
	else if (document.all) document.all.fa.style.zIndex = amt;
	else if (document.getElementById) document.getElementById("fa").style.zIndex = amt;
}