/**
 * File: gallery.js
 * Description:
 *   Defines a library of variables and function used
 *   to control the Gallery.
 */

var Gallery = {
    /**
     * A collection of callback functions
     * used by the navigation and thumb buttons.
     */
    'buttonCallbacks' : {
        'preventDefault' : function(event) {
            event.preventDefault();
        },
        'thumb' : function() {
            Gallery.setActiveImage($(this).attr("imgIndex"));
        },
        'nav' : function() {
            if(!($(this).children().hasClass("Disabled"))) {
                Gallery.setActiveImage($(this).attr("imgIndex"));
            }
        }
    },

    /**
     * Initializes the navigation and thumb buttons
     * and binds them to their respective callbacks.
     * @param pExtra The extra parts of the URI path. Used to determine the inital image.
     */
    'initialize' : function(pExtra) {
        // Initialize the buttons
        Gallery.initializeButtons();

        // Bind the buttons
        Gallery.bindButtons();

        // Set the inital image
        var currentIndex = pExtra[1] ? parseInt(pExtra[1]) : 0;
        $("#GalleryImageList a").eq(currentIndex).click();
    },

    /**
     * Initializes the buttons.
     * Prevents their default click action, otherwise used to ensure
     * graceful degregation.
     */
    'initializeButtons' : function() {
        $("#GalleryImageList a").click(Gallery.buttonCallbacks['preventDefault']);
        $("#GalleryControls_First").click(Gallery.buttonCallbacks['preventDefault']);
        $("#GalleryControls_Previous").click(Gallery.buttonCallbacks['preventDefault']);
        $("#GalleryControls_Next").click(Gallery.buttonCallbacks['preventDefault']);
        $("#GalleryControls_Last").click(Gallery.buttonCallbacks['preventDefault']);
    },

    /**
     * Binds the buttons to their respective click callback functions.
     */
    'bindButtons' : function() {
        $("#GalleryImageList a").click(Gallery.buttonCallbacks['thumb']);
        $("#GalleryControls_First").click(Gallery.buttonCallbacks['nav']);
        $("#GalleryControls_Previous").click(Gallery.buttonCallbacks['nav']);
        $("#GalleryControls_Next").click(Gallery.buttonCallbacks['nav']);
        $("#GalleryControls_Last").click(Gallery.buttonCallbacks['nav']);
    },

    /**
     * Unbids the buttons from their respective click callback functions.
     */
    'unbindButtons' : function() {
        $("#GalleryImageList a").unbind('click', Gallery.buttonCallbacks['thumb']);
        $("#GalleryControls_First").unbind('click', Gallery.buttonCallbacks['nav']);
        $("#GalleryControls_Previous").unbind('click', Gallery.buttonCallbacks['nav']);
        $("#GalleryControls_Next").unbind('click', Gallery.buttonCallbacks['nav']);
        $("#GalleryControls_Last").unbind('click', Gallery.buttonCallbacks['nav']);
    },

    /**
     * Sets the active image in the gallery.
     * Switches the preview image and updates the thumbs and nav buttons.
     * @param pIndex The index of the thumb that represents the image.
     */
    'setActiveImage' : function(pIndex) {
        // Get the selected thumb
        var selected = $("#GalleryImageList a[imgIndex="+ pIndex +"]").eq(0);

        // Switch the Selected thumb
        $("#GalleryImageList img[class=Selected]").each(function(){
            $(this).removeClass('Selected');
        });
        selected.children("img").addClass('Selected');

        // Show the load bar
        var loadBarTimeout = window.setTimeout(function(){
            showLoading($("#GalleryPreview"), 'center', 'middle');
        }, 150);

        // Unbind the gallery buttongs
        Gallery.unbindButtons();

        // Set a timeout at 30 seconds
        var loadingTimeout = window.setTimeout(function() {
            $("#GalleryImagePreview").attr("src", "")
                                     .attr("alt", "Image failed to load")
                                     .css('display', 'inline-block');
            window.clearTimeout(loadBarTimeout);
            hideLoading();
            Gallery.bindButtons();
        }, 30000);

        // Set the main image
        $("#GalleryImagePreview").load(function() {
            var rThis = $(this);
            //window.setTimeout(function() {
                window.clearTimeout(loadBarTimeout);
                window.clearTimeout(loadingTimeout);
                hideLoading();
                rThis.fadeIn("slow", function() {
                    Gallery.bindButtons();
                });
            //}, 1500)
            

        }).hide().attr("src", selected.attr("imgSrc"));

        // Update navigation
        Gallery.updateControls(parseInt(selected.attr("imgIndex")));

        // Set the location hash
        window.location.hash = "gallery/image/"+ pIndex +"/";
    },

    /**
     * Updates the navigation and thumbnail controls.
     * @param pIndex The index of the active thumbnail.
     */
    'updateControls' : function(pIndex) {
        // Find the last index
        var lastIndex = parseInt($("#GalleryImageList a:last").attr('imgIndex'));

        // Set the "x of total" count
        var number = pIndex + 1;
        $("#Gallery_CountCurrent").html(number);

        // Update Left group
        if(pIndex > 0) {
            var prev = $("#GalleryControls_Previous");

            $("#GalleryControls_First").attr('href', pageRoot + "gallery/image/0/").children().removeClass("Disabled");
            prev.attr('href', pageRoot + "gallery/image/" + (pIndex - 1)).children().removeClass("Disabled");

            prev.attr('imgIndex', pIndex - 1);
            prev.attr('imgSrc', $("#GalleryImageList a[imgIndex="+ (pIndex - 1) +"]").eq(0).attr('imgSrc'));
        }
        else {
            $("#GalleryControls_First").removeAttr('href').children().addClass("Disabled");
            $("#GalleryControls_Previous").removeAttr('href').children().addClass("Disabled");
        }

        // Update Right group
        if(pIndex < lastIndex) {
            var next = $("#GalleryControls_Next");

            $("#GalleryControls_Last").attr('href', pageRoot + "gallery/image/"+ lastIndex +"/").children().removeClass("Disabled");
            next.attr('href', pageRoot + "gallery/image/" + (pIndex + 1)).children().removeClass("Disabled");

            next.attr('imgIndex', pIndex + 1);
            next.attr('imgSrc', $("#GalleryImageList a[imgIndex="+ (pIndex + 1) +"]").eq(0).attr('imgSrc'));
        }
        else {
            $("#GalleryControls_Last").removeAttr('href').children().addClass("Disabled");
            $("#GalleryControls_Next").removeAttr('href').children().addClass("Disabled");
        }
    }
}