var dimx_lbState = 0;	//Animation
var dimx_lbAnimImg, dimx_divLbBg, dimx_divLbMiniNav;
var dimx_lbTrgTop, dimx_lbTrgLeft, dimx_lbDiffTop, dimx_lbDiffLeft;
var dimx_lbDiffWidth, dimx_lbDiffHeight;
var dimx_lbDiv0,dimx_lbImg0;
var dimx_lbOpacityInc = 25;
var dimx_lbOpacityStart = 0;
var dimx_lbOpacity = dimx_lbOpacityStart;
var dimx_lbImgAnimSteps = 5;
var dimx_lbImgAnimCounter = 0;
var dimx_lbUseZoom = false;
var dimx_lbLoadingTimeout = false;
var dimx_lbCurrentImg = false;
var lbStr1 = albumStr1;	//(Ansicht = x %)
var dimx_lb_string_loading = "Loading...";

function dimx_showLbImg(id) {
	if (!dimx_lb_imgs[id] || dimx_lb_imgs[id] == "undefined")
		return;
	dimx_lbDiv0 = document.getElementById("dimx_lb_div0");
	dimx_lbImg0 = document.images["lb_img0"];

	if (!dimx_lbDiv0  || !dimx_lbImg0)
		return;

	dimx_lbImg0.onload = "";

	if (dimx_lbState == 0) {
		window.setTimeout("dimx_lb_animateBg()",20);
	    if (dimx_arrangeLbDivs()) {
	    	dimx_lbState = 1;
            dimx_lbOpacity = dimx_lbOpacityStart;
            window.onresize = dimx_lbResize;
		}
		else {
			dimx_showLbImgClose();
			return;
		}
	}
	dimx_createLbImgView(id);
}

