
function startAnimation( hasLoadedHomePageRecently )
{	
	if( ! hasLoadedHomePageRecently )
		slideLogo();
	else
		changeQuotes( true );
}

var kTableWidth = 725;
var kFinalColumnWidth = 160;
var kSlideTime = 2000;
var kInitialOffset = 200;
var kPI = 3.1415;
var startSlideTime = -1;

function slideLogo()
{
	var t = new Date();
	var curTime;
	var curWidth;
	var x, y;
	var xDistortion;

	curTime = t.getTime();

	if( startSlideTime == -1 )
	{
		startSlideTime = curTime - kInitialOffset;
		setTimeout("slideLogo()", 1 );
	}
	else if( curTime - startSlideTime > kSlideTime )
	{
		// finish it
		curWidth = kFinalColumnWidth;
		document.getElementById('LogoCell').width = curWidth;
		changeQuotes();
	}
	else if( curTime - startSlideTime > kSlideTime / 2 )
	{
		// second half of slide
		x = kPI * (curTime - startSlideTime) / kSlideTime;
		x = -x + kPI; // flip the graph along the y axis and offset by pi
		xDistortion = x * 2.0 / kPI;

		x *= xDistortion;

		y = 1.0 - (1.0 + Math.cos( x ) ) / 2.0; // note the "1.0 -" that flips the thing vertically
		curWidth = kFinalColumnWidth + (kTableWidth - kFinalColumnWidth) * y;

		document.getElementById('LogoCell').width = curWidth;
		setTimeout("slideLogo()", 1 );
	}
	else
	{
		// first half of slide
		x = kPI * (curTime - startSlideTime) / kSlideTime;
		xDistortion = x * 2.0 / kPI;
		
		x *= xDistortion;

		y = (1.0 + Math.cos( x ) ) / 2.0;
		curWidth = kFinalColumnWidth + (kTableWidth - kFinalColumnWidth) * y;
		
		document.getElementById('LogoCell').width = curWidth;
		setTimeout("slideLogo()", 1 );
	}
	
}

function getRandomNum(lbound, ubound) {
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

var curQuote = -1;
var cycleStartTime;
var cycleInitialCycleTime;
var firstCycleFinished;
var quoteGroupCount;

var kCycleTime = 7500;
var kInitialCycleOffset = 100;
var kFadeTime = 500;

function changeQuotes( dontAnimate )
{
	var newQuote;
	var curTime;
	var newColor;
	var mt_r, mt_g, mt_b;
	var t = new Date();

	curTime = t.getTime();

	if( curQuote != -1 )
	{
		// we should already be at the next cycle, possibly many cycles ahead (if we were sleeping, etc.)
		if( curTime > cycleStartTime + kCycleTime )
		{
			// fast forward as many cycles as we need to get us current
			while( cycleStartTime + kCycleTime < curTime )
				cycleStartTime += kCycleTime;

			document.getElementById('QuoteGroup_' + curQuote).style.display = 'none';
			document.getElementById('QuoteGroup_' + curQuote).style.color = '';
			curQuote = curQuote + 1;
			if( curQuote == quoteGroupCount ) curQuote = 0;

			document.getElementById('QuoteGroup_' + curQuote).style.display = '';
			document.getElementById('QuoteGroup_' + curQuote).style.color = '#FFFFFF';
		}

		// now we know we are on the right cycle
		
		if( curTime > cycleStartTime + kCycleTime - kFadeTime )
		{
			// we should be fading out our old quote
			newColor = ( Math.round(255 * (curTime - (cycleStartTime + kCycleTime - kFadeTime)) / kFadeTime ) );
			newColor = Math.max( 50, newColor).toString(16);
			if( newColor.length == 1 ) newColor = '0' + newColor;
			newColor = '#' + newColor + newColor + newColor;
			document.getElementById('QuoteGroup_' + curQuote).style.color = newColor;
		}
		else if( curTime > cycleStartTime + kFadeTime )
		{
			// this quote should be 100% there
			if( document.getElementById('QuoteGroup_' + curQuote).style.color != '' )
				document.getElementById('QuoteGroup_' + curQuote).style.color = '';

			if( document.getElementById('MoreTestimonials' ) != null )
				if( document.getElementById('MoreTestimonials' ).style.display != '' )
					document.getElementById('MoreTestimonials' ).style.display = '';
	
			var winAndMacLogo = document.getElementById( "WinAndMac" );
			if( winAndMacLogo != null )
				winAndMacLogo.style.display = '';
			
			var tryNow = document.getElementById( "TryNow" );
			if( tryNow != null )
				tryNow.style.display = '';

			if( ! firstCycleFinished )
			{
				firstCycleFinished = true;
			}
		}
		else
		{
			// we should be fading IN this quote
			newColor = (255 - Math.round(255 * (curTime - cycleStartTime) / kFadeTime ) );
			newColor =  Math.max( 50, newColor).toString(16);
			if( newColor.length == 1 ) newColor = '0' + newColor;
			newColor = '#' + newColor + newColor + newColor;
			document.getElementById('QuoteGroup_' + curQuote).style.color = newColor;

			if( curTime < cycleInitialCycleTime + kFadeTime )
			{
				// we are fading in the initial cycle, so fade in our "more testimonials" link too!

				newColor = (255 - Math.round(255 * (curTime - cycleStartTime) / kFadeTime ) );
				mt_r =  Math.max( 0x00, newColor).toString(16);
				mt_g =  Math.max( 0x66, newColor).toString(16);
				mt_b =  Math.max( 0x99, newColor).toString(16);

				if( mt_r.length == 1 ) mt_r = '0' + mt_r;
				if( mt_g.length == 1 ) mt_g = '0' + mt_g;
				if( mt_b.length == 1 ) mt_b = '0' + mt_b;

				newColor = '#' + mt_r + mt_g + mt_b;
				if( document.getElementById('MoreTestimonials' ) != null )
					document.getElementById('MoreTestimonials' ).style.color = newColor;
			}
		}
	}
	else
	{
		// this is the first cycle, initialize everything

		// figure out how many quote grouns we have
		quoteGroupCount = 0;
		do{
			quoteGroupCount++;
		} while( document.getElementById('QuoteGroup_' + quoteGroupCount ) != null );
	
		curQuote = getRandomNum(0,quoteGroupCount - 1);
		document.getElementById('QuoteGroup_' + curQuote).style.display = '';
		document.getElementById('QuoteGroup_' + curQuote).style.color = '#FFFFFF';

		firstCycleFinished = false;

		if( dontAnimate ) cycleStartTime = cycleInitialCycleTime = curTime - kFadeTime;
		else cycleStartTime = cycleInitialCycleTime = curTime - kInitialCycleOffset;
	}
		
	setTimeout("changeQuotes()", 5 );
}