var Flickrclone = Class.create();

Flickrclone.prototype = {

	initialize: function(whatcontainer) {
		//sanity check
		if (!document.getElementsByTagName){ return; }	
		//load up the images
		this.container = $(whatcontainer);
		this.images = new Array;
		this.imageIndex = 0;
		
		var showfrontimage = document.createElement("img");
		showfrontimage.setAttribute("id","showfrontimage");
		showfrontimage.setAttribute("width","300");
		
		
		//append the front image
		
		var anchors = this.container.getElementsByTagName("a");
		
		for(var j=0; j<anchors.length;j++){
			var anchor = anchors[j];
				
			var source =  String(anchor.getAttribute('href'));
			var animage = new Image();
			if(j == (anchors.length - 1))
			{
				//are we on the last image?
				animage.onload = this.start;
			}
			animage.src = source;	
			this.images.push(animage);
			
		}
		
    	//now clean out the div
		while(this.container.childNodes.length > 0) {
			this.container.removeChild(this.container.childNodes[0]);
		}
		
		//and append the image holder
		this.container.appendChild(showfrontimage);
		
		//grumble freakin IE.. grumble
		//set up the background image
		if(this.imageIndex.length > 1)
		{
			this.container.style.backgroundImage = this.images[this.imageIndex+1].src;
		}
		else
		{
			this.container.style.backgroundImage = this.images[this.imageIndex].src;
		}
		
		//this.start();
	},
	
	start: function() {
		$('showfrontimage').src = myClone.images[myClone.imageIndex].src;
		new Effect.Opacity('showfrontimage',
		    { duration: 10.0, 
      			transition: Effect.Transitions.linear, 
			      from: 1.0, to: 0.0, afterFinish: myClone.swapImage 
			});
			      
	
			
	},
	
	swapImage: function() {
	
		//move the background image to the foreground
		$('showfrontimage').src = myClone.images[myClone.imageIndex].src;
		//show the foreground image
		Element.setOpacity('showfrontimage',1.0);
		
		//now replace the background image with the next image
		myClone.imageIndex++;
		if(myClone.imageIndex >= myClone.images.length)
		{
			myClone.imageIndex = 0;
		}
		myClone.container.style.backgroundImage = "url(" + myClone.images[myClone.imageIndex].src + ")";
		
		//and run the fade
		myClone.start();
		
		
		
		
	
	}
	
	
	
		
}

var myClone;