function dimx_lbImgLoaded(resized) {
	resized = resized === 1;
	id = dimx_lbCurrentImg;
	var el = document.getElementById("dimx_lb_textCalc_div");

	if (dimx_lbLoadingTimeout)
		window.clearTimeout(dimx_lbLoadingTimeout);

	if (el && dimx_lb_imgs[id]) {
		el.style.display = "none";
		el.style.top = "0px";
		el.style.left = "0px";
		el.style.width = "400px";
		el.style.overflow = "visible";
		el.style.visibility = "hidden";
		el.style.display = "block";

//		el.innerHTML = dimx_string_image+" 1 "+dimx_string_of+" 1<br>"+dimx_lb_imgs[id][6];
		el.innerHTML = "&nbsp;";
		var txHeight = el.scrollHeight;
		el.innerHTML+= "<br>...";
		var txHeight2 = el.scrollHeight+5;

		var w = dimx_lb_imgs[id][4];
		var h = dimx_lb_imgs[id][5];
		var p = 100;

		var w1 = document.body.clientWidth ? document.body.clientWidth-5 : document.offsetWidth-5;
		var w2 = w1 > 800 ? 800 : w1;

		if (w > w2) {
			h = Math.round(w2*(h/w));
        	p = Math.round(w2/w*100);
        	w = w2;
        	txHeight = txHeight2;
        }

		el.innerHTML = "";
		el.style.display = "none";

		var h1 = window.innerHeight ? window.innerHeight-180-txHeight : document.body.clientHeight-180-txHeight;
		var h2 = h1 > 500 ? 500 : h1;

        if (h > h2) {
        	h1-= (txHeight2-txHeight);
        	h2-= (txHeight2-txHeight);
        	w = Math.round(h2*(w/h));
        	p = Math.round(h2/h*100);
        	h = h2;
        }
		if (p != 100) {
			dimx_lb_imgs[id][1] = w;
			dimx_lb_imgs[id][2] = h;

		}
		dimx_lb_imgs[id][3] = p; //prozent resized

		//--- --- -- --- --- ---

		var imgOld = document.images['lb_img0'];
		var el2Old = document.getElementById("dimx_lb_div0");

		var img = document.images['lb_img0'];
		img.onload = "";

		var hrtx = "";
		var alt = dimx_lb_imgs[id][6];

		if (dimx_lb_imgs[id][3] < 100) {
			var windowTitle = dimx_lb_imgs[id][6];
			var src = "<html><head><title>"+windowTitle+"</title></head><body><img src='"+pageMediaDir+"/"+dimx_lbCurrentImg+"' border=0 title='"+alt+"'></body></html>";
			img.onclick = function() { win=window.open("","_blank","menubar=yes,scrollbars=yes,resizable=yes,width="+(dimx_lb_imgs[id][4]+40)+",height="+(dimx_lb_imgs[id][5]+50));win.document.write(src);win.document.close() };
			img.style.cursor = "pointer";

			hrtx+= "<br>"+lbStr1+p+"%)";	//"Klicken Sie auf das Bild, um es in Originalgr&ouml;&szlig;e anzuzeigen (Ansicht = "")
		}
		else {
			img.onclick = "";
			img.style.cursor = "default";
		}

		img.alt = alt;

//		img.style.width = resized ? w+"px" : imgOld.style.width;// w+"px";
//		img.width = resized ? w : imgOld.width;//w;
//		img.style.height = resized ? h+"px" : imgOld.style.height;//h+"px";
//		img.height = resized ? h : imgOld.height; //h;

		var el2 = dimx_lbDiv0;
		if (el2 && el2Old) {
			var scrTop = document.body.scrollTop;
			var scrLeft = document.body.scrollLeft;

			el2.style.width = resized ? w+"px" : el2Old.style.width; //w+"px";
			el2.style.height = resized ? h+"px" : el2Old.style.height; //h+"px";

			dimx_lbDiffWidth = w-parseInt(el2Old.style.width);
			dimx_lbDiffHeight = h-parseInt(el2Old.style.height);

			dimx_lbTrgTop = ((h1-h)/2+scrTop+5);
			dimx_lbTrgLeft = ((w1-w)/2+scrLeft);//+5);
			dimx_lbDiffTop = dimx_lbTrgTop-parseInt(el2Old.style.top);
			dimx_lbDiffLeft = dimx_lbTrgLeft-parseInt(el2Old.style.left);

			el2.style.top = resized ? dimx_lbTrgTop+"px" : el2Old.style.top;
			el2.style.left = resized ? dimx_lbTrgLeft+"px" : el2Old.style.left;

			if (!dimx_lbUseZoom) {
				el2.style.width = w+"px";
				el2.style.height = h+"px";
				el2.style.top = dimx_lbTrgTop+"px";
				el2.style.left = dimx_lbTrgLeft+"px";
				dimx_lbDiffWidth = 0;
				dimx_lbDiffHeight = 0;
				dimx_lbDiffTop = 0;
				dimx_lbDiffLeft = 0;
			}

			if (!resized) {
			    if (el2.style.opacity)
                    el2.style.opacity = 0;
                else
                    el2.filters[0].opacity = 0;

                el2.style.display = "block";
                dimx_lbImgAnimCounter = 0;
                var dimx_tTime = document.all ? 20 : 50;
                dimx_lbAnimImg = window.setInterval("dimx_lbImgTransition()",dimx_tTime);
            }
		}

		var hr = document.getElementById("dimx_lb_headRow");
		var cp = document.getElementById("dimx_lb_caption");
		if (hr && cp) {
			hr.innerHTML = "<nobr>"+hrtx+"<br></nobr>";
			hr.style.visibility = "hidden";
			hr.style.display = "block";
			hr.style.top = dimx_lbTrgTop+h+5+"px"; //(parseInt(el2.style.top)+h+5)+"px";
			hr.style.left = ((w1-hr.scrollWidth)/2+scrLeft)+"px";
			if (resized)
			hr.style.visibility = resized ? "visible" : "hidden";

			var capEl = document.getElementById("dimx_lb_desc_"+id);

			cp.innerHTML = capEl ? capEl.innerHTML : ""; //dimx_lb_imgs[id][6];	//caption
			cp.style.visibility = "hidden";
			cp.style.display = "block";
			cp.style.top = (parseInt(hr.style.top)+hr.scrollHeight+10)+"px";
			cp.style.left = w < 300 ? ((w1-300)/2+scrLeft)+"px" : dimx_lbTrgLeft;//el2.style.left;
			cp.style.width = w < 300 ? "300px" : w+"px";
			if (resized)
				cp.style.visibility = "visible";
		}
	}
}

