function Gallery(id, userSettings) {
	
	var Gallery = this;
	
	this.container = null;
	this.elements = [];
	this.preview = null;
	this.previewImage = null;
	this.currentLink = null;
	this.currentIndex = null;
	this.navigation = null;
	this.showElements = null;
	
	this.defaultSettings = {
		elementClass : 'element',
		previewClass : 'preview',
		previewFormat : 'preview',
		start : 0,
		overOpacity : 1,
		outOpacity : .5,
		message : 'Er zijn geen afbeeldingen beschikbaar.',
		navigation : true,
		navigationClass : 'navigation',
		lightwindow : true,
		lightwindowFormat : 'lightwindow',
		lightwindowGallery : null
	};
	this.settings = {};
	
	this.overrideSettings = function() {
		for(var key in this.defaultSettings)
			this.settings[key] = this.defaultSettings[key];
		for(var key in userSettings)
			this.settings[key] = userSettings[key];
	};
	
	this.setContainer = function() {
		this.container = document.getElementById(id);
	};
	
	this.showMessage = function() {
		
		var message = document.createElement('p');
		message.appendChild(document.createTextNode(this.settings.message));
		
		this.container.appendChild(message);
		
	};
	
	this.setElements = function() {
		
		var elements = this.container.getElementsByTagName('div'),
			counter = 0;
		
		for(var i = 0, j = elements.length; i < j; i++) {
			var element = elements[i];
			if(element.className == this.settings.elementClass) {
				this.setElement(element, counter);
				counter++;
			};
		};

	};
	
	this.setElement = function(element, index) {
		
		var childNode = element.firstChild;
		element.index = index;
		element.previewSource = this.replaceFormat(childNode.href, this.settings.previewFormat);
		
		if(!this.currentLink)
			this.currentLink = childNode;
		
		element.setOpacity = function(n) {
			this.style.filter = 'alpha(opacity=' + n * 100 + ')';
			this.style.opacity = n;
		};
		element.isActive = function() {
			return this.index == Gallery.currentIndex;
		};
		element.onmouseover = function() {
			this.setOpacity(Gallery.settings.overOpacity);
		};
		element.onmouseout = function() {
			if(!this.isActive())
				this.setOpacity(Gallery.settings.outOpacity);
		};
		element.onclick = function() {
			if(Gallery.settings.lightwindow)
				Gallery.currentLink = element.firstChild;
			Gallery.showPreview(element.index);
			return false;
		};
		
		if(this.settings.lightwindow) {
			element.lightwindowSource = this.replaceFormat(childNode.href, this.settings.lightwindowFormat);
			if(this.settings.lightwindowGallery) {
				childNode.rel = '[' + this.settings.lightwindowGallery + ']';
				lightwindow.appendToGallery(childNode, this.settings.lightwindowGallery);
			};
		};
		
		this.elements.push(element);
		
	};
	
	this.createPreview = function() {
		
		this.preview = document.createElement('div');
		this.preview.className = this.settings.previewClass;
		
		if(this.settings.lightwindow) {
			
			this.previewLink = document.createElement('a');
			this.previewLink.href = '';
			this.previewLink.title = '';	
			
			this.previewLink.onclick = function() {
				this.href = Gallery.elements[Gallery.currentIndex].lightwindowSource;
				Gallery.showPopup();
				return false;
			};
			
			this.previewImage = document.createElement('img');
			this.previewImage.src = '';
			this.previewImage.alt = '';
			
			this.previewLink.appendChild(this.previewImage);
			this.preview.appendChild(this.previewLink);
			
		} else {
			
			this.previewImage = document.createElement('img');
			this.previewImage.src = '';
			this.previewImage.alt = '';
			
			this.preview.appendChild(this.previewImage);
			
		};
		
		this.container.insertBefore(this.preview, this.container.firstChild);
		
	};
	
	this.setNavigation = function() {
		
		this.navigation = document.createElement('div');
		this.navigation.className = this.settings.navigationClass;
		
		var previousButton = document.createElement('div');
		previousButton.className = 'previous';
		previousButton.onclick = function() {
			Gallery.showPreviousElement();
		};
		previousButton.appendChild(document.createTextNode('Vorige'));
		
		var nextButton = document.createElement('div');
		nextButton.className = 'next';
		nextButton.onclick = function() {
			Gallery.showNextElement();
		};
		nextButton.appendChild(document.createTextNode('Volgende'));
		
		this.navigation.appendChild(previousButton);
		this.navigation.appendChild(nextButton);
		
		this.preview.appendChild(this.navigation);
		
	};
	
	this.showPreview = function(index) {
		
		this.currentIndex = index;
		this.previewImage.src = this.elements[index].previewSource;
		
		for(var i = 0, j = this.elements.length; i < j; i++) {
			if(i == this.currentIndex)
				this.elements[i].setOpacity(this.settings.overOpacity);
			else
				this.elements[i].setOpacity(this.settings.outOpacity);
		};
		
	};
	
	this.hideElements = function() {
		for(var i = 0, j = this.elements.length; i < j; i++)
			this.elements[i].style.display = 'none';
	};
	
	this.showPopup = function() {
		lightwindow.show(this.currentLink);
	};
	
	this.showPreviousElement = function() {
		this.showPreview(this.getPreviousIndex());
	};
	
	this.showNextElement = function() {
		this.showPreview(this.getNextIndex());
	};
	
	this.getPreviousIndex = function() {
		return this.currentIndex > 0 ? this.currentIndex - 1 : this.elements.length - 1;
	};
	
	this.getNextIndex = function() {
		return this.currentIndex < this.elements.length - 1 ? this.currentIndex + 1 : 0;
	};
	
	this.replaceFormat = function(source, format) {
		return source.substring(0 , source.indexOf('&')) + '&format=' + format;
	};
	
	this.init = function() {
		
		this.setContainer();
		
		if(this.container) {
			
			this.overrideSettings();
			this.setElements();
			this.showElements = this.elements.length > 1;
			
			if(this.elements.length) {
				
				this.createPreview();
				
				if(this.settings.navigation && this.showElements)
					this.setNavigation();
				
				if(!this.showElements)
					this.hideElements();
					
				this.showPreview(this.settings.start);
				
			} else {
				
				this.showMessage();
				
			};
			
		};
		
	};

};
