var text_fade = 0;
var colors = ['#f0f0f0', '#f0f0f0', '#f0f0f0', '#e0e0e0', '#e0e0e0', '#d0d0d0', '#c0c0c0', '#b0b0b0', '#a0a0a0', '#909090', '#808080', '#707070', '#606060', '#505050', '#404040', '#303030', '#202020', '#101010', '#000000' ];
var timerID = 0;
var currAnimation = 0;
var secondsBetweenAds = 7; 
var paused = false;

function change_ad(img_path)
{
	crossfade(document.getElementById('ad_image'), img_path, '2', 'picture of car', 285);
}

function pause() 
{
	paused = true;
	$('ad-pause-button-img').src = "/images/pause-down.png";
	$('ad-play-button-img').src = "/images/play.png";
}

function play()
{
	paused = false;
	next_ad();
	$('ad-pause-button-img').src = "/images/pause.png";
	$('ad-play-button-img').src = "/images/play-down.png";
}

function show_ad_text()
{
  if (timerID)
   {
      clearTimeout(timerID);
   }
	
	if (text_fade == 19)
	{
		// We're done fading the text. Wait for next ad.
	   timerID = setTimeout("next_ad()", secondsBetweenAds*1000);			
	}
	else
	{
		text_fade++;
		var t = document.getElementById('ad_testimonial2');
		t.innerHTML = ad_testimonial[currAnimation];
		t.style.color = colors[text_fade];
		t = document.getElementById('ad_customer2');
		t.innerHTML = '— ' + ad_customer[currAnimation];
		t.style.color = colors[text_fade];
	    timerID = setTimeout("show_ad_text()", 100);
	}
}

function first_ad()
{
	setTimeout("cacheImages('/images/ads/', ad_image_path)", 50);
       timerID = setTimeout("next_ad()", secondsBetweenAds * 1000);
}

function next_ad()
{
	if( paused ) {
		return;
	}
	
 	if (++currAnimation == ad_count) {
 		currAnimation = 0;
 	}
 	
 	var t = document.getElementById('ad_testimonial2');
 	t.innerHTML = "";
 	t.style.color = '#ffffff';
 	t = document.getElementById('ad_customer2');
 	t.innerHTML = "";
 	t.style.color = '#ffffff';
 	
 	if (timerID) {
 		clearTimeout(timerID);
 	}
 	
 	change_ad("/images/ads/" + ad_image_path[currAnimation]);
 	
 	text_fade = 0;
 	timerID = setTimeout("show_ad_text()", 3 * 1000);
}