function dimx_showLbLoadingMsg() {
	var hr = document.getElementById("dimx_lb_headRow");
	if (hr) {
		var cw = document.body.clientWidth ? document.body.clientWidth-10 : document.offsetWidth-10;
		var ch = document.body.clientHeight ? document.body.clientHeight-10 : document.offsetHeight-10;
		hr.innerHTML = dimx_lb_string_loading;
		hr.style.display = "block";
		hr.style.top = (ch/2+document.body.scrollTop)+"px";
		hr.style.left = ((cw-hr.scrollWidth)/2+document.body.scrollLeft)+"px";
		hr.style.visibility = "visible";
	}
	dimx_lbLoadingTimeout = false;
}

function dimx_createLbImgView(id) {
	var img = document.images['lb_img0'];
//	img.src = "gfx/clearpixel.gif";
//	img.width = 1;
//	img.height = 1;
//	w = dimx_lb_imgs[id][4];
//	h = dimx_lb_imgs[id][5];
//	var w1 = w;
//	var h1 = h;

	dimx_lbCurrentImg = id;

	dimx_lbLoadingTimeout = window.setTimeout("dimx_showLbLoadingMsg()",500);

	img.onload = dimx_lbImgLoaded;
	img.src = pageMediaDir+"/"+id;
}

function dimx_lbResize() {
	if (dimx_lbState != 0 && dimx_lbCurrentImg != "") {
		dimx_arrangeLbDivs();
		dimx_lbImgLoaded(1);
	}
}
function dimx_arrangeLbDivs() {
	var el = document.getElementById("dimx_lb_blocker");
	var el3 = document.getElementById("dimx_lb_miniNav");

	var w1,h1;
	if (el && el3) {
		dimx_divLbBg = el;
		dimx_divLbMiniNav = el3;

		document.body.style.overflow = "hidden";
		if (document.all)
			document.body.style.overflowY = "hidden";

		var scrTop = document.body.scrollTop;
		var scrLeft = document.body.scrollLeft;

		w1 = document.body.clientWidth ? document.body.clientWidth : document.offsetWidth;
		h1 = document.body.clientHeight ? document.body.clientHeight : document.offsetHeight;
		if (document.body.scrollWidth > w1 || document.body.scrollHeight > h1) {
			el.style.width = document.body.scrollWidth+"px";
			el.style.height = document.body.scrollHeight+"px";
			el.style.top = "0px";
			el.style.left = "0px";
		}
		else {
			el.style.width = document.all ? w1+"px" : "100%";
			el.style.height = document.all ? h1+"px" : "100%";
			el.style.top = scrTop+"px";
			el.style.left = scrLeft+"px";
		}
		el.style.display = "block";

		document.body.onscroll = function() {document.body.scrollTop = scrTop;document.body.scrollLeft = scrLeft;return false;};
		
		el3.style.top = (scrTop+h1-125)+"px";
		el3.style.left = (w1/2-340+scrLeft)+"px";
		el3.style.display = "block";
		
		return true;
	}
	return false;
}
function dimx_showLbImgClose() {
	if (dimx_lbImg0) {
		dimx_lbImg0.onload = "";
		dimx_lbImg0.src = "gfx/clearpixel.gif";
	}
	
	if (dimx_lbAnimImg)
		window.clearInterval(dimx_lbAnimImg);

	if (dimx_lbLoadingTimeout)
		window.clearTimeout(dimx_lbLoadingTimeout);

	var el = document.getElementById("dimx_lb_blocker");
	var el6 = document.getElementById("dimx_lb_div0");
	var el4 = document.getElementById("dimx_lb_headRow");
	var el3 = document.getElementById("dimx_lb_miniNav");
	var el5 = document.getElementById("dimx_lb_caption");
	if (el && el4 && el6) {
		el.style.display = "none";
		el6.style.display = "none";
		el4.style.display = "none";
		el5.style.display = "none";
		el3.style.display = "none";
		el.style.left = "-9999px";
		el6.style.left = "-9999px";
		el6.style.top = "-9999px";

		if (el.style.opacity) {
			el.style.opacity = 0.2;
			el3.style.opacity = 0.2;
			el6.style.opacity = dimx_lbOpacityStart/100;//0.50;
		}
		else {
			el.filters[0].opacity = 20;
			el3.filters[0].opacity = 20;
			el6.filters[0].opacity = dimx_lbOpacityStart;
		}

		dimx_lbState = 0;

		window.onresize = "";
		document.body.style.overflow = "auto";
		if (document.all)
			document.body.style.overflowY = "scroll";
//		if (navigator.appName == "Opera") {
			document.body.onscroll = "";
//		}
	}
}

