var lbOverlay = "lb_overlay";
var lbContent = "lb_content";
var lbImage = "lb_image";
var lbHeaderText = "lb_header_text";
var lbImageMaxId = "lightbox_max";

var lbImageIdx = 0;
var lbImageWidth = 400;
var lbImageHeight = 200;
var lbImageTimeout = null, lbAnimationTimeout;
var lbDoAnimation = 1;

function lbHide() {

    if(lbImageTimeout != null) {
        window.clearTimeout(lbImageTimeout);
        lbImageTimeout = null;
    }
    document.getElementById(lbContent).style.display='none';
    document.getElementById(lbOverlay).style.display='none';

}

// Action Event ID's
// 0: update size
// 1: resize div
// 2: shift opacity to visible
// 3: shift opacity to transparent
function lbShowNoAnim(idx, list) {
    
    lbDoAnimation = 0;
    window.clearTimeout(lbAnimationTimeout);
    if(lbAnimationTimeout == lbImageTimeout) lbImageTimeout = null;
    lbAnimationTimeout = null;
    document.getElementById("lb_animation").src = "./images/buttons/play.png";
    
    lbShow(idx, list);
    
}

function lbShow(idx, list) {

    var image = document.getElementById(lbImage);
    image.style.filter = "Alpha(Opacity=0)";
    image.style.MozOpacity = 0;
    image.style.opacity = 0;

    if (REQUEST == null) createRequest();
    var listObj = document.getElementById(list);
    REQUEST.open("GET", "subpages.php?pid=lightbox&images=" + escape(listObj.value), true);
    REQUEST.onreadystatechange = function() {
    switch(REQUEST.readyState) {
        case 4:if(REQUEST.status!=200) {
                    alert("Fehler:"+REQUEST.status);
                }else{

                    image.innerHTML = REQUEST.responseText;
                    document.getElementById(lbOverlay).style.display='block';
                    document.getElementById(lbContent).style.display='block';
                    lbImageIdx = idx;
                    lbChangeImage(0);

                }
                break;
          default:
              return false;
              break;
        }
     };
    REQUEST.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    REQUEST.send(null);

}

function lbAnimation() {

    if(lbDoAnimation == 0) {
        lbDoAnimation = 1;
        lbChangeImage(1);
        document.getElementById("lb_animation").src = "./images/buttons/pause.png";
    } else {
        lbDoAnimation = 0;
        window.clearTimeout(lbAnimationTimeout);
        if(lbAnimationTimeout == lbImageTimeout) lbImageTimeout = null;
        lbAnimationTimeout = null;
        document.getElementById("lb_animation").src = "./images/buttons/play.png";
    }
    
}

function lbChangeImage(direction) {

    if(lbImageTimeout != null) {
        var image = document.getElementById(lbImage);
        image.style.filter = "Alpha(Opacity=0)";
        image.style.MozOpacity = 0;
        image.style.opacity = 0;
        window.clearTimeout(lbImageTimeout);
        lbImageTimeout = null;
    }
    var maxId = document.getElementById(lbImageMaxId).value;

    if (direction != 0) {

        var nid = lbImageIdx + direction;
        if (nid < 0) nid = maxId-1;
        else if (nid >= maxId) nid = 0;
        lbImageIdx = nid;

    }

    var header = document.getElementById(lbHeaderText);
    header.innerHTML = "Bild  " + (lbImageIdx+1) + " von " + maxId;

    lbDoAction(3);

}

