
/** Progressive enhancements */

(function() {
	
	var lib = {
		imageRotator : function() {
			$(".image-rotator").each(function(i, base) {
				var current, previous;
				base = $(base);
				base.addClass("enhanced");
				current = base.children("img").first();
				current.addClass("current");
				var timer = setInterval(function() {
					var next = current.next();
					if (!next || !next.length) {
						next = current.parent().children("img").first();
					}
					if (previous) {
						previous.removeClass('previous');
					}
					next.hide().addClass('current');
					next.fadeIn('slow');
					current.removeClass('current').addClass('previous');
					previous = current;
					current = next;
				}, 3000);
				lib.intervals.push(timer);
			});
		},
		destroy : function() {
			if (!lib) {
				return false;
			}
			for (var x=lib.unloadRegistry.length; x--; ) {
				lib.unloadRegistry[x]();
			}
			lib.unloadRegistry = null;
			for (x=lib.timeouts.length; x--; ) {
				clearTimeout(lib.timeouts[x]);
			}
			for (x=lib.intervals.length; x--; ) {
				clearInterval(lib.intervals[x]);
			}
			$(window).unbind('unload', lib.destroy)
					.unbind('beforeunload', lib.destroy);
			lib = null;
		},
		unloadRegistry : [],
		timeouts	: [],
		intervals	: []
	};
	
	
	lib.imageRotator();
	
	
	// Cleanup
	$(window).bind('unload', lib.destroy)
			.bind('beforeunload', lib.destroy);
	
}());

