var lbOverlay = "lb_overlay";
var lbContent = "lb_content";
var lbImage = "lb_image";
var lbHeaderText = "lb_header_text";
var lbXMLResponse = null;

var lbImageIdx = 0;
var lbImageWidth = 400;
var lbImageHeight = 200;
var lbImageTimeout = null, lbAnimationTimeout;
var lbDoAnimation = 0;
var lbIconResource = "./images/icons/";

function lbHide() {

    if(lbImageTimeout != null) {
        window.clearTimeout(lbImageTimeout);
        lbImageTimeout = null;
    }
    lbDoAnimation = 0;
    document.getElementById("lb_animation").src = lbIconResource + "play.png";
    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 lbShowx(dir, start) {

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

    if (REQUEST == null) createRequest();
    REQUEST.open("GET", "ajax/lightbox.php?dir=" + dir, true);
    REQUEST.onreadystatechange = function() {
    switch(REQUEST.readyState) {
        case 4:if(REQUEST.status!=200) {
                    alert("Fehler:"+REQUEST.status);
                }else{

                    lbXMLResponse = REQUEST.responseXML;
                    document.getElementById(lbOverlay).style.display='block';
                    document.getElementById(lbContent).style.display='block';
                    lbImageIdx = start;
                    lbChangeImage(0);

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

}

function lbShow(dir) {
    lbShowx(dir, 0);
}

function lbAnimation() {

    if(lbDoAnimation == 0) {
        lbDoAnimation = 1;
        lbChangeImage(1);
        document.getElementById("lb_animation").src = lbIconResource + "pause.png";
    } else {
        lbDoAnimation = 0;
        window.clearTimeout(lbAnimationTimeout);
        if(lbAnimationTimeout == lbImageTimeout) lbImageTimeout = null;
        lbAnimationTimeout = null;
        document.getElementById("lb_animation").src = lbIconResource + "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 = 0;
    var images = lbXMLResponse.getElementsByTagName("image");
    maxId = images.length;

    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 + ")", 15);
    else if (lbDoAnimation == 1) {
        lbImageTimeout = window.setTimeout("lbChangeImage(1)", 6000);
        lbAnimationTimeout = lbImageTimeout;
    }

}

function lbUpdateSize() {

    var images = lbXMLResponse.getElementsByTagName("image");
    var image = null;
    for(a = 0; a < images.length; ++a) {
        if(images[a].getAttribute("id") == lbImageIdx) 
            image = images[a];
    }

    if (image != null) {

        document.getElementById(lbImage).style.backgroundImage='url(' + image.getAttribute("src") + ')';
        lbImageWidth = image.getAttribute("width");
        lbImageHeight = image.getAttribute("height");

    }

    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);
    var cwidth = c1.offsetWidth;
    var cheight = c1.offsetHeight;

    var cc = document.getElementById(lbOverlay);
    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;

}
