    ////////////////////////////////////////////////////
    // Scriptinfo:
    // ===========
    // Author: Julian Mollik
    // Email: jule@creative-coding.net
    // Website: http://www.creative-coding.net/
    // Created: 02/2009
    //
    // based on Lightbox   - http://huddletogether.com/projects/lightbox/
    //      and Lightbox 2 - http://huddletogether.com/projects/lightbox2/
    // modified to fit PIXHUNTER's needs
    //
    // Copyright 2009 Julian Mollik
    // Illegal distribution prohibited
    ////////////////////////////////////////////////////

// configuration
var loadingImage       = 'graphic/admin/loading3.gif';		
var closeButton        = 'graphic/admin/popup_close.gif';
var navigationNext     = 'graphic/admin/popup_navigation_next.gif';
var navigationPrevious = 'graphic/admin/popup_navigation_previous.gif';

var aImages = new Array; // [][0] -> link
                         // [][1] -> caption
                         // [][2] -> replypixhunter or replyphotographer id (if available)
var iActiveImage;

// returns array with x,y page scroll values.
// core code from - quirksmode.org
function getPageScroll() {

    var yScroll;

    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    }
    else if (document.body) { // all other Explorers
        yScroll = document.body.scrollTop;
    }

    arrayPageScroll = new Array('',yScroll) 
    return arrayPageScroll;
}

// returns array with page width, height and window width, height
// core code from - quirksmode.org
// edit for Firefox by pHaez
function getPageSize() {
	
    var xScroll, yScroll;
	
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight) {
        pageHeight = windowHeight;
    }
    else { 
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {	
        pageWidth = windowWidth;
    }
    else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}

// pauses code execution for specified time. Uses busy code, not good.
// help from Ran Bar-On [ran2103@gmail.com]
function pause(ms){
    var date = new Date();
    curDate = null;
    do {
        var curDate = new Date();
    }
    while (curDate - date < ms);
}

// gets keycode. If 'x' is pressed then it hides the lightbox.
function getKey(e){
    if (e == null) { // ie
        keycode = event.keyCode;
    }
    else { // mozilla
        keycode = e.which;
    }
    key = String.fromCharCode(keycode).toLowerCase();

    if (key == 'x') {
        hideLightbox();
    }
}

function listenKey() {
    document.onkeypress = getKey;
}

Array.prototype.removeDuplicates = function () {
    for(i = 0; i < this.length; i++){
        for(j = this.length-1; j>i; j--){        
            if(this[i][0] == this[j][0]){
                this.splice(j,1);
            }
        }
    }
}

function showSelectBoxes(){
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "visible";
    }
}

function hideSelectBoxes(){
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }
}

function showFlash(){
    var flashObjects = document.getElementsByTagName("object");
    for (i = 0; i < flashObjects.length; i++) {
        flashObjects[i].style.visibility = "visible";
    }

    var flashEmbeds = document.getElementsByTagName("embed");
    for (i = 0; i < flashEmbeds.length; i++) {
        flashEmbeds[i].style.visibility = "visible";
    }
}

function hideFlash(){
    var flashObjects = document.getElementsByTagName("object");
    for (i = 0; i < flashObjects.length; i++) {
        flashObjects[i].style.visibility = "hidden";
    }

    var flashEmbeds = document.getElementsByTagName("embed");
    for (i = 0; i < flashEmbeds.length; i++) {
        flashEmbeds[i].style.visibility = "hidden";
    }
}

//	preload previous and next images.
function preloadNeighborImages() {
    if ((aImages.length - 1) > iActiveImage){
        preloadNextImage = new Image();
        preloadNextImage.src = aImages[iActiveImage + 1][0];
    }
    if (iActiveImage > 0) {
        preloadPrevImage = new Image();
        preloadPrevImage.src = aImages[iActiveImage - 1][0];
    }
}

