function ego_gallery(path, skin, max)
{
	var ids = { // Verschiedene IDs
		gallery : 'gallery',
		image : 'gallery_image',
		title : 'gallery_title'
	};
	
	var script = '/light_gallery/gallery.php'; // PHP Script
	var display_thumbs = 4; // Angezeigte Thumbnails
	
	var maxWidth = (max && max[0])? max[0]:''; // maximale Breite
	var maxHeight = (max && max[1])? max[1]:''; // maximale Höhe
		
	var thumb_index = 0;
	this.imgs = [];
	
	/* Scrollposition ermitteln */
	function scrollPos()
	{
		return ((y = window.pageYOffset)? y:document.documentElement.scrollTop)+'px';
	}
	
	/* Holt die URL des aktuell angezeigten Bildes */
	function currentImage()
	{
		var img = document.getElementById(ids.image);
		return stripSizes(img.src);	
	}
	
	/* Entfernt width und height Parameter aus der URL, ggf auch den Pfad */
	function stripSizes(str, basename)
	{
		var output = str.replace(/(-width-\d+)/i, '').replace(/(-height-\d+)/i, '');
		output = output.replace(/(&?width=\d+)/i, '').replace(/(&?height=\d+)/i, '');
		
		if(basename)
		{
			var base = output.match(/[\/|\\]([^\\\/]+)$/);
			output = base? base[1]:output;
		}

		return output;
	}
	
	/* Akutelles und neues Bild haben die selbe ID */
	function sameId(url)
	{
		function getId(str)
		{
			var isId = str.match(/[?&]id=(\d+)/);
			return isId? isId[1]:false;
		}
		
		var id_1 = getId(url);
		var id_2 = getId(currentImage());

		return (id_1 && id_2)? ((id_1 == id_2)? true:false):false;
	}
	
	/* Angezeigtes Bild skalieren */
	function scaleImage(newImg)
	{
		var img = document.getElementById(ids.image);
		
		if(img)
		{
			var _img = (newImg === undefined)? img:newImg;
	
			if(!img.getAttribute('width')) img.setAttribute('width', img.width);
			if(!img.getAttribute('height')) img.setAttribute('height', img.height);
			
			img.width = (maxWidth != '' && _img.width > maxWidth)? maxWidth:_img.width;
			img.height = (maxHeight != '' && _img.height > maxHeight)? maxHeight:_img.height;
		}					
	}
	
	/* Ein Event hinzufügen ohne es zu überschreiben */
	function addEvent(obj, event, func)
	{
		if(obj.addEventListener)
		{
			obj.addEventListener(event, func, false); 
			return true; 
		}
		else if(obj.attachEvent)
		{
			var rev = obj.attachEvent('on'+event, func); 
			return rev; 
		}
			
		return false; 
	}	
	
	/* Gallery laden */
	function loadGallery(obj, url)
	{
		var _skin = skin? 'site/'+skin:'';
		
		req.open('POST', path+_skin+script, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send('path='+path+'&url='+url);

		req.onreadystatechange = function()
		{
			if(req.readyState == 4)
			{
				obj.innerHTML = req.responseText;
				scaleImage();
			}
		};		
	}	
	
	/* Ajax Objekt */
	var ajax = {
		_factories : [
			function(){ return new XMLHttpRequest(); },
			function(){ return new ActiveXObject('Msxml2.XMLHTTP'); },
			function(){ return new ActiveXObject('Msxml3.XMLHTTP'); },
			function(){ return new ActiveXObject('Microsoft.XMLHTTP'); }	
		],
		
		newObject : function()
		{
			var req = false;
	
			for(var i=0;i<this._factories.length;i++)
			{
				try {
					req = this._factories[i]();
				} catch(e) { continue; }
				break;
			}
			
			return req;			
		}
	};
	
	var req = ajax.newObject();	

	/* Öffnet die Gallerie und erstellt den Container */
	this.open = function(url)
	{
		var container = document.createElement('div');
		container.setAttribute('id', ids.gallery);
		container.style.top = scrollPos();
		
		var background = document.createElement('div');
		background.setAttribute('id', 'g_background');
		
		var content = document.createElement('div');
		content.setAttribute('id', 'g_content');
		
		var box = document.createElement('div');
		box.setAttribute('id', 'g_box');
		
		content.appendChild(box);	

		window.onscroll = function(){ // Position beim Scrollen anpassen
			container.style.top = scrollPos();
		};
		
		container.appendChild(background);
		container.appendChild(content);
		
		document.body.appendChild(container, document.body.firstChild);
		
		loadGallery(box, url); // Gallerie in Container laden
	};
	
	/* Schliesst die Gallerie und entfernt den Container */
	this.remove = function()
	{
		var gallery = document.getElementById(ids.gallery);
		
		if(gallery)
		{
			window.onscroll = null;
			document.body.removeChild(gallery);
		}
		else
			window.close();
	};	
	
	/* Zeigt ein Bild an */
	this.view = function(url, title)
	{
		var img = document.getElementById(ids.image);
		var text = document.getElementById(ids.title);
		
		var newImg = new Image();
		img.style.display = 'none'; // Bild bis es geladen und skaliert ist ausblenden		
				
		newImg.onload = function(){
			scaleImage(newImg);

			img.src = url;
			img.style.display = '';
		};
		
		newImg.src = url;	
		text.innerHTML = title;
		
		this.getImages();
		for(var i=0;i<this.imgs.length;i++)
		{	
			if(this.imgs[i] && stripSizes(this.imgs[i].url, true) == stripSizes(url, true))
			{		
				var arrows = [
					document.getElementById('gallery_image_back'),
					document.getElementById('gallery_image_next')
				];
				
				arrows[0].style.display = this.imgs[i-1]? '':'none';
				arrows[1].style.display = this.imgs[i+1]? '':'none';			
				break;
			}
		}		
	};
	
	/* Bild in einem neuen Fenster öffnen */
	this.popup = function(url) 
	{
		var popup = window.open(stripSizes(url), 'gallery_popup');
		popup.focus();
	}
	
	/* Ermittelt alle verfügbaren Bilder */
	this.getImages = function()
	{
		if(this.imgs.length == 0)
		{
			var i=0;
			while(true)
			{
				var img = document.getElementById('gallery_image_'+i);
				
				if(img)
					this.imgs.push({url:img.src, title:img.title});
				else
					break;
					
				i++;
			}
		}	
	};	
	
	/* Nächstes, bzw. Letztes Bild anzeigen */
	this.nextImage = function(dir)
	{
		this.getImages();
		
		for(var i=0;i<this.imgs.length;i++) {
			var img = dir? this.imgs[i-1]:this.imgs[i+1];

			if(img && (stripSizes(img.url) == currentImage() || sameId(img.url)))
			{
				this.view(stripSizes(this.imgs[i].url), this.imgs[i].title);
				
				var arrows = [
					document.getElementById('gallery_image_back'),
					document.getElementById('gallery_image_next')
				];
				
				arrows[0].style.display = this.imgs[i-1]? '':'none';
				arrows[1].style.display = this.imgs[i+1]? '':'none';
				break;
			}
		}		
	};	
	
	/* Thumbnail Liste vor, bzw zurück schieben */
	this.thumblist = function(dir)
	{
		var thumbs = [
			document.getElementById('gallery_thumb_'+(thumb_index-(dir?0:1))),
			document.getElementById('gallery_thumb_'+(thumb_index+display_thumbs-(dir?0:1))),
			document.getElementById('gallery_thumb_back'),
			document.getElementById('gallery_thumb_next')
		];
		
		if(thumbs[0] || thumbs[1])
		{
			if(thumbs[0]) thumbs[0].style.display = dir? 'none':'';
			if(thumbs[1]) thumbs[1].style.display = dir? '':'none';
			
			thumb_index += dir? 1:-1;

			thumbs[2].style.display = (thumb_index> 0)? '':'none';
			thumbs[3].style.display = document.getElementById('gallery_thumb_'+(thumb_index+display_thumbs))? '':'none';
		}	
	};
	
	/* Bild beim Laden skalieren, auch in einem Popup Fenster */
	addEvent(window, 'load', function(){
			scaleImage(document.getElementById(ids.image));
		}
	);			
}
