﻿function _lightbox(parent, tag, group) {
    this.parent = parent;
    this.tag = tag;
    this.group = group;
    this.srcArray = new Array();

    if (!this.parent || this.parent == "*") { this.parent = document; }
    if (!this.tag) { this.tag = "*"; }
    var arrImages = getElementsByAttribute(this.parent, this.tag, "lbGroupName", this.group);
    for (qq = 0; qq < arrImages.length; qq++) {
        if (!arrImages[qq].getAttribute("fullSrc")) {
            this.srcArray.push(arrImages[qq].getAttribute("fullSrc"));
        } else {
            this.srcArray.push(arrImages[qq].src);
        }
        var anchor = document.createElement("a");
        anchor.href = arrImages[qq].src;
        anchor.target = "_blank";
        anchor.onclick = function(e) { _lightboxClick(e); return false; };
        //AttachEvent(anchor, "click", _lightboxClick, false);
        arrImages[qq].parentNode.appendChild(anchor);
        arrImages[qq].parentNode.removeChild(arrImages[qq]);
        arrImages[qq].className = "lightboxImage";
        anchor.appendChild(arrImages[qq]);
    }
    _prefetchImages(this.srcArray);
    window.lightboxSources = this.srcArray;
}

function _lightboxClick(e) { // Uses dgebi(id) from vanilla.js
    if (!e) { e = window.event; };
    if (!e.target) { elm = e.srcElement; } else { elm = e.target; }
    
    var overlay = _lightboxOverlay("create");
    _lightboxDialog("create", document.body, true, true);
    var strSrc = elm.src.replace(/height=[0-9]+&?/, "");
    strSrc = strSrc.replace(/width=[0-9]+&?/, "");
    strSrc = strSrc.replace(/isThmb=[0-9]+&?/, "");

    var img = document.createElement("img");
    img.src = strSrc;
    //alert(elm.src +  "\n" + strSrc);
    //dgebi("lightbox_maincontents").innerHTML = "";
    dgebi("lightbox_body").appendChild(img);
    _lightboxSetSize(600, 400);
}

function _lightboxOverlay(action) {
    fnDebug("_lightboxOverlay(" + action + ") entered.");
    switch (action) {
        case "create":
            fnDebug("Case create entered.");
            var overlay = document.createElement("div");
            overlay.id = "appOverlay";
            document.body.appendChild(overlay);
            
            return overlay;
            break;
        case "kill":
            fnDebug("Case kill entered.");
            var overlay = document.getElementById("appOverlay");
            document.body.removeChild(overlay);
            break;
    }
}

function _lightboxDialog(action, parent, statusbar, closebutton) { // Uses dgebi(id) from vanilla.js
    fnDebug("fnOverlay(" + action + ", " + parent + ") entered.");
    if (!parent || parent == "*") { parent = document.body; }
    
    switch (action) {
        case "create":
            fnDebug("Case create entered.");
            var dialog = document.createElement("div");
            dialog.id = "appDialog";

            var part = document.createElement("div");
            part.id = "lightbox_topLeft";
            dialog.appendChild(part);
            part = document.createElement("div");
            part.id = "lightbox_topRight";
            dialog.appendChild(part);
            part = document.createElement("div");
            part.id = "lightbox_bottomLeft";
            dialog.appendChild(part);
            part = document.createElement("div");
            part.id = "lightbox_bottomRight";
            dialog.appendChild(part);

            part = document.createElement("div");
            part.id = "lightbox_titlebar";
            dialog.appendChild(part);
            var buttonholder = document.createElement("div");
            buttonholder.id = "lightbox_winbuttons";
            dialog.appendChild(buttonholder);

            if (closebutton) {
                part = document.createElement("div");
                part.href = "#";
                part.id = "lightbox_closebutton";
                part.className = "lightbox_closebutton";
                window.lightboxCloseClick = function() { return _lightboxDialog("kill"); };
                window.lightboxCloseMousedown = function() { dgebi("lightbox_closebutton").className = "lightbox_closebutton_pressed"; };
                window.lightboxCloseMouseover = function() { fnDebug("over"); dgebi("lightbox_closebutton").className = "lightbox_closebutton_hover"; };
                window.lightboxCloseMouseout = function() { dgebi("lightbox_closebutton").className = "lightbox_closebutton"; };
                AttachEvent(part, "click", window.lightboxCloseClick, false);
                AttachEvent(part, "mouseover", window.lightboxCloseMouseover, false);
                AttachEvent(part, "mousedown", window.lightboxCloseMousedown, false);
                AttachEvent(part, "mouseout", window.lightboxCloseMouseout, false);
                buttonholder.appendChild(part);
            }

            var holder = document.createElement("div");
            holder.id = "lightbox_mainholder";
            dialog.appendChild(holder);
            var contents = document.createElement("div");
            contents.id = "lightbox_maincontents";
            contents.innerHTML = _lipsum;
            holder.appendChild(contents);

            part = document.createElement("div");
            part.id = "lightbox_body";
            contents.appendChild(part);
            
            if (statusbar) {
                part = document.createElement("div");
                part.id = "lightbox_statusbar";
                holder.appendChild(part);
            }

            window.lightbox_bodyScroll = document.body.scroll;
            window.lightbox_bodyOverflow = GetStyle(document.body, "overflow");
            document.body.style.overflow = "hidden";
            document.body.scroll = "no";

            parent.appendChild(dialog);

            //_lightboxSetSize();
            break;
        case "kill":
            fnDebug("Case kill entered.");
            document.body.removeChild(document.getElementById("appDialog"));
            document.body.removeChild(document.getElementById("appOverlay"));

            document.body.scroll = window.lightbox_bodyScroll;
            document.body.style.overflow = window.lightbox_bodyOverflow;

            window.lightbox_bodyScroll = null;
            window.lightbox_bodyOverflow = null;
            window.lightboxSources = null;
            window.lightboxCloseClick = null;
            window.lightboxCloseMousedown = null;
            window.lightboxCloseMouseout = null;
            break;
    }
    return false;
}

