var fade_to_white_duration = 125;
var pause_between_white_duration = 125;
var fade_from_white_duration = 750;
var frame_hold_duration = 500;
var crossfade_duration = 750;
	
var timeout;
var loop_count = 0;

function videoFadeThroughWhite(cursor) {
	jQuery('#singleGallery').find('.flash:first').css('opacity', '0');
	jQuery('#singleGallery').find('.flash:first').css('background-image', '');
	jQuery('#singleGallery').find('.flash:first').animate({opacity:1.0}, fade_to_white_duration, 'linear',  
		function () {
			jQuery('#singleGallery').css('background-image', 'url('+ gallery[cursor] +')');
			jQuery('#singleGallery').animate({opacity:1}, pause_between_white_duration, function () { //opacity 1 when already one doubles as a pause
				jQuery('#singleGallery').find('.flash:first').animate({opacity:0}, fade_from_white_duration, 'linear', 
					function () {
						jQuery('#singleGallery').find('.flash:first').css('opacity', '0');
						jQuery('#singleGallery').find('.flash:first').css('background-image', '');
						timeout = setTimeout("videoSequence(" + (cursor + 1) + ")", frame_hold_duration);
				});
			});
		});
}

function videoCrossfade(cursor) {
	jQuery('#singleGallery').find('.flash:first').css('opacity', '0');
	jQuery('#singleGallery').find('.flash:first').css('background-image', 'url('+ gallery[cursor] +')');
	jQuery('#singleGallery').find('.flash:first').animate({opacity:1.0}, crossfade_duration, 'linear',  
		function () {
			jQuery('#singleGallery').css('background-image', 'url('+ gallery[cursor] +')');
			
			jQuery('#singleGallery').find('.flash:first').css('opacity', '0');
			jQuery('#singleGallery').find('.flash:first').css('background-image', '');
			timeout = setTimeout("videoSequence(" + (cursor + 1) + ")", frame_hold_duration);
		}
	);
}

function videoSequence(cursor) {
	// clear the previous timeout
	clearTimeout(timeout);
	
	// loop (if necessary)
	if (!isSet(gallery[cursor])) {
		cursor = 0;
	}
	
	// configure the gallery
	if (loop_count == 0) {
		jQuery('#singleGallery').css('width', '720px');
		jQuery('#singleGallery').css('height', '540px');
		jQuery('#singleGallery').css('background-image', 'url('+ gallery[cursor] +')');
		
		// cache all images
		jQuery.cacheImage(gallery);
		
		timeout = setTimeout("videoSequence(" + (cursor + 1) + ")", frame_hold_duration / 2);
	} else {
		// us the transition approriate to the frme
		switch (cursor) {
		case 0: // red char
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 1: // red chair, text
			frame_hold_duration = 1000;
			videoCrossfade(cursor);
			break;
		case 2:
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 3:
			frame_hold_duration = 1000;
			videoCrossfade(cursor);
			break;
		case 4:
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 5:
			frame_hold_duration = 1000;
			videoCrossfade(cursor);
			break;
		case 6:
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 7:
			frame_hold_duration = 1000;
			videoCrossfade(cursor);
			break;
		case 8:
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 9:
			frame_hold_duration = 1000;
			videoCrossfade(cursor);
			break;
		case 10:
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 11:
			frame_hold_duration = 1000;
			videoCrossfade(cursor);
			break;
		case 12:
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 13:
			frame_hold_duration = 1000;
			videoCrossfade(cursor);
			break;
		case 14: // Sklar
			frame_hold_duration = 400;
			videoFadeThroughWhite(cursor);
			break;
		case 15:
		case 16:
		case 17:
			frame_hold_duration = 200;
			videoCrossfade(cursor);
			break;
		case 18: // address
			frame_hold_duration = 10000;
			videoCrossfade(cursor);
			break;
		default:
			videoCrossfade(cursor);
			break;
		}
	}
	
	loop_count ++;
}
