var HomeGallery = new Class({
	gallery : null,
	images : null,
	left : null,
	right : null,
	caption : null,

	animating : false,
	fx : null,

	imageClass : '.home_ga_item',

	initialize : function(gallery) {
		this.gallery = $(gallery);

		this.images = $('home_ga_images');
		this.left = $('home_ga_left');
		this.right = $('home_ga_right');
		this.caption = $('home_ga_text');

		this.previous = this.previous.bind(this);
		this.next = this.next.bind(this);
		this.onComplete = this.onComplete.bind(this);

		this.images.getChildren(this.imageClass).each(function(image, index) {
			image.setStyles({
				'left': 626 * index,
				'top': 0
			});
		});
		this.images.setStyle('width', 626 * this.images.getChildren(this.imageClass).length);

		this.attach();

		this.show(this.images.getElement(this.imageClass));
	},
	attach : function() {
		this.left.addEvent('dblclick', function(event) {event.stop();});
		this.right.addEvent('dblclick', function(event) {event.stop();});
		this.left.addEvent('click', this.previous);
		this.right.addEvent('click', this.next);
	},
	detatch : function() {
		this.left.removeEvent('click', this.previous);
		this.left.removeEvent('click', this.next);
	},

	show : function(picture) {
		if (this.animating) {
			this.fx.cancel();
		} else {
			this.animating = true;
		}
		
		this.caption.set('text', picture.get('alt'));

		this.left.fade($chk(picture.getPrevious(this.imageClass)) ? 'in' : 'out');
		this.right.fade($chk(picture.getNext(this.imageClass)) ? 'in' : 'out');

		picture.addClass('current');
		if ($chk(this.current)) {
			this.current.removeClass('current');
		}

		this.current = picture;
		this.fx = new Fx.Tween(this.images, {property : 'left', onComplete : this.onComplete}).start(picture.getStyle('left').toInt() * -1);
		return this;
	},

	previous : function(event) {
		var previous = this.current.getPrevious(this.imageClass);
		event.stop();
		if (previous) {
			this.show(previous);
		}
	},
	next : function(event) {
		var next = this.current.getNext(this.imageClass);
		event.stop();
		if (next) {
			this.show(next);
		}
	},
	onComplete : function() {
		this.animating = false;
	}
});

window.addEvent('domready', function() {

	(function() {
		var visible = ($chk(location.hash) ? location.hash.substring(1) : 'fw');

		(['fw', 'lm', 'sd', 'ga']).each(function(code) {
			var nav = $('home_' + code + '_nav');
			var box = $('home_' + code + '_box');

			nav.addEvent('click', function(event) {
				if (code != visible) {
					if (visible) {
						var oldNav = $('home_' + visible + '_nav');
						var oldBox = $('home_' + visible + '_box');

						oldBox.fade('out');
						oldBox.removeClass('current');
						oldNav.removeClass('current');
					}

					nav.addClass('current');
					box.addClass('current');
					box.fade('in');
					
					location.hash = code;
					visible = code;
				}

				event.stop();
			});

			if (code == visible) {
				nav.addClass('current');
			} else {
				box.setStyles({
					display: 'block',
					visibility: 'hidden'
				});
				box.fade('hide');
			}
		});

	})();

	new HomeGallery('home_ga');
});
