$(document).ready(function() {
    var totalImages = $("#mini-gallery div").length - 1;
    var currentImageNum = 0;
    var allowClick = "yes";
    var animating = false;


    //set the thickbox link for first image
    setLightboxLink(totalImages, "startup");

    // retrieves link from querystring and opens corresponfing lightbox
    linkID = gup("linkID");
    linkID = linkID.replace("%20", " ");
    if (linkID.length > 0) {
        $("a[id='" + linkID + "']").trigger('click');
    }

    // when you rollover image show tools (prev + next only if more than 1
    $(".pic-tools").hide();

    if (!totalImages) {
        $("#mini-gallery .pt-prev").remove(); //just hiding doesn't work in IE
        $("#mini-gallery .pt-next").remove();
    }

    $(".pic-holder").hover(function() {
        if (!animating) {
            animating = true;

            $(this).children('ul').fadeIn("normal", function() {
                animating = false;
            });
        }
    }, function() {
        $(this).children('ul').hide();
    });



    // rotate images backwards
    function getPrevImage(x) {
        allowClick = "no"

        if (x == 0) {
            $("#mini-gallery div:visible").fadeOut("fast", function() {
                $("#mini-gallery div:eq(" + totalImages + ")").fadeIn("fast");

                allowClick = "yes";
            });
        }

        else {
            $("#mini-gallery div:visible").fadeOut("fast", function() {
                $("#mini-gallery div:eq(" + (x - 1) + ")").fadeIn("fast");

                allowClick = "yes";
            });
        }

    }


    // rotate images forwards
    function getNextImage(x) {
        allowClick = "no"

        if (x == totalImages) {
            $("#mini-gallery div:visible").fadeOut("fast", function() {
                $("#mini-gallery div:eq(" + 0 + ")").fadeIn("fast");

                allowClick = "yes";
            });
        }

        else {
            $("#mini-gallery div:visible").fadeOut("fast", function() {
                $("#mini-gallery div:eq(" + (x + 1) + ")").fadeIn("fast");

                allowClick = "yes";
            });
        }
    }


    // sets the new link + title to the lightbox
    function setLightboxLink(s, direction) {
        if (direction == "backwards") {
            if (s == 0) {
                var newLargeImageLink = $("#mini-gallery div:eq(" + totalImages + ") img").attr("rel");
                var newLargeImageTitle = $("#mini-gallery div:eq(" + totalImages + ") img").attr("alt");
            }

            else {
                var newLargeImageLink = $("#mini-gallery div:eq(" + (s - 1) + ") img").attr("rel");
                var newLargeImageTitle = $("#mini-gallery div:eq(" + (s - 1) + ") img").attr("alt");
            }
        }

        else {
            if (s == totalImages) {
                var newLargeImageLink = $("#mini-gallery div:eq(" + 0 + ") img").attr("rel");
                var newLargeImageTitle = $("#mini-gallery div:eq(" + 0 + ") img").attr("alt");
            }

            else {
                var newLargeImageLink = $("#mini-gallery div:eq(" + (s + 1) + ") img").attr("rel");
                var newLargeImageTitle = $("#mini-gallery div:eq(" + (s + 1) + ") img").attr("alt");
            }
        }

        $("#mini-gallery .thickbox, #enlarge-button").attr("href", newLargeImageLink);
        if (newLargeImageTitle) {
            $("#mini-gallery .thickbox, #enlarge-button").attr("title", newLargeImageTitle);
        }
        else {
            $("#mini-gallery .thickbox, #enlarge-button").attr("title", "");
        }
    }


    // updates the current number of image
    function updateNum(s, direction) {
        if (direction == "backwards") {
            if (s == 0) {
                currentImageNum = totalImages;
            }

            else {
                currentImageNum = s - 1;
            }
        }

        else {
            if (s == totalImages) {
                currentImageNum = 0;
            }

            else {
                currentImageNum = s + 1;
            }
        }
    }


    // bind click event to 'previous image' button
    $("#mini-gallery .pt-prev").click(function() {
        if (allowClick == "yes") {
            getPrevImage(currentImageNum, totalImages);
            setLightboxLink(currentImageNum, "backwards");
            updateNum(currentImageNum, "backwards");
        }

        return false;
    });


    // bind click event to 'next image' button
    $("#mini-gallery .pt-next").click(function() {
        if (allowClick == "yes") {
            getNextImage(currentImageNum, totalImages);
            setLightboxLink(currentImageNum, "fowards");
            updateNum(currentImageNum, "fowards");
        }

        return false;
    });

    $('.mini-gallery-thumbnail-list img').click(function() {
        var index = $.inArray(this, $('.mini-gallery-thumbnail-list img'));
        if (index > -1 && currentImageNum != index) {
            getNextImage(index - 1);
            setLightboxLink(index - 1, "fowards");
            currentImageNum = index;
            $(".pic-holder").children('ul').fadeIn("fast");
        }
    });
});
