var slides = [];
var currentFrame = 0;
var slideTime = 4000;

$(document).ready(function(){
	$("#slider img.slide").wrapAll('<div id="bgSlider" />')
   
   $("#bgSlider img").each(function(index) {
		slides.push($(this));
		$(this).remove();
	});
	
	if (slides.length > 1) {
		$("#bgSlider").append(slides[0]);
		setTimeout(slideBtnClick, slideTime);
	}
});

function slideBtnClick() {
	currentFrame++;
	if (currentFrame >= slides.length) { currentFrame = 0; }
	FadeSlide(currentFrame);
}

function FadeSlide(frame) {
	$("#bgSlider img:first").css("margin-left", "-" + $("#slider").width() + "px");
	$("#bgSlider").prepend(slides[frame]);
	$("#bgSlider img:first").animate({opacity: 1.0, "margin-left": "0px"}, 0);
	$("#bgSlider img:last").animate({
		 opacity: 0.0
		}, 500, 'swing', function() { 
			$("#bgSlider img:last").remove();
			setTimeout(slideBtnClick, slideTime);
	});
}
