// redWhite LightBox

// Autor: Nico Pradella
// E-Mail: nico@zwanzig-zehn.de

// Verwendung nur unter Genehmigung des Autors!

// Lightbox ist tw. per CSS definiert.
// Config: Ordner, in dem sich die Button-Grafiken befinden.
	var IMGURL='http://www.malteser-kreis-heinsberg.de/template/';
	
	var lightBoxSpeed=20;

	var factorWidth, factorHeight, lbSlide, LightBoxOpen, LightBoxInAction, dragDropTO, mousePosX, mousePosY;
	var dragDropStart=0;
	var dragDropping=0;
	var windowOpen=0;
	var slideStatus=0;
	var sliding=0;
	var minWidth=300;

	function getMousePos(e) {

		mousePosX = (document.all) ? event.clientX + document.body.scrollLeft : e.pageX;
		mousePosY = (document.all) ? event.clientY + document.body.scrollTop : e.pageY;

		if(mousePosX < 0) mousePosX=0;
		if(mousePosY < 0) mousePosY=0;

	}

	if(!document.all) document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove=getMousePos;

	window.onmouseup=function() { if(dragDropTO) clearTimeout(dragDropTO); }

	function MSIEdragDrop() {

		if(dragDropping == 1) {

			lightboxMessage(0);
			clearTimeout(dragDropTO);
			dragDropping=0;

		} else {

			lightboxMessage(1);
			dragDropStart=1;
			dragDropWin();
			dragDropping=1;

		}

	}

	function lightboxMessage(id) {

		var LightBox=document.getElementById("lightbox");
		var LightBoxMsg=document.getElementById("lightbox-message");

		currentWidthUnExp=LightBox.style.height;
		currentHeightUnExp=LightBoxMsg.style.height;
		var numReg = /\d+/;
		var currentWidth=parseInt(numReg.exec(currentWidthUnExp));
		var currentHeight=parseInt(numReg.exec(currentHeightUnExp));
		
		var goOn=0;
		
		if(id == 1) {
			if(currentHeight < 40) {
				LightBox.style.height=(currentWidth+1)+"px";
				LightBoxMsg.style.height=(currentHeight+1)+"px";
				goOn=1;
			}
		} else {
			if(currentHeight > 0) {
				LightBox.style.height=(currentWidth-1)+"px";
				LightBoxMsg.style.height=(currentHeight-1)+"px";
				goOn=1;
			}
		}
		if(currentHeight == 0) LightBoxMsg.style.display="none";
		else LightBoxMsg.style.display="block";

		if(goOn == 1) window.setTimeout("lightboxMessage("+id+")", 10);
		
	}

	function dragDropWin() {

		var lightbox=document.getElementById("lightbox");

		if(dragDropStart == 1) {

			var elementTopCSS=lightbox.style.top;
			var elementLeftCSS=lightbox.style.left;

			var numReg = /\d+/;
			var elementTop=parseInt(numReg.exec(elementTopCSS));
			var elementLeft=parseInt(numReg.exec(elementLeftCSS));

			dragDropTopD=mousePosY-elementTop;
			dragDropLeftD=mousePosX-elementLeft;

			dragDropStart=0;

		}

		lightbox.style.top=(mousePosY-dragDropTopD)+"px";
		lightbox.style.left=(mousePosX-dragDropLeftD)+"px";

		var dragDropFunc = function() { dragDropWin(); }
		dragDropTO=window.setTimeout(dragDropFunc, 1);
	}

	function createLightBox() {

		var Body=document.getElementsByTagName("body")[0];

		var lightBoxCode='<div id="lightbox" style="display: none; width: 0px; height: 0px; top:20px; left: 20px;">';
		lightBoxCode+='<div id="lightbox-content" style="display: block; width: 0px; height: 0px">';
		lightBoxCode+='<img id="close-img" src="'+IMGURL+'lightbox_close.gif" width="20" height="20" alt="Schlie&szlig;en" onclick="closeLightBox()" style="display: block; cursor: pointer" />';
		lightBoxCode+='<div id="lightbox-head"'+(navigator.userAgent.indexOf('MSIE') < 0  ? ' onmousedown="dragDropStart=1;dragDropWin();':'')+(navigator.userAgent.indexOf('MSIE') > 0  ? ' onclick="MSIEdragDrop();':'')+'"></div>';
		lightBoxCode+='<div id="lightbox-message" style="display: none; height: 0px"><span>Klicken Sie erneut die linke Mausetaste, um das Fenster an der gew&uuml;nschten Position abzulegen!</span></div>';
		lightBoxCode+='<center><img id="lightbox-img" src="" alt="" width="" height="" /></center><div id="lightbox-navi" style="display: none"></div></div></div>';

		Body.innerHTML+=lightBoxCode;
		
		if(navigator.userAgent.indexOf('MSIE') > 0) document.getElementById("close-img").style.display="none";
	}

	function lbSlide(aimWidth, aimHeight) {

		var LightBox=document.getElementById("lightbox");

		var currentWidthUnExp=LightBox.style.width;
		var currentHeightUnExp=LightBox.style.height;
		var numReg = /\d+/;
		var currentWidth=parseInt(numReg.exec(currentWidthUnExp));
		var currentHeight=parseInt(numReg.exec(currentHeightUnExp));

		if(currentWidth < aimWidth) newWidth=(aimWidth > (currentWidth+factorWidth) ? currentWidth+factorWidth : aimWidth);
		if(currentHeight < aimHeight) newHeight=(aimHeight > (currentHeight+factorHeight) ? currentHeight+factorHeight : aimHeight);
		
		if(currentWidth > aimWidth) newWidth=(aimWidth < (currentWidth-factorWidth) ? currentWidth-factorWidth : aimWidth);
		if(currentHeight > aimHeight) newHeight=(aimHeight < (currentHeight-factorHeight) ? currentHeight-factorHeight : aimHeight);

		LightBox.style.width=newWidth+"px";
		LightBox.style.height=newHeight+"px";
		
		if(navigator.userAgent.indexOf('MSIE') > 0 && newWidth > (aimWidth/2)) document.getElementById("close-img").style.display="block";

		if(newWidth == aimWidth && newHeight == aimHeight) {

			if(aimWidth == 0 && aimHeight == 0) {
				LightBox.style.display="none";
				LightBox.style.top="20px";
				LightBox.style.left="20px";
			}
			LightBoxInAction=0;

		} else window.setTimeout("lbSlide("+aimWidth+", "+aimHeight+")", 10);

	}

	function openGalleryItem(itemId) {
	
		var itemId=parseInt(itemId);

		var src=gal[itemId]["src"];
		var width=gal[itemId]["width"];
		var height=gal[itemId]["height"];
		var alt=gal[itemId]["alt"];

		openLightBox(src, width, height, alt, 1, itemId);

	}
	
	function openLightBox(src, width, height, alt, galleryItem, galleryItemId) {

		if(LightBoxInAction == 1) return;

		if(!document.getElementById("lightbox")) createLightBox();

		var LightBox=document.getElementById("lightbox");
		var LightBoxHead=document.getElementById("lightbox-head");
		var LightBoxContent=document.getElementById("lightbox-content");
		var Img=document.getElementById("lightbox-img");
		var LightBoxNavi=document.getElementById("lightbox-navi");

		if(LightBoxOpen == 1) {
			if(src == Img.src) return;
			aimWidth=0;
			aimHeight=0;
			lbSlide();
			return;
		}
		
		var lightBoxWidth=(width < minWidth ? minWidth : width);
		var lightBoxHeight=height+(galleryItem == 1 ? 70 : 30);

		var widthToHeight=height/width;

		factorWidth=lightBoxSpeed;
		factorHeight=Math.floor(lightBoxSpeed*widthToHeight);

		var scrollTop=window.pageYOffset || document.documentElement.scrollTop || 0;

		LightBoxTop=(scrollTop < 30 ? 20 : scrollTop+10);
		
		LightBoxHead.innerHTML=(galleryItem == 1 ? 'Gallerieansicht':'Gro&szlig;ansicht');

		LightBox.style.top=LightBoxTop+"px";
		LightBox.style.display="block";
		LightBoxNavi.style.display=(galleryItem == 1 ? "block" : "none");

		Img.src=src;
		Img.style.width=width;
		Img.style.height=height;
		Img.alt=alt;

		LightBoxContent.style.width=lightBoxWidth+"px";
		LightBoxContent.style.height=lightBoxHeight+"px";

		if(galleryItem == 1) {

			var numGalleryItems=gal.length;
			var lastGalleryItem=numGalleryItems-1;

			var GalleryButtons='';
			if(galleryItemId != 0) GalleryButtons+='<img src="'+IMGURL+'gallery_back.png" width="41" height="25" alt="zur&uuml;ck" style="cursor: pointer" onclick="openGalleryItem(\''+(galleryItemId-1)+'\')" />';
			GalleryButtons+='<span style="margin-right: 15%; margin-left: 15%">Bild '+(galleryItemId+1)+' von '+numGalleryItems+'</span>';
			if(galleryItemId != lastGalleryItem) GalleryButtons+='<img src="'+IMGURL+'gallery_forward.png" width="41" height="25" alt="vor" style="cursor: pointer" onclick="openGalleryItem(\''+(galleryItemId+1)+'\')" />';

			LightBoxNavi.innerHTML=GalleryButtons;
		}

		LightBoxInAction=1;
		lbSlide(lightBoxWidth, lightBoxHeight);

	}
	
	function closeLightBox() {

		LightBoxInAction=1;
		
		aimWidth=0;
		aimHeight=0;
		
		lbSlide(0, 0);

	}
