
// This global variable holds the popup animation (type of Effect.Scale)
// We keep track of it in order to abort the animation prematurely
// if the user moves the mouse out of the thumbnail
var scale;

function initPopups() {
    var popups = document.getElementsByClassName("popup");
    for(var i in popups) {
        popups[i].onmouseover = popup;
    }
    
    if ($('popup') != null) {
        $('popup').onmouseout = unpop;
    }
}

function initGallery() {
    initPopups();
    $('loadImg').setOpacity(0);
    $('galleryContent').show();
    var nextButton = $('nextButton'), prevButton = $('prevButton');
    prevButton.onclick = nextButton.onclick = galleryPage;
}

function popup(source) {
    // Safari doesn't like absolute positioning, so
    // don't do popups for it
    if (Prototype.Browser.WebKit)
        return;

    if (this != null) {
        source = this;
    }
    var p = $('popup');
    var pImg = $('popupImg');
    
    pImg.src = source.src;
    pImg.parentNode.href = source.parentNode.href;
    pImg.width = source.width;
    pImg.height = source.height;
    pImg.style.maxWidth = source.width + "px";
    pImg.style.maxHeight = source.height + "px";
    
    p.style.top = source.cumulativeOffset().top;
    p.style.left = source.cumulativeOffset().left;
    p.style.width = (source.width + 15) + "px";
    p.style.height = (source.height + 15) + "px";
    p.style.display = "block";
    
    /*scale = new Effect.Scale('popup', 150, {
        duration: 0.2,
        fps: 40,
        scaleFromCenter: true
    });*/
}

function unpop(source) {
    if (scale != null && scale.state == "running") {
        scale.cancel();
    }
    var p = $('popup');
    p.style.display = 'none';
}

function galleryDisablePageButtons() {
    var prevButton = $('prevButton');
    prevButton.addClassName('disabled');
    prevButton.onclick = null;
    
    var nextButton = $('nextButton');
    nextButton.addClassName('disabled');
    nextButton.onclick = null;
}

function galleryPage(source) {
    if (this != window && this != null) { source = this; }
    if (source.hasClassName('disabled')) {
        return;
    }
    
    var page = parseInt($('page').value);
    if (source.id == 'nextButton') {
        page = page + 1;
    }
    else {
        page = page - 1;
    }
    $('page').value = page;
    
    // Disable navigation buttons
    galleryDisablePageButtons();
    
    // Begin the load animation
    $('loadImg').setOpacity(100);
    
    // Fades out the images. After the last fade animation,
    // Request the new page from the server. Server also returns
    // the current page and the number of pages
    var images = document.getElementsByClassName("popup");
    for (var i = 0; i < images.length; i++) {
        images[i].onmouseover = null;
    }
    
    new Effect.Opacity('galleryContent', {
        duration: 0.2,
        fps: 40,
        from: 1.0,
        to: 0.0,
        afterFinish: function(p) {
            new Ajax.Request('galleryPage.php?page=' + $('page').value + "&gallery=" + $('galID').value, {
                method: 'get',
                onSuccess: function(transport) {
                    var content = $('galleryContent');
                    content.setOpacity(0);
                    content.hide();
                    var htmlEl = transport.responseXML.getElementsByTagName('html')[0];
                    
                    for (var i = 0; i < htmlEl.childNodes.length; i++) {
                        if (htmlEl.childNodes[i].nodeType == 4) { // Text/CDATA section
                            content.innerHTML = htmlEl.childNodes[i].nodeValue;
                        }
                    }
                    var pageCount = parseInt(transport.responseXML.getElementsByTagName('count')[0].childNodes[0].nodeValue);
                    var page = parseInt($('page').value);
                    if (page > 0) {
                        $('prevButton').removeClassName('disabled');
                    }
                    if (page + 1 < pageCount) {
                        $('nextButton').removeClassName('disabled');
                    }
                    
                    // Fading in the gallery causes flicker in Firefox
                    // so just have it appear
                    if (Prototype.Browser.Gecko) {
                        initGallery();
                        content.setOpacity(1);
                    }
                    else {
                        window.setTimeout(fadeContent, 500);
                    }
                }
            }); // End Ajax.Request
        } // End afterFinish
    }); // End Effect.Opacity
}

function fadeContent() {
    new Effect.Opacity('galleryContent',{
        duration: 0.5,
        fps: 40,
        start: 0.0,
        end: 1.0,
        afterFinish: initGallery
    });
}

addLoadEvent(initPopups);
