
var current = 0;
var fadeCover = null;
var contentPhoto = null;
var photoDescrip = null;
var contentDescrip = null;

function rotateHeader(pe)
{
	current++;
	if(current >= headerImages.length)
	{
		current = 0;
	}

	var imageEffect = new Effect.Opacity(
		fadeCover,
		{
			from: 0,
			to: 1,
			beforeStart: function()
			{
				fadeCover.setStyle(
					{
						opacity: 0,
						display: "block"
					}
				);
			},
			afterSetup: function()
			{
				fadeCover.setStyle(
					{
						background: 'url(' + headerImages[current].src + ') no-repeat'
					}
				);
			},
			afterFinish: function()
			{
				contentPhoto.src = headerImages[current].src;
				fadeCover.hide();
			},
			sync: true
		}
	);

	var captionEffect = new Effect.Opacity(
		contentDescrip,
		{
			from: 0,
			to: 1,
			sync: true,
			afterSetup: function()
			{
				contentDescrip.update(headerImages[current].title);
			},
			afterFinish: function()
			{
				contentDescrip.up().down("p.content-photo-description").update(headerImages[current].title);
			}
		}
	);

	new Effect.Parallel(
		[
			imageEffect,
			captionEffect
		],
		{
			duration: 3
		}
	);
}

function preloadImages()
{
	for(var i=0; i<headerImages.length; i++)
	{
		window['image'+i] = new Image();
		window['image'+i].src = headerImages[i].src;
	}
}

function initFader() {
	fadeCover = $('fade-cover');
	contentPhoto = $('content-photo-img-image');
	contentDescrip = $('content-photo-description');
	
	var bgColor = contentDescrip.getStyle('backgroundColor');
	
	contentDescrip.setStyle(
		{
			position: "relative",
			zIndex: 1
		}
	);
	
	photoDescrip = new Element('p', { className: 'content-photo-description' });
	photoDescrip.setStyle({ display: 'block', backgroundColor: bgColor }).update(contentDescrip.innerHTML);
	
	$('content-photo').appendChild(photoDescrip);
	
	preloadImages();
	
	new PeriodicalExecuter(rotateHeader, rotationSpeed);
}

Event.observe(window, 'load', initFader);

