function DD_Lightbox(setname) {

	this.slideshow = false;
	

	this.timer = 5000;
	

	this.anchorpoint = "bottom-right";	
	

	var self = this;
	

	var setname = setname;
	

	var interval;
	

	var elements = new Array();
	

	var currentelement;
	
	var lang = {
		close: "Schließen",
		prev: "Vorheriges Bild",
		next: "Nächstes Bild",
		slideshow: "Slideshow",
		yes:"Ja",
		no:"Nein",
		on:"An",
		off:"Aus"
	};
	

	var vbid = "size_"+setname;
	

	var menuheight = 0;
	

	var startup = true;
	
	/**
	 * Initialisiern 
	 */
	this.init = function() {
		clickOpenLink();
		resetSettings();
	}
	

	var resetSettings = function() {
		currentelement = 0;
	}
	

	this.add = function(src) {
		elements.push(src); 
	}
	
	var clickOpenLink = function() {
		$("a.sizeup[@rel^="+setname+"]").click(function() {
			//Einstellungen zurücksetzen
			resetSettings();
			
			//Nur hinzufügen, wenn noch nicht vorhanden
			if($("body:has(div#"+vbid+")").size() == 0) {
				addViewingBox();
						
				positionViewingBox();
				
				//1. Element laden
				showElement(currentelement);
			}
			return false;
		});
		$("a.sizeup[@rel^="+setname+"] img").after('<span class="plus"></span>');
		if($.browser.msie || $.browser.mozilla || $.browser.opera) {
			$("a.sizeup[@rel^="+setname+"] .plus").css('margin-bottom', '3px');
		}
	}
	
	var clickCloseLink = function() {
		$("div#"+vbid+" .close a").click(function() {
			$("div#"+vbid).fadeOut("normal", function() { removeViewingBox(); });
			//Wenn die Slideshow aktiviert wurde, dann den Timer löschen
			if(self.slideshow == true) { deactivateTimer(); }
			return false;
		});
	}
	
	var addViewingBox = function() {
		$("body").append("<div class=\"sizeup\" id=\""+vbid+"\"></div>");
		$("div#"+vbid).html("<div class=\"content\"></div>");
		$("div#"+vbid+" .content").after("<p class=\"close\"><a href=\"#\">"+lang.close+"</a></p>");
		$("div#"+vbid+" .content").after("<div class=\"menu\"></div>");
		$("div#"+vbid+" .menu").html("<p class=\"next\"><a href=\"#\">"+lang.next+"</a></p>");
		$("div#"+vbid+" .menu .next").after("<ul class=\"numbers\"></ul>");
		$("div#"+vbid+" .menu .numbers").after("<p class=\"prev\"><a href=\"#\">"+lang.prev+"</a></p>");
		
		//Slideshow-Button hinzufügen, wenn mehr als 1 Element vorhanden
		if(elements.length > 1) {
			$("div#"+vbid+" .next").after("<p class=\"slideshow\">"+lang.slideshow+": <a href=\"#\">"+((self.slideshow) ? lang.on : lang.off)+"</a></p>");
			$("div#"+vbid+" .slideshow a").click(function() {
				self.slideshow = !self.slideshow;
				
				if(self.slideshow == true) {
					activateTimer();
					$(this).text(lang.on);
				} else {
					deactivateTimer();
					$(this).text(lang.off);
				}
				return false;
			});
		}
		

		menuheight = parseInt($("div#"+vbid+" .menu").height()) + 20;
		
		$("div#"+vbid).hide();
		$("div#"+vbid).css("width", "0px");
		$("div#"+vbid).css("height", "0px");
		
		$("div#"+vbid+" .next").hide();
		$("div#"+vbid+" .prev").hide();
		
		clickCloseLink();
		generateNavigation();
	}
	
	var removeViewingBox = function() {
		$("div#"+vbid).remove();
		startup = true;
	}
	

	var generateNavigation = function() {
		//Nummern-Navigation erstellen
		/*if(elements.length > 1) {
			for(var i=0;i<elements.length; i++) {
				$("div#"+vbid+" .numbers").append("<li><a href=\"#\">"+(i+1)+"</a></li>");
				$("div#"+vbid+" .numbers li:eq("+ i +") a").click(function() {
					currentelement = parseInt($(this).text())-1;
					hideElement(function(){showElement(currentelement);});
					return false;
				});
			}
		}*/
		
		//Rückwärts-Link
		$("div#"+vbid+" .prev a").click(function() {
			if(hasElement(currentelement-1)) {
				currentelement--;
				hideElement(function(){showElement(currentelement);});
			}
			return false;
		});
		
		//Vorwärts-Link
		$("div#"+vbid+" .next a").click(function() {
			if(hasElement(currentelement+1)) {
				currentelement++;
				hideElement(function(){showElement(currentelement);});
			}
			return false;
		});
	}
	
	var changeActiveNumber = function(newactive) {
		$("div#"+vbid+" .numbers li.active").removeClass("active");
		$("div#"+vbid+" .numbers li:eq("+newactive+")").addClass("active");
	}
		
	var checkNavigation = function() {
		if(hasElement(currentelement+1)) {
			$("div#"+vbid+" .prev").removeClass('spacer');
			//Beim Startup einfach einblenden und nicht einfaden
			if(startup == true) {
				$("div#"+vbid+" .next").show();
			} else {
				$("div#"+vbid+" .next").fadeIn("fast");
			}
		} else {
			if(startup == true) {
				$("div#"+vbid+" .next").hide();
			} else {
				$("div#"+vbid+" .next").fadeOut("fast", function() {$("div#"+vbid+" .prev").addClass('spacer');});
			}
		}
		if(hasElement(currentelement-1)) {
			if(startup == true) {
				$("div#"+vbid+" .prev").show();
			} else {
				$("div#"+vbid+" .prev").fadeIn("fast");
			}
		} else {
			if(startup == true) {
				$("div#"+vbid+" .prev").hide();
			} else {
				$("div#"+vbid+" .prev").fadeOut("fast");
			}
		}
	}
	
	var showElement = function(index) {
		imgPreloader = new Image();
		//Funktion, wenn das Bild geladen wurde
		var fncOnLoad = function(evt, mywidth, myheight){
			imgPreloader.onload = null;
			
			width = (mywidth == undefined) ? imgPreloader.width : mywidth;
			height = (myheight == undefined) ? imgPreloader.height : myheight;
			
			$("div#"+vbid+" .content").html(imgPreloader);
			$("div#"+vbid+" .content img").hide();
			
				resizeViewingBox(width, height, function() {
					if(startup == true) {
						checkNavigation();
					}
					
					//Slideshow-Modus aktiviert?
					if(self.slideshow == true) {
						$("div#"+vbid+" .content img").fadeIn("normal", activateTimer);
						
					} else {
						$("div#"+vbid+" .content img").fadeIn("normal");
					}
					$("div#"+vbid+" .content img").attr("width", width);
					$("div#"+vbid+" .content img").attr("height", height);
					$("div#"+vbid).removeClass('loading');
					

					startup = false;
				});
			
			
			if(startup == false) {
				checkNavigation();
			}			
		}
		if(startup == true) {
			var loadinginterval = window.setInterval (function() {
				window.clearInterval(loadinginterval);
				$("div#"+vbid).addClass('loading');
			}, 700);
		} else {
			$("div#"+vbid).addClass('loading');
		}
		
		if(jQuery.browser.opera == false && jQuery.browser.safari == false) {
			imgPreloader.onload = fncOnLoad;
		}
		
		imgPreloader.src = elements[index];
		

		if(jQuery.browser.opera == true || jQuery.browser.safari == true) {
			var waitforimgsize = window.setInterval (function() {
				if(imgPreloader.complete) {
					window.clearInterval(waitforimgsize);

					fncOnLoad(null, imgPreloader.width, imgPreloader.height);
				} else {

				}
			}, 300);
		}
	}
	
	var hideElement = function(callback) {
		if(self.slideshow == true) {
			deactivateTimer();
		}
		
		$("div#"+vbid+" .content img").fadeOut("normal", callback);
	}
	
	var resizeViewingBox = function(width, height, callback) {
		if(callback == undefined) callback = null;

		if(parseInt($("div#"+vbid).css("height")) != height || parseInt($("div#"+vbid).css("width")) != width) {
			$("div#"+vbid).animate( { height:height+"px"}, { queue:false, duration:500, "easing": "swing"} ).animate({ width:width+"px"} , {duration:500, "easing": "swing", complete:callback});
			
		} else {
			callback();
		}
	}


	var positionViewingBox = function() {		
		var child = ($("a.sizeup[@rel^="+setname+"] img").length) ? $("a.sizeup[@rel^="+setname+"] img") : $("a.sizeup[@rel^="+setname+"]");
		
		var top = child.offset().top;
		var bottom = (getH() - child.offset().top - child.height() - 10);
		var left = child.offset().left;
		var right = (getW() - child.offset().left - child.width() - 10);
		
		if(jQuery.browser.msie == true && jQuery.browser.version.substr(0,1)=="6") {
			bottom = getPageSizeWithScroll()[1] - top - child.height() - 2;
		} else if(jQuery.browser.opera == true) {
			bottom -= 40;
		} else if(jQuery.browser.safari == true) {
			bottom += 50;
		}
		

		$("div#"+vbid).css("position", "absolute");
		

		if(self.anchorpoint == "bottom-right") {
			$("div#"+vbid).css("bottom", bottom + "px");
			$("div#"+vbid).css("right", right+"px");
			

		} else if(self.anchorpoint == "bottom-left") {
			$("div#"+vbid).css("bottom", bottom + "px");
			$("div#"+vbid).css("left", left + "px");
			

		} else if(self.anchorpoint == "top-right") {
			$("div#"+vbid).css("top", top + "px");
			$("div#"+vbid).css("right", right+"px");
			

		} else {
			$("div#"+vbid).css("top", top + "px");
			$("div#"+vbid).css("left", left + "px");
		}
		

		$(window).resize(positionViewingBox);
	}
	
	function getW(){
		var w;
		if(document.innerWidth){ w=document.innerWidth;
		} else if(document.documentElement.clientWidth){ w=document.documentElement.clientWidth;
		} else if(document.body){ w=document.body.clientWidth; }
		return w;
	}

	function getH(){
		var h;
		if(document.innerHeight){ h=document.innerHeight;
		} else if(document.documentElement.clientHeight){ h=document.documentElement.clientHeight;
		} else if(document.body){ h=document.body.clientHeight; }
		return h;
	}
	

	function getPageSizeWithScroll(){
		if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
	  	}
		arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
		//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
		return arrayPageSizeWithScroll;
	}
	

	var hasElement = function(index) {
		return (elements[index] != undefined);
	}
	
	var activateTimer = function() {
		interval = window.setInterval (function() {
			if(hasElement(currentelement+1)) {
				currentelement++;
			} else {
				currentelement = 0;
			}
			hideElement(function(){showElement(currentelement);});
		}, self.timer);
	}
	
	var deactivateTimer = function() {
		window.clearInterval(interval);
	}
	function debug(text) {
		((window.console && console.log) ||
		(window.opera && opera.postError) ||
		window.alert).call(this, text);
	}
}