function _lightboxSetSize(setW, setH) { //uses GetWidth(), GetHeight() and dgebi(id) from vanilla.js
    var ww = GetWidth();
    var wh = GetHeight();
    
    var dialog = dgebi("appDialog");
    var mainholder = dgebi("lightbox_mainholder");
    var maincontents = dgebi("lightbox_maincontents");
    var dWidth = mainholder.offsetWidth;
    var dHeight = mainholder.offsetHeight;
    var pw, ph, hMargin, vMargin;
    
    hMargin = 100;
    vMargin = 100;

    if (!setH) { setH = dHeight; }
    if (!setW) { setW = dWidth; }
    if (setW > ww - hMargin) { setW = ww - hMargin; }
    if (setH > wh - vMargin) { setH = wh - vMargin; }
    pw = setW / 100;
    ph = setH / 100;
    
    /*
    dHeight += (GetStyleFix(mainholder, "margin-top")[0] * 1);
    dHeight += (GetStyleFix(mainholder, "margin-bottom")[0] * 1);
    dWidth += (GetStyleFix(mainholder, "margin-left")[0] * 1);
    dWidth += (GetStyleFix(mainholder, "margin-right")[0] * 1);
    */
    
    dgebi("appOverlay").style.height = wh + "px";
    dgebi("lightbox_topLeft").style.height = (ph * 55) + 36 + "px";
    dgebi("lightbox_topLeft").style.width = (pw * 55) + "px";
    dgebi("lightbox_topRight").style.height = (ph * 55) + 36 + "px";
    dgebi("lightbox_topRight").style.width = (pw * 55) + "px";
    dgebi("lightbox_bottomLeft").style.height = (ph * 55) + "px";
    dgebi("lightbox_bottomLeft").style.width = (pw * 55) + "px";
    dgebi("lightbox_bottomRight").style.height = (ph * 55) + "px";
    dgebi("lightbox_bottomRight").style.width = (pw * 55) + "px";
    
    dgebi("lightbox_titlebar").style.width = setW - 100 + "px";
    dgebi("lightbox_winbuttons").style.width = setW - 80 + "px";
    if (dgebi("lightbox_statusbar")) {
        dgebi("lightbox_statusbar").style.width = setW + "px";
        setH = setH - dgebi("lightbox_statusbar").offsetHeight;
    }
    
    mainholder.style.height = setH + "px";
    mainholder.style.width = setW + "px";
    maincontents.style.width = setW - (GetStyleFix(mainholder, "padding-right")[0] * 1) + "px";
    maincontents.style.height = setH - dgebi("lightbox_statusbar").offsetHeight + "px";
    
    dialog.style.left = (ww / 2) - (dialog.offsetWidth / 2) + "px";
    dialog.style.top = (wh / 2) - (dialog.offsetHeight / 2) + "px";
}
