

function Gallery(images, node){
	this.images = images;
	// $(this.images).hide();
	this.imgNode = node;
	this.position = 0;
	this.delay = 4000; // milliseconds
}

Gallery.prototype.begin = function(){
	if(this.images.length > 1){
		this.wait();
	}
}

Gallery.prototype.wait = function(){
	// console.log('wait');
	// we need this line so we can actually call nextImage on 'this' object
	var currentObject = this;
	setTimeout(function(){ currentObject.nextImage(); }, this.delay);
}

Gallery.prototype.nextImage = function(){
	// console.log('nextImage');
	// we need this line so we can actually call nextImage on 'this' object
	var currentObject = this;
	// get the position of the next image
	this.position = (this.position + 1) % this.images.length;

	$(this.imgNode).fadeOut('slow', function(){
		$(currentObject.imgNode).attr('visibility', 'hidden');
		$(currentObject.imgNode).attr('src', currentObject.images[currentObject.position].src);
		$(currentObject.imgNode).attr('visibility', 'visible');
		
		var fadeInAndWait = function(){
			$(currentObject.imgNode).fadeIn('slow', function(){ currentObject.wait() });
		}
		
		setTimeout(fadeInAndWait, 100);
		
		
	});
}


// jQuery.fn.replace = function() {
//     var stack = [];
//     return this.domManip(arguments, true, 1, function(a){
//         this.parentNode.replaceChild( a, this );
//         stack.push(a);
//     }).pushStack( stack );
// };
