// Copyright 2009 Daniel Arbuckle
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
  Set FADE_IN and FADE_OUT to true or false to control
  whether photos fade in or out.
*/

FADE_IN = false;
FADE_OUT = true;

function hide_popper(pop) {
    pop.setStyle('z-index', 2);
    if(FADE_OUT) {
        pop.fade('out');
    }
    else {
        pop.fade('hide');
    }

    var on_hide = pop.getProperty('onhide');
    if(on_hide == null) { on_hide = ""; }

    setTimeout(function() {try { eval(on_hide); } catch(err) {}}, 1000); // Ugly "solution" to slow loading times on Windows
}

function show_popper(pop) {
    $$('.pp-fullsize').each(function(which) {
        if(which != pop) {
            hide_popper(which);
        }
    });
    pop.setStyle('z-index', 3);
    if(FADE_IN) {
        pop.fade('in');
    }
    else {
        pop.fade('show');
    }

    var on_show = pop.getProperty('onshow');
    if(on_show == null) { on_show = ""; }

    setTimeout(function() {try { eval(on_show); } catch(err) {}}, 1000); // Ugly "solution" to slow loading times on Windows
}

function bind_poppers() {
    $$('.pp-fullsize').each(function(pop) {
        pop.fade('hide');
        pop.setStyle('display', 'block');
        var parent = pop.getParent();
        parent.setStyle('cursor', 'pointer');

        if(pop.hasClass('pp-stickaround')) {
            function stop(evt) {
                evt.stop()
            }

            pop.addEvent('click', stop);
        }

        function show(evt) {
            parent.removeEvent('click', show);
            document.addEvent('click', hide);
            evt.stop();
            show_popper(pop);
        }

        function hide(evt) {
            document.removeEvent('click', hide);
            parent.addEvent('click', show);
            evt.stop()
            hide_popper(pop);
        }

        pop.getChildren('.pp-close').each(function(closer) {
            closer.setStyle('cursor', 'pointer');
            closer.addEvent('click', hide);
        });

        parent.addEvent('click', show);
    });
}

window.addEvent('domready', bind_poppers);
