$(document).ready(function(){
	Gallery.init();
});


var Gallery = {
	backgrounds: [
		'/i/photos/gallery-bg-4.jpg',
		'/i/photos/gallery-bg-5.jpg',
		'/i/photos/gallery-bg-3.jpg'
	],
	init: function() {
		if ($('#gallery')) {
			this.faders = [];
			this.container = $('#gallery');
			this.showOnHover = $(".showOnHover", this.container);
			$(this.container).wrapInner('<div class="galleryContent"></div>')
			for (var i=0; i < this.backgrounds.length; i++) {
				Gallery.addFader(this.backgrounds[i]);
			};
			this.startSlideShow();
			$(this.container).mouseover(function(event){Gallery.mouseOver(event)});
			$(this.container).mouseout(function(event){Gallery.mouseOut(event)});
//			$(document).mousemove(function(event){Gallery.mouseMove(event)})
		}
	},
	hideHoverContent: function() {
		for (var i=0; i < this.showOnHover.length; i++) {
			$(this.showOnHover[i]).stop();
			$(this.showOnHover[i]).animate({opacity: 0}, 500);
		};
	},
	showHoverContent: function() {
		for (var i=0; i < this.showOnHover.length; i++) {
			$(this.showOnHover[i]).stop();
			$(this.showOnHover[i]).animate({opacity: 1}, 500);
		};
	},
	mouseOver: function(event) {
		Gallery.stopSlideShow();
		event.stopPropagation();
	},
	mouseMove: function(event) {
		this._isMouseOver = this.isMouseOver(event);
	},
	mouseOut: function(event) {
		if (!this.isMouseOver(event)) {
			Gallery.startSlideShow();
		}
		event.stopPropagation();
	},
	addFader: function(backgroundFile) {
		var newFader = $(document.createElement('div')).addClass('fader');
		this.container.prepend(newFader);
		$(newFader).css({'background': 'url(\''+backgroundFile+'\')', 'opacity': 0, 'background-repeat': 'no-repeat', 'height': this.container.height()+'px'});
		this.faders.push(newFader);
	},
	startSlideShow: function() {
		Gallery.hideHoverContent();
		Gallery.nextSlide();
		clearInterval(this.nextSlideInterval);
		this.nextSlideInterval = setInterval('Gallery.nextSlide()', 5000);
	},
	stopSlideShow: function() {
		Gallery.showHoverContent();
		clearInterval(this.nextSlideInterval);
		for (var i=0; i < this.faders.length; i++) {
			this.hideFader(i);
		};
		this.currentSlide = null;
	},
	nextSlide: function() {
		if (false && this._isMouseOver) {
			this.stopSlideShow();
		} else {
			var currentSlide = Math.floor(Math.random()*this.faders.length);
			if (this.currentSlide == currentSlide) {
				currentSlide = (currentSlide+1) % this.faders.length;
			}
			for (var i=0; i < this.faders.length; i++) {
				(i == currentSlide) ? this.showFader(i) : this.hideFader(i);
			};
			this.currentSlide = currentSlide;
		}
	},
	showFader: function(id) {
		$(this.faders[id]).stop();
		$(this.faders[id].css({'z-index': 40}));
		$(this.faders[id]).animate({opacity: 1}, this.currentSlide >= 0 ? 0 : 500);
	},
	hideFader: function(id) {
		$(this.faders[id]).stop();
		$(this.faders[id]).css('z-index', 50);
		$(this.faders[id]).animate({opacity: 0}, 500);
	},
	isMouseOver: function(event) {
		var offset = this.container.offset();
		return (
			event.pageX > offset.left &&
			event.pageX < offset.left+this.container.width() &&
			event.pageY > offset.top &&
			event.pageY < offset.top+this.container.height()
		)
	}
}
