var gallery = {
	init : function() {
		$(".album").each(function(){
			var speed = "slow";
			var timeout = 5000;
			var obj = $(this);
			var elements = $("a",obj);
			var picture = $(".picture",obj);
			elements.click(function(){
				var href = $(this).attr("href");
				picture.attr("src",href).hide().fadeIn(speed);
				return false;
			}).mouseover(function(){
				$('img',$(this)).css({opacity:".5"}).stop().animate({opacity:"1"},500);
			});
			setTimeout(function() {
				next(picture,elements,speed,timeout,1);
			},timeout);
		})
	}
};

function next(picture,elements,speed,timeout,current){
	var href = $(elements[current]).attr("href");
	picture.attr("src",href).hide().fadeIn(speed);
	if ((current+1) < elements.length) {
		current++;
	} else {
		current = 0;
	};
	setTimeout(function() {
		next(picture, elements,speed, timeout, current);
	},timeout);
}

$(document).ready( function() {
	gallery.init();
});