function hideItems() {
	$('.items').each(
		function(item) {
			$(this).hide();
		}
	);
};

function attachEvents() {
	$('.group a').each(
		function() {
			$(this).click(
				function(event) {
					var group = this.id.split('_');
					var div = $('#' + group[0] + '_items');
					div.toggle('fast');
					event.preventDefault();
					if (div.height() == 1) {
						$('html,body').animate({scrollTop: $(this).offset().top - 20}, 'slow');
					}
				}
			);
		}
	)
}

Gallery = {
	_container: null,
	_list: null,
	defaultImage: null,
	
	init: function(container, list) {
		if (!list || !container) {
			return;
		}
		
		this._list = list;
		this._container = container;
		this._createGallery();
	},
	
	_createGallery: function() {
		$('.' + this._list).each(
			function() {
				$(this).children().each(
					function () {
						if (this.tagName == 'LI') {
							Gallery.alterLiElement(this);
							$(this).children().each(
								function() {
									Gallery.alterLink(this);
								}
							);
						}
					}
				);
			}
		);
		
		this.createDivs();
	},
	
	createDivs: function() {
		var loader = document.createElement('div');
		loader.id = 'loader';
		loader.innerHTML = '<img src="' + Gallery.defaultImage.src + '" alt="' + Gallery.defaultImage.alt + '" />';
		$('#' + this._container).prepend(loader);
	},
	
	alterLiElement: function(li) {
		$(li).children().each(
			function () {
				if (this.tagName == 'IMG') {
					var imageSource = this.src;
					
					if (!Gallery.defaultImage) {
						Gallery.defaultImage = {
							src: this.src,
							alt: this.alt,
							height: this.height
						};
					}
					
					this.src = Gallery.getThumbSource(this.src);
					image = this;
					li.removeChild(this);
					var link = Gallery.addLink(image, imageSource);
					$(li).append(link);
				}
				
				$(li).addClass('gallery_element');
			}
		);
	},
	
	getThumbSource: function(src) {
		var parts = src.split('/');
		var file = parts.pop();
		var path = parts.join('/');
		return path + "/thumb/" + file;
	},
	
	addLink: function(image, href) {
		var link = document.createElement('a');
		link.href = href;
		$(link).append(image);
		return link;
	},
	
	alterLink: function(element) {
		if (element.tagName != 'A') {
			return;
		}
		
		if (!element.firstChild || element.firstChild.tagName != 'IMG' || $(element).children().length != 1) {
			return;
		}
		$(element).bind('click', function(event) {
			event.preventDefault();
			$('html,body').animate({scrollTop: $('#gallery_title').offset().top - 20}, 'slow');
			Gallery.showImage(element.href);
		});
	},
	
	showImage: function(src) {
		var largeImage = new Image();
		$(largeImage).attr('src', src).load(
			function() {
				$(largeImage).hide();
				$('#loader').css({height: largeImage.height});
				$('#loader img').fadeOut(400).remove();
				$('#loader').append(largeImage);
				$(largeImage).fadeIn(400);                      
			}
		);
	}
};
