"use strict"
var images; //april 5 2011: added because strict is now implemented in FF4!

var CrossFade = {
	defaults : {
		wrapper : '#background',
		fade : 2000,
		delay : 4000
	},
	settings : {},
	images : {},
	
	// Setups up the images and starts the cross fadding
	init : function(options) {
		CrossFade.settings = $.extend({}, CrossFade.defaults, options)
		
		images = $(CrossFade.settings.wrapper + ' img'); // Grabs all the images in the div
		
		if (images.length > 1) CrossFade.fade(images.last()); // Starts the fade with the last image since it is visble by default
		else images.show();		
	},
	
	// Infinite loop of cross fades
	fade : function(image) {
		image.show();
		if (image.prev().length) { // if there is another image behind this one, fade out
			image.prev().show();
			image.delay(CrossFade.settings.delay).fadeOut(CrossFade.settings.fade, function() {
				CrossFade.fade(image.prev()) // Fade Out again with the next image
				});
		} else { // if it is the last image, fade in the first image and start again.
			images.last().delay(CrossFade.settings.delay).fadeIn(CrossFade.settings.fade, function(){
				CrossFade.fade(images.last()); // Repeat forever.
			})
		}
	}
}
