$(function() {
	$("section.gallery-bg").each(function() {
		var self = this;
		var items = $.makeArray($("article.image-gallery", this));
		var current = null;
		var T;
		
		(function() {
			var next = items.shift();
			if (!current) {
				$(next).addClass('current');
			} else {
				$(current).animateClass("current", "");
				$(next).animateClass("", "current");
			}
			items.push(next);
			current = next;
			
			if ($(self).attr("data-container")) {
				var id = $(self).attr('data-container');
				$("#" + id).empty()
					.attr("class", "gallery-bg-title")
					.html("<a class='back' href='#' onclick='return false;'>&lt;</a> <a class='next' href='#' onclick='return false;'>&gt;</a> <span class='title'></span> <a href='#' class='open'>Learn more</a>");
				$("#" + id + " .title").text($(current).attr('data-title'));
				$("#" + id + " .open").click(function() {
					var g = $(current).attr('data-record-id');
					var f = window['gallery_' + g];
					f(0);
					return false;
				});
				if (items.length == 1) {
					$("#" + id + " .back, #" + id + " .next").remove();
				} else {
					var C = arguments.callee;
					$("#" + id + " .next").click(function() {
						clearTimeout(T);
						C();
					});
					$("#" + id + " .back").click(function() {
						clearTimeout(T);
						var P = items.pop();
						items.unshift(P);
						P = items.pop();
						items.unshift(P);
						C();
					});				
				}
			}
			if (items.length > 1)
				T = setTimeout(arguments.callee, 5000);
		})();
	});
});

		

