(function($) {
$(document).ready(function($) {
    // 'Extra info' on the details page
    $("h2.toggle").click(function() {
        $("#detailinfo").slideToggle("slow");
        $(this).toggleClass("open");

        // Set the correct image for the arrow
        var arrow_state = $("#togglearrow").attr("src");
        if (arrow_state === (App.home + 'img/arrow_closed.png')) {
            $("#togglearrow").attr('src', App.home + 'img/arrow_open.png');
        } else {
            $("#togglearrow").attr('src', App.home + 'img/arrow_closed.png');
        }
    });

    // Sort options on gallery page
    $("#sortoptions > h2").click(function() {
        $("#sortoptions .content").slideToggle("medium");
    });

    // Confirm alerts
    $("a.confirm").click(function() {
       return confirm("Wilt u dit object echt verwijderen?");
    });

    function iviewer() {
        function open(src) {
            var firstZoom = true;

            $("#iviewer").fadeIn().trigger('fadein');

            $("#iviewer .viewer").
                width($(window).width() - 80).
                height($(window).height()).
                iviewer({
                    src : src,
                    ui_disabled : true,
                    zoom : 'fit',
                    initCallback : function() {
                        var self = this;

                        $("#iviewer .zoomin").click(function(e) {
                            e.preventDefault();
                            self.zoom_by(1);
                        });

                        $("#iviewer .zoomout").click(function(e) {
                            e.preventDefault();
                            self.zoom_by(-1);
                        });
                    },
                    onZoom : function() {
                        if (!firstZoom) return;

                        $("#iviewer .loader").fadeOut();
                        $("#iviewer .viewer").fadeIn();
                        firstZoom = true;
                    }
                }
            );
        }

        function close() {
            $("#iviewer").fadeOut().trigger('fadeout');
        }

        $('#detailimage').click(function(e) {
            e.preventDefault();
            var src = $(this).find("a").attr('href');
            open(src);
        });

        $("#iviewer .close").click(function(e) {
            e.preventDefault();
            close();
        });

        $("#iviewer").bind('fadein', function() {
            $(window).keydown(function(e) {
                if (e.which == 27) close();
            });
        });
    }

    // Make the gallery sortable
    if ($("#sortables").length > 0) {
        $("#sortables").sortable({
            revert: true
        });
    }

    $("#submitsort").click(function() {
        // construct what we are gonna give back to the PHP script
        var order = $("#sortables").sortable('serialize');
        // make a GET call
        $.get(App.home + 'admin/savedata.php', order, function(msg) {
            if (msg == 'ok') {
                alert('Nieuwe volgorde opgeslagen');
            } else {
                alert('Er is iets mis gegaan: ' + msg);
            }
        });
    });

    $("#detailinfo > a").click(function(e) {
        e.preventDefault();
        $(this).hide();
        $("#detailinfo .content").show();
    });

    $(".jshide").hide();
    $(".jsonly").show();

    iviewer();
});
})(jQuery);
