var imgs;
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;

function find(photo) {
    for (i = 0; i < imgs.length; i++) {
        if (photo == imgs[i])
            return i;
    }
    return -1;
}

function fotopopup(gallery, photo) {
    var position = find(photo);
    //    alert("fotopopup: " + photo + " | position: " + position);
    if (position == -1)
        return;

    var stopBubbling = function(e) {
        if (e != null && e != "undefined") {
            e.stopPropagation();
        }
        window.event.cancelBubble = true;
    };

    var prv = getElement("previous");
    prv.style.visibility = 'hidden';
    prv.href = "javascript:fotopopup('" + gallery + "', '" + imgs[position - 1] + "');";
    prv.onclick = stopBubbling;
    prv.blur();

    var nxt = getElement("next");
    nxt.style.visibility = 'hidden';
    nxt.href = "javascript:fotopopup('" + gallery + "', '" + imgs[position + 1] + "');";
    nxt.onclick = stopBubbling;
    nxt.blur();

    var img = getElement("gallery-photo");
    var show = (img.style.visibility != "visible");
    img.style.visibility = "visible";
    img.style.height = "25px";
    img.height = 25;
    img.src = "static/img/loading.jpg";
    var tempImg = new Image();
    tempImg.onload = function() {
        img.style.visibility = "hidden";
        img.src = tempImg.src;
        var maxImgHeight = document.documentElement.clientHeight - 150;
        if (tempImg.height > maxImgHeight) {
            img.style.height = maxImgHeight + "px";
            img.height = maxImgHeight;
        } else {
            img.style.height = tempImg.height + "px";
            img.height = tempImg.height;
        }
        img.style.visibility = "visible";
        if (position == 1)
            prv.style.visibility = 'hidden';
        else
            prv.style.visibility = 'visible';
        if (position == imgs.length - 1)
            nxt.style.visibility = 'hidden';
        else
            nxt.style.visibility = 'visible';
        tempImg.style.display = "none";
    };
    tempImg.src = "content/gallery/" + gallery + "/photos/" + photo;

    var descriptionUrl = "content/gallery/" + gallery + "/text/" + photo.substr(0, photo.lastIndexOf(".")) + ".txt";
    getDescription(descriptionUrl);

    var viewer = getElement("gallery-viewer");
    viewer.style.width = document.documentElement.clientWidth + "px";
    viewer.style.height = document.documentElement.clientHeight + "px";

    var wrapper = getElement("galleryWrapper");
    wrapper.style.top = getScrollTopOffset() + "px";
    wrapper.style.display = "block";
    wrapper.style.width = document.documentElement.clientWidth + "px";
    wrapper.style.height = document.documentElement.clientHeight + "px";

    var back = getElement("gallery-viewer-back");
    back.style.display = "block";
    back.style.width = document.documentElement.clientWidth + "px";
    back.style.height = document.documentElement.clientHeight + "px";

    window.onresize = function() {
        window.onresize = null;
        var img = getElement("gallery-photo");
        if (img.style.visibility == "visible") {
            fotopopup(gallery, photo);
        }
    };

    //
    // FLOAT IT **************************************************************************************
    if (show) {
        floatGallery("galleryWrapper", 0, 0).floatIt();
    }
    // FLOAT IT **************************************************************************************
}

function hideFotopopup() {
    var back = getElement("gallery-viewer-back");
    back.style.display = "none";
    var prv = getElement("previous");
    prv.style.visibility = 'hidden';
    var nxt = getElement("next");
    nxt.style.visibility = 'hidden';
    var img = getElement("gallery-photo");
    img.style.visibility = "hidden";
    img.onload = function() {
        getElement("galleryWrapper").style.display = 'none';
        img.onload = null;
    };
    img.style.height = "25px";
    img.src = "static/img/loading.jpg";
}

/*
 *******************************************************************
 * You may use this code for free on any web page provided that
 * these comment lines and the following credit remain in the code.
 * Floating Div from http://www.javascript-fx.com
 *******************************************************************
 */

function floatGallery(id, sx, sy) {
    var el = getElement(id);
    var px = document.layers ? "" : "px";
    window[id + "_obj"] = el;
    if (d.layers)el.style = el;
    el.cx = el.sx = sx;
    el.cy = el.sy = sy;
    var bg = getElement("gallery-viewer-back");
    el.sP = function(x, y) {
        this.style.top = y + px;
        bg.style.top = y + px;
    };

    el.floatIt = function() {
        var pX, pY;
        pX = (this.sx >= 0) ? 0 : ns ? innerWidth :
                                  document.documentElement && document.documentElement.clientWidth ?
                                  document.documentElement.clientWidth : document.body.clientWidth;
        pY = getScrollTopOffset();
        if (this.sy < 0)
            pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ?
                                     document.documentElement.clientHeight : document.body.clientHeight;
        this.cx += (pX + this.sx - this.cx) / 8;
        this.cy += (pY + this.sy - this.cy) / 8;
        this.sP(this.cx, this.cy);
        var img = d.getElementById ? d.getElementById("gallery-photo") : d.all ? d.all["gallery-photo"] : d.layers["gallery-photo"];
        if (img.style.visibility == "visible") {
            setTimeout(this.id + "_obj.floatIt()", 40);
        }
    };
    return el;
}

function getScrollTopOffset() {
    return ns ? pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
}

function getElement(id) {
    return d.getElementById ? d.getElementById(id) : d.all ? d.all[id] : d.layers[id];
}

function getDescription(filename) {
    getElement("gallery-desc").style.display = "none";
    advAJAX.get({
        url: "scripts/text.php?src=" + filename,
        onSuccess : function(obj) {
            if (obj.responseText != "") {
                getElement("gallery-desc").innerHTML = obj.responseText;
                getElement("gallery-desc").style.display = "block";
            }
        }
    });
}

