/*global $ window */
function rotateImages(div_id, images, staticTime, fadeTime) {
	if ($("#"+div_id).length===0) {
		return false;
	}
	
	var div_contents = $("#"+div_id+">*");
	$("#"+div_id).empty();
	$("#"+div_id).append($('<div id="'+div_id+'_imageRotationDiv1"></div>').append(div_contents.clone()));
	$("#"+div_id).append($('<div id="'+div_id+'_imageRotationDiv2"></div>').append(div_contents.clone()));
	

	$("#"+div_id+" div").css({
		position:"absolute",
		top:"0px",
		left:"0px"
	});
	$("#"+div_id+"_imageRotationDiv2").hide();
	
	$("#"+div_id+" img").css("display", "block");
	
	var path_elements = /^(.*)\/([^\/]+)$/.exec($("#"+div_id+"_imageRotationDiv1 img").attr("src"));
	var image_path = path_elements[1];
	var image_index = 0;
	$("#"+div_id+"_imageRotationDiv2 img").attr("src", image_path+"/"+images[1]);

	var swap_function = function(){
		image_index = (image_index+1)%images.length;
		var new_image_index = (image_index+1)%images.length;
		$("#"+div_id+"_imageRotationDiv2").fadeIn(fadeTime, function(){
			$("#"+div_id+"_imageRotationDiv1 img").attr("src", image_path+"/"+images[image_index]);
			$("#"+div_id+"_imageRotationDiv2").hide();
			//Allow time to hide before changing the src to avoid the image flashing
			window.setTimeout(function(){
				$("#"+div_id+"_imageRotationDiv2 img").attr("src", image_path+"/"+images[new_image_index]);
			}, 10);
			window.setTimeout(swap_function, staticTime);
		});
		
	};
	window.setTimeout(swap_function, staticTime);
	return true;
}