var RotatingThumbs = {
	interval	: 500,
	timer		: null,
	img		: null,
	origSRC		: '',
	thumbs		: [],
	current		: 0,
	
	debug : false,
	
	rotate : function(img,thumbIDs)
	{	
		if ( this.timer )
			window.clearTimeout(this.timer);
			
		img.onmouseout = function() {
			RotatingThumbs.img.onload = function(){};
			RotatingThumbs.img.onmouseout = function(){};
			window.clearTimeout(RotatingThumbs.timer);
			RotatingThumbs.img.src = RotatingThumbs.origSRC;

			for ( var i=0; i<RotatingThumbs.thumbs.length; i++ )
				delete RotatingThumbs.thumbs[i];
			
			RotatingThumbs.debugMsg('MouseOut');
		};
		img.onload = function() {
			RotatingThumbs.debugMsg('Loaded '+img.src);
			
			window.clearTimeout(RotatingThumbs.timer);			
			RotatingThumbs.timer = window.setTimeout(RotatingThumbs.pump, RotatingThumbs.interval);
		};

		this.img = img;
		this.origSRC = img.src;
	
	
		thumbIDs = thumbIDs.split(',');
		this.thumbs = [];
		var masterImg = new Image();
		masterImg.src = this.origSRC;
		this.thumbs.push(masterImg);
		
		for ( var i=0; i<thumbIDs.length; i++ )
			if ( thumbIDs[i] )
			{
				var preload = new Image();
				preload.src = thumbIDs[i];
				this.thumbs.push(preload);
			}
		//this.debugMsg('Thumbs: ' + this.thumbs.join(','));
		
		if ( this.thumbs.length == 1 )
			return; // nothing to rotate
		
		this.current = 0;
		this.pump();
	},
	
	pump : function()
	{
		if ( ++RotatingThumbs.current >= RotatingThumbs.thumbs.length )
			RotatingThumbs.current = 0;
		
		RotatingThumbs.debugMsg('Pumping thumb #' + RotatingThumbs.current);
		
		RotatingThumbs.img.src = RotatingThumbs.thumbs[RotatingThumbs.current].src;
	},
	
	debugMsg : function(msg)
	{
		if ( !this.debug ) return;
		
		if ( !this.debugDiv )
		{
			this.debugDiv = document.createElement('div');
			this.debugDiv.style.position = 'absolute';
			this.debugDiv.style.left = 0 + 'px';
			this.debugDiv.style.top = 0 + 'px';
			this.debugDiv.style.backgroundColor = '#880000';
			this.debugDiv.style.color = '#FFF';
			document.body.appendChild(this.debugDiv);
		}
		
		this.debugDiv.innerHTML += '<br />' + msg;
	}
}
