﻿// var slideShow = { 'clock' : null, 'count' : 1 }
var slideShow = {'clock' : null};

slideShow.images = [
	'images/photo1.jpg',
	'images/photo2.jpg',
	'images/photo3.jpg',
	'images/photo4.jpg',
	'images/photo5.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg',
	'images/photo6.jpg'
    ];

//cache the images
slideShow.imagesLen = slideShow.images.length;
slideShow.cache = [];
for(var i=0; i<slideShow.imagesLen; ++i)
{
	slideShow.cache[i] = new Image;
	slideShow.cache[i].src = slideShow.images[i];
}

// Usage: crossfade(image object,
//	div object,
//	new image src,
//	alt text,
//	length (ms))
function crossfade()
{
	// Do nothing if already running
	if (slideShow.clock != null)
		return;

	slideShow.imgObj = arguments[0];
	slideShow.divObj = arguments[1];
	slideShow.src = arguments[2];
	slideShow.imgObj.alt = arguments[3];
	slideShow.length = parseInt(arguments[4], 10);

	determineSupportedOpacity();

	if (slideShow.type == 'none')
	{
		// Just swap images
		slideShow.imgObj.src = slideShow.src;
		return;
	}

	// set image to transparent to prepare for fade in
	setOpacity(0);
	
	slideShow.imgObj.src = slideShow.src;

	//start the timer - max 100 frames
	slideShow.clock = setInterval('crossfadeStep()', Math.max(slideShow.length / 1000, 1));
	slideShow.startTime = (new Date()).getTime();
}

function crossfadeStep()
{
	var progressPct = ((new Date()).getTime() - slideShow.startTime) / slideShow.length;

	if (progressPct >= 1)
	{
		//clear the timer
		clearInterval(slideShow.clock);
		slideShow.clock = null;

		//set the original image to the src of the new image
		slideShow.divObj.style.backgroundImage = "url("+ slideShow.src +")";
	}

	setOpacity(progressPct);
}

// opacityPct is 0-1.0
function setOpacity(opacityPct)
{
	switch(slideShow.type)
	{
		case 'ie' :
			slideShow.imgObj.filters.alpha.opacity = opacityPct * 100;
			break;
			
		case 'khtml' :
			slideShow.imgObj.style.KhtmlOpacity = 1 - (opacityPct);
			break;

		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			slideShow.imgObj.style.MozOpacity = Math.min(opacityPct, 0.9999999);
			break;

		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			slideShow.imgObj.style.opacity = Math.min(opacityPct, 0.9999999);
	}
}

function determineSupportedOpacity()
{
	//store the supported form of opacity
	if(typeof slideShow.imgObj.style.opacity != 'undefined')
	{
		slideShow.type = 'w3c';
	}
	else if(typeof slideShow.imgObj.style.MozOpacity != 'undefined')
	{
		slideShow.type = 'moz';
	}
	else if(typeof slideShow.imgObj.style.KhtmlOpacity != 'undefined')
	{
		slideShow.type = 'khtml';
	}
	else if(typeof slideShow.imgObj.filters == 'object')
	{
		//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
		//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
		//then the returned value type, which should be a number, but in mac/ie5 is an empty string
		slideShow.type = (slideShow.imgObj.filters.length > 0 && typeof slideShow.imgObj.filters.alpha == 'object' && typeof slideShow.imgObj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
	}
	else
	{
		slideShow.type = 'none';
	}
}