/** Slideshow for BJD Electrical
 ** By Adrian Cooney
 ** Monday 9th of May, 2011
 **/
var parent = $(".photoslideshow"),
left = parent.find(".left"),
right = parent.find(".right");

function changeIndex() {
	var that = $(this), zIndex = (that.index() == 0) ? (function() { that.addClass("slideshow-img-active"); return 1;})() : 0; 
	that.css("z-index", zIndex);
}

left.find("img").each(function() {
	changeIndex.call(this);
});

right.find("img").each(function() {
	changeIndex.call(this);
});

function loop() {
	$(".left .slideshow-img-active").animate({ opacity: 0}, 0, function() {
		var img = ($(this).index() == left.find("img").length-1) ? left.find("img:first") : $(this).next();
		
		$(this).css({"z-index": 0, "opacity": 1}).removeClass("slideshow-img-active");
		img.addClass("slideshow-img-active").css("z-index", 1);
	});
	
	$(".right .slideshow-img-active").animate({ opacity: 0}, 0, function() {
		var img = ($(this).index() == right.find("img").length-1) ? right.find("img:first") : $(this).next();
		
		$(this).css({"z-index": 0, "opacity": 1}).removeClass("slideshow-img-active");
		img.addClass("slideshow-img-active").css("z-index", 1);
	});
	
	setTimeout(loop, 5000);
}

setTimeout(loop, 5000);
