var HomeGallery = {
	imageArr: null,
	current_img: 0,
	total_imgs: null,
	interval: null,
	
	init: function() {
		this.imageArr = $("div#page div#header .image img");
		this.total_imgs = this.imageArr.length - 1;
		this.setIndexes();
		var _this = this;
		if( this.total_imgs >= 1 )
		this.interval = setInterval(function() { _this.showNext(); }, 4000);
	},
	
	setIndexes: function() {
		for(var i=0; i < this.imageArr.length; i++) {
			$(this.imageArr[i]).css("z-index", this.imageArr.length - i).css('opacity', 0).filter(":first").css('opacity', 1.0);
		}
	},
	
	showNext: function() {
		if(this.current_img < this.total_imgs) {
			// show next image behind
			$(this.imageArr[this.current_img + 1]).css('opacity', 1.0).css('display', 'block');
			// fade out current image
			$(this.imageArr[this.current_img]).fadeOut(1000);
			// increment counter
			this.current_img++;
		} else {
			// fade in first image
			var _this = this;
			$(this.imageArr[0]).fadeIn(1000, function() {
				// hide last image
				$(_this.imageArr[_this.current_img]).css('opacity', 0);
				// reset counter
				_this.current_img = 0;
			});
		}
	}
}

// DOM ready Events
$(function () {
	$("div#page div#header .image img:first-child").show();
	HomeGallery.init();
});
