/* ==================================================================================================== */
/* Defaulteinstellungen 
 * können auf der jeweiligen Seite überschrieben werden, wenn die Variable
 * gesetzt wird, bevor das Script eingebunden wird
 */

var lightboxOverlayColor = lightboxOverlayColor ? lightboxOverlayColor : '000000';
var lightboxOverlayAlpha = lightboxOverlayAlpha ? lightboxOverlayAlpha : 25;
var lightboxTopMargin = lightboxTopMargin ? lightboxTopMargin : 20;


/* ==================================================================================================== */
/* User-Funktionen * /

/**
  * öffnet die Lightbox
  * wird normalerweise vom Script automatisch an alle URLs angehängt die das Attribut  rel="fid_lightbox"  haben.
  * kann aber auch mannuell gerufen werden (z.B. aus einem Flash heraus)
  */
function openLightbox (url) {
	if (LightboxIsCurrentlyOpen) {
		closeLightbox();
	}
	new Ajax.Updater('lightboxContent', url, {method: 'get', evalScripts: true,
        onComplete: function(){
            lightboxContentGeladen();
        }
	});
}

/**
  * Schließt die Lightbox
  */
function closeLightbox () {
	var bg = $('lightboxBG');
	var lb = $('lightbox');
	var lbc = $('lightboxContent');
	lb.hide();
	bg.hide();
	lbc.style.position = 'absolute';
	lbc.style.width = 'auto';
	lbc.innerHTML = '';
	LightboxIsCurrentlyOpen = false;
}


/* ==================================================================================================== */
/* ==================================================================================================== */




var LightboxIsCurrentlyOpen = false;

function initLightBox () {
	appendLightbox();
	document.observe('click', function(e) {
		var target = e.findElement('a[rel^=fid_lightbox]') || e.findElement('area[rel^=fid_lightbox]');
		if (target) {
			Event.stop(e);
			e.stop();	
			openLightbox(target.href);
		}
	});
}

function lightboxContentGeladen () {
	LightboxIsCurrentlyOpen = true;
	var bg = $('lightboxBG');
	var lb = $('lightbox');
	lb.show();
	bg.show();
	setLightboxSize();
}

function setLightboxSize () {
	var bg = $('lightboxBG');
	var lb = $('lightbox');
	var lbc = $('lightboxContent');
	var fidFooter = $('footer');
	var s_offset=document.body.cumulativeScrollOffset();
	bg.style.height = '100%';
	var h = Math.max(Math.max(document.viewport.getHeight(), $(document.body).getHeight()), $(bg).getHeight());
	if (fidFooter) {
		h = Math.max(h, (fidFooter.cumulativeOffset()[1]+fidFooter.getHeight()));
	}
	lbc.hide();
	lbc.style.width = lbc.getWidth()+'px';
	lbc.style.position = 'relative';
	
	if (lightboxCheckQuirks()) {
		if (lightboxCheckIE()) {
			var scrollOffset = lb.cumulativeScrollOffset()[1];
			var w = Math.max(document.viewport.getWidth(), document.body.getWidth());
			lbc.style.left = ((w - lbc.getWidth())/2) + 'px';
			lb.style.top = (lightboxTopMargin + scrollOffset) + 'px';
		}
	}
	bg.style.height = h+"px";
	lbc.show();
}

function lightboxCheckIE () {
	if (document.all && !window.XMLHttpRequest) {
		return true;	// IE6
	} else if (document.all && !window.opera) {
		return true;	// IE7
	} else {
		return false;
	}
}

function lightboxCheckQuirks () {
	return (document.compatMode && document.compatMode=='BackCompat') ? true : false;
}

function ieUpdateLightboxPos (e) {
	var lb = $('lightbox');
	var scrollOffset = lb.cumulativeScrollOffset()[1];
	lb.style.top = (lightboxTopMargin + scrollOffset) + 'px';
}
	
/* hängt den Lightbox-Container im Body an */
function appendLightbox () {
	var code = '<div id="lightboxBG" style="display:none; position:absolute; top:0; left:0; width:100%; z-index:1000; background:#'+lightboxOverlayColor+'; filter:Alpha(opacity='+lightboxOverlayAlpha+'); opacity:'+(lightboxOverlayAlpha/100)+';"></div>';
	if (!window.XMLHttpRequest || (lightboxCheckQuirks() && lightboxCheckIE())) {		
		code += '<div id="lightbox" style="position:absolute; top: expression(eval(document.documentElement.scrollTop + '+lightboxTopMargin+')+\'px\'); width:100%; z-index:1001; background:transparent;">';	
	} else {
		code += '<div id="lightbox" style="position:fixed; top:'+lightboxTopMargin+'px; width:100%; z-index:1001; background:transparent;">';
	}
	code += '<div id="lightboxContent" style="margin:0 auto;"></div></div>';	
	if (document.body) {
		$(document.body).insert(code);
	}

	$('lightboxBG').observe('click', function() {closeLightbox(); });
	$('lightbox').observe('click', function(event) {if (event.element().id == 'lightbox') {closeLightbox();} });
}

/* automatische Initialisierung */
if (document.all) {
	if (!window.XMLHttpRequest) {
		document.write("<style type=\"text\/css\"> html {top: expression((typeof _E=='undefined') &&(document.body) &&(_E=document.body.currentStyle) &&(_dbs=document.body.style) &&((_E.backgroundImage!='none')||(_dbs.background=' '+_E.backgroundColor+' url(\"test.gif\") fixed')) &&((_E.backgroundAttachment=='fixed') ||((_P=_E.backgroundPositionX+' '+_E.backgroundPositionY) &&(this.style.background=' '+'transparent '+_E.backgroundImage+' '+_E.backgroundRepeat+' '+_E.backgroundAttachment+' '+_P)&&(_dbs.backgroundImage='none') &&(_dbs.backgroundColor='transparent') &&(setTimeout(function(){document.documentElement.style.setexpression(\"backgroundPosition\", \"document.body.currentStyle.backgroundPositionX+' '+document.body.currentStyle.backgroundPositionY\")},0)))) &&(setTimeout(function(){eval('document.documentElement.style.top=\"0px\"')},0)));}<\/style>");
	}
	Event.observe(window,'load', function () { initLightBox(); });		// IE
} else {
	document.observe('dom:loaded', function () { initLightBox(); });	// andere Browser
}

if (lightboxCheckQuirks() && lightboxCheckIE()) {
	window.onscroll = ieUpdateLightboxPos;
}