// update navigation
function getNavigation2() {
    var sReturn = '';
    
    if (aImages.length > 1) {
        sReturn = sReturn + 'Bild ' + eval(iActiveImage + 1) + ' von ' + aImages.length + '<br />';
    }

	// if not first image in set, display prev image button
	if (iActiveImage != 0) {
	    sReturn = sReturn + '<a href="javascript:void(0);" onClick="changeLightboxImage(' + eval(iActiveImage - 1) + '); return false;" title="Vorheriges Bild">zur&uuml;ck</a>';
	}

	if (iActiveImage != 0 && iActiveImage != (aImages.length - 1)) {
	    sReturn = sReturn + ' oder ';
	}
	
	// if not last image in set, display next image button
	if (iActiveImage != (aImages.length - 1)) {
	    sReturn = sReturn + '<a href="javascript:void(0);" onClick="changeLightboxImage(' + eval(iActiveImage + 1) + '); return false;" title="N&auml;chstes Bild">weiter</a>';
	}
    
    return sReturn;
}
function getNavigation() {
    var sReturn = '';
    
    // if not first image in set, display prev image button
	if (iActiveImage != 0) {
	    sReturn = sReturn + '<a href="javascript:void(0);" onClick="changeLightboxImage(' + eval(iActiveImage - 1) + '); return false;" title="Vorheriges Bild"><img src="' + navigationPrevious + '" alt="zur&uuml;ck" /></a> ';
	}
    
    if (aImages.length > 1) {
        sReturn = sReturn + 'Bild ' + eval(iActiveImage + 1) + ' von ' + aImages.length + ' ';
    }

	// if not last image in set, display next image button
	if (iActiveImage != (aImages.length - 1)) {
	    sReturn = sReturn + '<a href="javascript:void(0);" onClick="changeLightboxImage(' + eval(iActiveImage + 1) + '); return false;" title="N&auml;chstes Bild"><img src="' + navigationNext + '" alt="weiter" /></a>';
	}
    
    return sReturn;
}

// changes the content of the lightbox
function changeLightboxImage(iImage) {
    
//  alert('iImage = ' + iImage);
    
    // update global variable
    iActiveImage = iImage;
    
    // prepare objects
    var oLoadingImage = document.getElementById('loadingImage');
    var oOverlay      = document.getElementById('overlay');
    var oCaption      = document.getElementById('lightboxCaption');
    var oNavigation   = document.getElementById('lightboxNavigation');
    var oLightbox     = document.getElementById('lightbox');
    var oImage        = document.getElementById('lightboxImage');
    var oLightboxDetails  = document.getElementById('lightboxDetails');
    var oLightboxDownload = document.getElementById('lightboxDownload');
    
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();
    
    // set height of Overlay to take up whole page and show
	oOverlay.style.height = (arrayPageSize[1] + 'px');
	oOverlay.style.display = 'block';
    
    // center loadingImage if it exists
    if (oLoadingImage) {
        oLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - oLoadingImage.height) / 2) + 'px');
        oLoadingImage.style.left = (((arrayPageSize[0] - 20 - oLoadingImage.width) / 2) + 'px');
        oLoadingImage.style.display = 'block';
    }
    
    // preload image
	imgPreload = new Image();

	imgPreload.onload = function() {
	    
		oImage.src = aImages[iImage][0];
		
    	// center lightbox and make sure that the top and left values are not negative
        // and the image placed outside the viewport
        var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
        var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
        
        oLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
        oLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
        
        oLightboxDetails.style.width = imgPreload.width + 'px';
        oCaption.style.width = (imgPreload.width - 150) + 'px';
        
        if (aImages.length > 1) {
            // this image is part of a set
            
            // preload neighbor images
            preloadNeighborImages();
            
            // show navigation and add previous and next link
            oNavigation.style.display = 'block';
            oNavigation.innerHTML = getNavigation();
        }
        else {
            // this is a single image
            
            // hide navigation
            oNavigation.style.display = 'none';
        }
        
        if (aImages[iImage][1] != '') {
            // this image has a caption (should always be the case)
            
            oCaption.style.display = 'block';
            oCaption.innerHTML = aImages[iImage][1];
        }
        else {
            oCaption.style.display = 'none';
        }
        
        if (aImages[iImage][2] != 0) {
            // this image has a reply id
            
            oLightboxDownload.style.display = 'block';
            oLightboxDownload.innerHTML = '<input type="button" class="download" value="download" onclick="window.location=\'graphic/imagedownload.php?' + aImages[iImage][2] + '\'" />';
        }
        else {
            oLightboxDownload.style.display = 'none';
        }
        
        // a small pause between the image loading and displaying is required with IE,
        // this prevents the previous image displaying for a short burst causing flicker.
        if (navigator.appVersion.indexOf("MSIE") != -1) {
        	pause(250);
        }
        
        // hide loading image
        if (oLoadingImage) {
            oLoadingImage.style.display = 'none';
        }
        
        // display lightbox
        oLightbox.style.display = 'block';
        
        // after image is loaded, update the overlay height as the new image might have
        // increased the overall page height.
        arrayPageSize = getPageSize();
        oOverlay.style.height = (arrayPageSize[1] + 'px');
    		
    	// check for 'x' keypress
        listenKey();
    
        return false;
	}
	
	imgPreload.src = aImages[iImage][0];
}