function lbDoAction(actionId) {

    if(lbImageTimeout != null) {
        window.clearTimeout(lbImageTimeout);
        lbImageTimeout = null;
        lbAnimationTimeout = lbImageTimeout;
    }
    var tId = -1;

    if (actionId == 0) {
        var xret = lbUpdateSize();
        if (xret == 0) tId = 1;
        else if (xret == -2) tId = -2; else tId = 0;
    } else if (actionId == 1) {
        if (lbResizeDIV() == 0) tId = 2; else tId = 1;
    } else if (actionId == 2) {
        if (lbShiftOpacity(1) == 0) tId = -1; else tId = 2;
    } else if (actionId == 3) {
        if (lbShiftOpacity(-1) == 0) tId = 0; else tId = 3;
    }


    if (tId > -1) lbImageTimeout = window.setTimeout("lbDoAction(" + tId + ")", 20);
    else if (lbDoAnimation == 1) {
        lbImageTimeout = window.setTimeout("lbChangeImage(1)", 6000);
        lbAnimationTimeout = lbImageTimeout;
    }

}

function lbUpdateSize() {

    var image = document.getElementById("lightbox_" + lbImageIdx);
    if (image != null) {

        var value = image.value.split(";");
        document.getElementById(lbImage).style.backgroundImage='url(' + value[2] + ')';
        lbImageWidth = value[0];
        lbImageHeight = value[1];

    }

    return 0;
    
}

function lbResizeWidth() {

    var c = document.getElementById(lbImage);
    var wdir = 1;
    if (c.offsetWidth > lbImageWidth) wdir = -1;
    var width = c.offsetWidth+wdir*8;

    if ((wdir == 1 && width > lbImageWidth) ||
        (wdir == -1 && width < lbImageWidth)) width = lbImageWidth;

    if (width != lbImageWidth) {
        c.style.width = width + "px";
        return -1;
    } else {
        return 0;
    }

}

function lbResizeHeight() {

    var c = document.getElementById(lbImage);
    var wdir = 1;
    if (c.offsetHeight > lbImageHeight) wdir = -1;
    var height = c.offsetHeight+wdir*8;

    if ((wdir == 1 && height > lbImageHeight) ||
        (wdir == -1 && height < lbImageHeight)) height = lbImageHeight;

    if (height != lbImageHeight) {
        c.style.height = height + "px";
        return -1;
    } else {
        return 0;
    }

}

function lbResizeDIV() {


    var widthStat = lbResizeWidth();
    var heightStat = -1;

    if (widthStat == 0) heightStat = lbResizeHeight();

    lbAlignDIV();

    if (widthStat == 0 && heightStat == 0) {
        return 0;
    } else {
        return -1;
    }

}

function lbAlignDIV() {

    var c1 = document.getElementById(lbContent);
    if(c1 != null) {
        
        var cwidth = c1.offsetWidth;
        var cheight = c1.offsetHeight;

        var cc = document.getElementById(lbOverlay);
        if(cc != null) {
            
            var fheight = cc.offsetHeight;
            var fwidth = cc.offsetWidth;

            c1.style.left = (fwidth/2 - cwidth/2) + "px";
            c1.style.top = (fheight/2 - cheight/2) + "px";
            
        }
        
    }

}

function lbShiftOpacity(onoff) {

    var image = document.getElementById(lbImage);
    var nopac = image.style.opacity*100 + onoff*2;
    if (nopac < 0 || nopac > 100) return 0;
    if (nopac < 0) nopac = 0;else if (nopac > 100) nopac = 100;

    image.style.filter = "Alpha(Opacity=" + nopac + ")";
    image.style.MozOpacity = nopac/100;
    image.style.opacity = nopac/100;
    return -1;

}


function lbHideInfo() {

    document.getElementById("lb_content_info").style.display='none';
    document.getElementById("lb_overlay_info").style.display='none';

}

function lbAlignInfoDIV() {

    var c1 = document.getElementById("lb_content_info");
    if(c1 != null) {
        
        var cwidth = c1.offsetWidth;
        var cheight = c1.offsetHeight;

        var cc = document.getElementById("lb_overlay_info");
        
        if(cc != null) {
            
            var fheight = cc.offsetHeight;
            var fwidth = cc.offsetWidth;

            c1.style.left = (fwidth/2 - cwidth/2) + "px";
            c1.style.top = (fheight/2 - cheight/2) + "px";
            
        }
        
    }

}
