// JavaScript Document

// images fading galery

var sequences = new Array();
	
jQuery(document).ready(function() {
	jQuery("div.sequence").each(
		function(i) {
			sequences.push(new Sequence(jQuery(this)));
			setInterval("sequences[" + i + "].next()", 2000 );
		}
	);
});

function Sequence (sequenceDiv) {
	this.currentImage;
	this.lastImage;
	this.iteration = 1;
	var _self = this;
	
	this.sequenceImages = jQuery.makeArray(sequenceDiv.children());
	
	this.currentImage = this.sequenceImages.length - 1;
	for(i=0; i<this.sequenceImages.length; i++) {
		jQuery(this.sequenceImages[i]).css("position", "absolute");
	}	
	for(i=0; i<this.sequenceImages.length - 1; i++) {
		jQuery(this.sequenceImages[i]).css("display", "none");
	}		

	this.next = function() {
		this.iteration++;

		this.lastImage = this.currentImage;
		this.currentImage++;
		if(this.currentImage == this.sequenceImages.length) {
			this.currentImage = 0;
		}
		
		jQuery(this.sequenceImages[this.currentImage]).fadeIn("slow", this.nextCB);
		jQuery(this.sequenceImages[this.currentImage]).css("z-index", this.iteration);
	};
	
	
	this.nextCB = function() {
		jQuery(_self.sequenceImages[_self.lastImage]).css("display", "none");
	}
}