function dimx_lb_animateBg() {
//	var el = document.getElementById("dimx_lb_blocker");
//	var el3 = document.getElementById("dimx_lb_miniNav");
	var op = 0;
	if (dimx_divLbBg.style.opacity) 
		op = dimx_divLbBg.style.opacity * 100
	else
		op = dimx_divLbBg.filters[0].opacity;
		
	if (op < 80) {
		op+=20;		//20 start
		if (dimx_divLbBg.style.opacity) {
			dimx_divLbBg.style.opacity = op/100;
			dimx_divLbMiniNav.style.opacity = op/100;
		}
		else {
			dimx_divLbBg.filters[0].opacity = op;
			dimx_divLbMiniNav.filters[0].opacity = op;
		}

		window.setTimeout("dimx_lb_animateBg()",20);
	}
	else {
		if (dimx_divLbMiniNav.style.opacity)
			dimx_divLbMiniNav.style.opacity = 1.0;
		else
			dimx_divLbMiniNav.filters[0].opacity = 100;
	}
}
function dimx_lbImgTransition() {
	var el = dimx_lbDiv0;
	var img = dimx_lbImg0;

	var op = dimx_lbOpacity;
	if (el) {
		el.style.zIndex = 1002;
   //     if (el.style.opacity)
   //         op = el.style.opacity * 100
   //     else
   //         op = el.filters[0].opacity;

		var inProgress = false;
        if (op < 100) {
            op+=dimx_lbOpacityInc;     //25 start
            op = op > 100 ? 100 : op;
            dimx_lbOpacity = op;

            if (el.style.opacity) {
                el.style.opacity = op/100;
       //         el2.style.opacity = 1-(op/100);
            }
            else {
                el.filters[0].opacity = op;
       //         el2.filters[0].opacity = 100-op;
            }
			if (dimx_lbImgAnimCounter < dimx_lbImgAnimSteps) {
				dimx_lbImgAnimCounter++;
                var pp = 0;

                if (dimx_lbDiffTop != 0) {
                    pp = parseInt(el.style.top)+dimx_lbDiffTop/dimx_lbImgAnimSteps;
                    if (pp >= 0) {
                        el.style.top = pp+"px";
                    }
                }
                if (dimx_lbDiffLeft != 0) {
                    pp = parseInt(el.style.left)+dimx_lbDiffLeft/dimx_lbImgAnimSteps;
                    if (pp >= 0) {
                        el.style.left = pp+"px";
                    }
                }

                if (dimx_lbDiffHeight != 0) {
                    pp = parseInt(el.style.height)+dimx_lbDiffHeight/dimx_lbImgAnimSteps;
                    if (pp >= 0) {
                        el.style.height = pp+"px";
                //      img.height = pp;
                    }
                }

                if (dimx_lbDiffWidth != 0) {
                    pp = parseInt(el.style.width)+dimx_lbDiffWidth/dimx_lbImgAnimSteps;
                    if (pp >= 0) {
                        el.style.width = pp+"px";
                //      img.width = pp;
                    }
                }
            	inProgress = pp >= 0;
            }
		}
     //       window.setTimeout("dimx_lbImgTransition()",1);

        if(!inProgress) {
        	clearInterval(dimx_lbAnimImg);
        	dimx_lbImgAnimCounter = 0;
        	dimx_lbAnimImg = false;
        	el.style.top = dimx_lbTrgTop+"px";
        	el.style.left = dimx_lbTrgLeft+"px";
        	el.style.width = dimx_lb_imgs[dimx_lbCurrentImg][1]+"px"; //el.style.width;
        	el.style.height = dimx_lb_imgs[dimx_lbCurrentImg][2]+"px"; //el.style.height;
       // 	img.width = parseInt(img.style.width);
       // 	img.height = parseInt(img.style.height);

        	if (el.style.opacity) {
                el.style.opacity = 1.0;
            }
            else {
                el.filters[0].opacity = 100;
            }
            dimx_lbOpacity = dimx_lbOpacityStart;
        	el = document.getElementById("dimx_lb_headRow");
        	el2 = document.getElementById("dimx_lb_caption");
        	if (el && el2) {
        		el.style.visibility = "visible";
        		el2.style.visibility = "visible";
        	}
        }
    }
}