// debug
function printImageArray() {
    var sImageArray = '';
    for (i = 0; i < aImages.length; i++) {
        sImageArray = sImageArray + '[' + i + '][0] -> ' + aImages[i][0] + "\n[" + i + '][1] -> ' + aImages[i][1] + "\n[" + i + '][2] -> ' + aImages[i][2] + "\n";
    }
    alert(sImageArray);
}

function getLinkAndReplyID(sHref) {
    
    var aReturn = [sHref, 0];
    var iPosAmp = sHref.indexOf('&');
    
    if (iPosAmp == -1) {
        // no "&" found meaning that no reply id is in the given sHref parameter, return [<link>, 0]
        return aReturn;
    }
    else {
        var sLink  = sHref.substring(0, iPosAmp);
        var iReply = sHref.substring(iPosAmp + 1, sHref.length);
        aReturn[0] = sLink;
        aReturn[1] = iReply;
        return aReturn;
    }
}

// hides selects and flash, resets the global variables and pushes (if part of a set)
// all images with the same rel-attribute into the global array. If not part of a set, the
// given link is pushed into the array
function showLightbox(oLink) {
    
    if (!document.getElementsByTagName) {
        return;
    }
    var anchors = document.getElementsByTagName('a');

    // hide select boxes and flash as they will 'peek' through the image in IE
	hideSelectBoxes();
	hideFlash();

	// reset global variables
	aImages = [];
	iImage = 0;
	
	// remember the link of the given oLink object (without the replypixhunter id)
	// this is needed to find the current clicked image (see while loop below)
	var aValuesLink = getLinkAndReplyID(oLink.getAttribute('href'));
		
	if (oLink.getAttribute('rel') == 'lightbox') {
	    // this image is not part of a set
	    var aValuesTemp = getLinkAndReplyID(oLink.getAttribute('href'));
	    aImages.push(new Array(aValuesTemp[0], oLink.getAttribute('title'), aValuesTemp[1]));
	}
	else {
        // this image is part of a set
        for (var i=0; i < anchors.length; i++){
            var oAnchor = anchors[i];
            if (oAnchor.getAttribute('href') && (oAnchor.getAttribute('rel') == oLink.getAttribute('rel'))) {
                var aValuesTemp = getLinkAndReplyID(oAnchor.getAttribute('href'));
                aImages.push(new Array(aValuesTemp[0], oAnchor.getAttribute('title'), aValuesTemp[1]));
            }
        }
        
        // get number of images
        while (aImages[iImage][0] != aValuesLink[0]) {
            iImage++;
        }
    }
    
	// change the actual lightbox image
	changeLightboxImage(iImage);
}

// 
function hideLightbox() {
    
    // get objects
    oOverlay  = document.getElementById('overlay');
    oLightbox = document.getElementById('lightbox');

    // hide lightbox and overlay
    oOverlay.style.display = 'none';
    oLightbox.style.display = 'none';

    // make select boxes and flash visible
    showSelectBoxes();
    showFlash();

    // disable keypress listener
    document.onkeypress = '';
}

// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
function initLightbox() {
    
    if (!document.getElementsByTagName) {
        return;
    }
    var anchors = document.getElementsByTagName("a");

    // loop through all anchor tags
    for (var i=0; i<anchors.length; i++){
        var oAnchor = anchors[i];
        var sRelAttribute = String(oAnchor.getAttribute('rel'));

        if (oAnchor.getAttribute("href") && sRelAttribute.toLowerCase().match('lightbox')) {
            oAnchor.onclick = function () {
                showLightbox(this);
                return false;
            }
        }
    }
    
    // the rest of this code inserts html at the top of the page that looks like this:
    //
    // <div id="overlay">
    //		<a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
    //	</div>
    // <div id="lightbox">
    //		<a href="#" onclick="hideLightbox(); return false;" title="Click anywhere to close image">
    //			<img id="closeButton" />		
    //			<img id="lightboxImage" />
    //		</a>
    //		<div id="lightboxDetails">
    //			<div id="lightboxCaption"></div>
    //			<div id="lightboxNavigation"></div>
    //          <div id="lightboxDownload"></div>
    //		</div>
    // </div>
	
    var oBody = document.getElementsByTagName("body").item(0);
	
    // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
    var oOverlay = document.createElement("div");
    oOverlay.setAttribute('id','overlay');
    oOverlay.onclick = function () {
        hideLightbox();
        return false;
    }
    oOverlay.style.display = 'none';
    oOverlay.style.position = 'absolute';
    oOverlay.style.top = '0';
    oOverlay.style.left = '0';
    oOverlay.style.zIndex = '90';
    oOverlay.style.width = '100%';
    oBody.insertBefore(oOverlay, oBody.firstChild);
	
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    // preload and create loader image
    var imgPreloader = new Image();
	
    // if loader image found, create link to hide lightbox and create loadingimage
    imgPreloader.onload=function(){

        var oLoadingImageLink = document.createElement("a");
        oLoadingImageLink.setAttribute('href','javascript:void(0);');
        oLoadingImageLink.onclick = function () {
            hideLightbox();
            return false;
        }
        oOverlay.appendChild(oLoadingImageLink);
		
        var oLoadingImage = document.createElement("img");
        oLoadingImage.src = loadingImage;
        oLoadingImage.setAttribute('id','loadingImage');
        oLoadingImage.style.position = 'absolute';
        oLoadingImage.style.zIndex = '150';
        oLoadingImageLink.appendChild(oLoadingImage);

        imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

        return false;
    }

    imgPreloader.src = loadingImage;

    // create lightbox div, same note about styles as above
    var oLightbox = document.createElement("div");
    oLightbox.setAttribute('id','lightbox');
    oLightbox.style.display = 'none';
    oLightbox.style.position = 'absolute';
    oLightbox.style.zIndex = '100';	
    oBody.insertBefore(oLightbox, oOverlay.nextSibling);
	
    // create link
    var oLink = document.createElement("a");
    oLink.setAttribute('href','#');
    oLink.setAttribute('title','Zum Schließen klicken oder \'x\' auf der Tastatur');
    oLink.onclick = function () {
        hideLightbox();
        return false;
    }
    oLightbox.appendChild(oLink);

    // preload and create close button image
    var imgPreloadCloseButton = new Image();

    // if close button image found, 
    imgPreloadCloseButton.onload=function(){

        var oCloseButton = document.createElement("img");
        oCloseButton.src = closeButton;
        oCloseButton.setAttribute('id','closeButton');
        oCloseButton.style.position = 'absolute';
        oCloseButton.style.zIndex = '200';
        oLink.appendChild(oCloseButton);

        return false;
	}

    imgPreloadCloseButton.src = closeButton;

    // create image
    var oImage = document.createElement("img");
    oImage.setAttribute('id','lightboxImage');
    oLink.appendChild(oImage);
	
    // create details div, a container for the caption and navigation
    var oLightboxDetails = document.createElement("div");
    oLightboxDetails.setAttribute('id','lightboxDetails');
    oLightbox.appendChild(oLightboxDetails);

	// create caption
    var oCaption = document.createElement("div");
    oCaption.setAttribute('id','lightboxCaption');
    oCaption.style.display = 'none';
    oLightboxDetails.appendChild(oCaption);

	// create keyboard message
    var oNavigation = document.createElement("div");
    oNavigation.setAttribute('id', 'lightboxNavigation');
    oNavigation.innerHTML = '';
    oLightboxDetails.appendChild(oNavigation);
    
    // create download div, a container for the download link
    var oLightboxDownload = document.createElement("div");
    oLightboxDownload.setAttribute('id','lightboxDownload');
    oLightboxDetails.appendChild(oLightboxDownload);
}

// adds event to window.onload without overwriting currently assigned onload functions.
// function found at Simon Willison's weblog - http://simon.incutio.com/
function addLoadEvent(func) {	
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
	}
}

addLoadEvent(initLightbox);	// run initLightbox onLoad
