

Gallery = function(properties) {
	
	this.gallery = null;
	this.interval = 0;
	
	if( typeof properties != "undefined" ) {
		for( var property in this) {
			if(typeof properties[property] != "undefined") {
					this[property] = properties[property];
			}
		}
	}

	this.images = [];	
	this.initialize();
}

Gallery.prototype = {
      
	initialize: function() {
		this.refreshImages();

		var x = 100;
		this.images.each(function(){
			$(this).css('z-index',x);
			x--;
		})
		
		this.gallery.show();
	},

	start: function(startDelay) {
		var thisGallery = this;
		
		setTimeout(function(){
			thisGallery.nextImage();
		},startDelay);		

	},

	nextImage: function() {
		var thisGallery = this;

		var firstImage = this.images.first();
		var lastImage = this.images.last();
		
		var zlast = lastImage.css('z-index');

		firstImage.fadeOut(1000, function(){
			firstImage.css('z-index',zlast - 1);

			firstImage.insertAfter(lastImage);



			firstImage.show();
			
			thisGallery.refreshImages();

		});
		
		
		
		setTimeout(function(){
			thisGallery.nextImage();
		}, this.interval );

	},
	refreshImages:function(){
		this.images = this.gallery.find('.image');
	}
	
}








GalleryManager = function(properties) {
	
	this.galleries = [];
	this.startDelay = 0;
	this.interval = 0;
	
	if( typeof properties != "undefined" ) {
		for( var property in this) {
			if(typeof properties[property] != "undefined") {
					this[property] = properties[property];
			}
		}
	}
	
	this.galleryArray = [];
	
	this.initialize();
}


GalleryManager.prototype = {
	initialize: function() {
		var x = 0;
		
		var thisGalleryManager = this;
		
		this.galleries.each(function(){
			var properties = {
				gallery:$(this),
				interval:thisGalleryManager.interval
			}
			thisGalleryManager.galleryArray[x] = new Gallery(properties);
			x++;
		})
		
		this.startGalleries();
	},
	
	startGalleries: function(){
		var thisGalleryManager = this;
				
		for (var i=0; i<this.galleryArray.length; i++) {
			var startDelay = Math.floor(Math.random() * this.interval)

			thisGalleryManager.galleryArray[i].start(startDelay);
		}
	}
}





