// JavaScript Document
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");

google.setOnLoadCallback(function() {
$(document).ready(function() {
        init();
    });
});

var imageCount;

function init(){
	
	imageCount = $("#imageRotator div").length;
	indexCount = $("#imageRotator div").length;
	
	$("#imageRotator div img").css("width","auto");
	
	if(imageCount > 1)
	{
		//Give all div's inside container a z-index value
		$("#imageRotator div").each(function()
		{
			$(this).css("position", "absolute");
			$(this).css("z-index", indexCount);
			indexCount--;
		});
	
		
		//Set speed for gallery to rotate
		//Set timer to run, call function & speed
		var speed = 6000;
		var run = setInterval('next()', speed);
	}

}
function next(){	
		//Loop through each div inside conatiner
		$("#imageRotator div").each(function()
		{
			//Check for current visible div
			if($(this).css("z-index") == imageCount)
			{
				//Fade div out
				$(this).animate({ opacity: 0 }, 1500, function(){
					//Send div to back
					$(this).css("z-index", 1);
				});
			}
			
			//Move all divs forward 1
			$(this).css("z-index", parseInt($(this).css("z-index")) + 1 );
			//Set opacity of faded div to show again
			$(this).animate({ opacity: 1 }, 0 );
		});
}
