var FOTO = {

}

//rotates 1 main image like the slideshow, but more simple
FOTO.Rotate = {
	imageId: 0, //the current image being displayed
	duration: 5000,
	fadeDuration: 1000,
	imageIds: Array(0), //keyed on slot, value is imageId
	slot: 0, //what slot our we on?
	play: 1,
	interval: 0,
	imgWidth: 900,
	imgHeight: 600,
	imgCrop: 1,
	loadingSrc: null, //the new image source being displayed to work around bug in chrome/ff
	lock: false,
	toggle: false,
	crossfade: false,

	getUrl: function(id){
		return '/fetch.php?imageId='+id+'&width='+FOTO.Rotate.imgWidth+'&height='+FOTO.Rotate.imgHeight+'&crop='+FOTO.Rotate.imgCrop;
	},

	transitionMainImage: function(slot){
		//the new image is already loaded, so fadeOut this image, load in the new src and fade back in
		var newImageId = FOTO.Rotate.imageIds[slot];
		var url = FOTO.Rotate.getUrl(newImageId);

		var imgA = $('#rotate_main_image');
		var imgB = $('#rotate_sec_image');
		if(imgB.length==0){
			imgB = $(document.createElement('img')).attr('id','rotate_sec_image').hide();
			imgA.after(imgB);
		}
		
		if(FOTO.Rotate.toggle === false){
			var show = imgB;
			var hide = imgA;
		} else {
			var show = imgA;
			var hide = imgB;
		}
		FOTO.Rotate.toggle = !FOTO.Rotate.toggle;
		
		show.attr('src',url);
		if(FOTO.Rotate.crossfade){
			hide.fadeOut(FOTO.Rotate.fadeDuration);
			show.fadeIn(FOTO.Rotate.fadeDuration);
		} else {
			hide.fadeOut(FOTO.Rotate.fadeDuration,function(){
				show.fadeIn(FOTO.Rotate.fadeDuration);
			});
		}
		
	},
	
	completeBug: function(){
		FOTO.Rotate.transitionMainImage(FOTO.Rotate.slot);
	},

	cycleImage: function(){
		FOTO.Rotate.slot++;
		if(FOTO.Rotate.slot >= FOTO.Rotate.imageIds.length){
			FOTO.Rotate.slot = 0;
		}

		var imageId = FOTO.Rotate.imageIds[FOTO.Rotate.slot];
		if(imageId){
			var url = FOTO.Rotate.getUrl(imageId);
			//var image = new Image(); //may not work with safari 2
			var image = document.createElement('img');
			image.onload = FOTO.Rotate.completeBug;
			image.src = url;
			
			/*if(image.complete){
				FOTO.Rotate.displaySrc = url;
				FOTO.Rotate.transitionMainImage(FOTO.Rotate.slot);
			}*/
		}
	},

	play: function(){
		if(FOTO.Rotate.play){
			FOTO.Rotate.interval = setInterval(FOTO.Rotate.cycleImage,FOTO.Rotate.duration);
		}
	},

	stop: function(){
		if(FOTO.Rotate.interval){
			clearInterval(FOTO.Rotate.interval);
		}
	}



}
