$(document).ready(function()
{
    var totalImages = $("#mini-gallery div").length - 1;
    var currentImageNum = 0;
    var allowClick = "yes";
    var animating = false;
	var slideSpeed = 7500;
	var slideIntervalId = 0;


    //set the thickbox link for first image
    setLightboxLink(totalImages, "startup");


    // when you rollover image show tools (prev + next only if more than 1
    $(".pic-tools").hide();

    if (!totalImages)
    {
        $("#mini-gallery .pt-prev").hide();
        $("#mini-gallery .pt-next").hide();
    }

    $(".pic-holder").hover(function()
    {
        if (!animating)
        {
            animating = true;

            $(this).children('ul').fadeIn("normal", function()
            {
                animating = false;
            });
        }
    }, function()
    {
        $(this).children('ul').fadeOut("normal");
    });



    // 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";
				slideIntervalId = setInterval("nextSlide()", slideSpeed);
            });
        }

        else
        {
            $("#mini-gallery div:visible").fadeOut("fast", function()
            {
                $("#mini-gallery div:eq(" + (x - 1) + ")").fadeIn("fast");

                allowClick = "yes";
				slideIntervalId = setInterval("nextSlide()", slideSpeed);
            });
        }

    }


    // 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";
				slideIntervalId = setInterval("nextSlide()", slideSpeed);
            });
        }

        else
        {
            $("#mini-gallery div:visible").fadeOut("fast", function()
            {
                $("#mini-gallery div:eq(" + (x + 1) + ")").fadeIn("fast");

                allowClick = "yes";
				slideIntervalId = setInterval("nextSlide()", slideSpeed);
            });
        }
    }


    // 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").attr("href", newLargeImageLink);
        $("#mini-gallery .thickbox").attr("title", newLargeImageTitle);
    }


    // 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")
        {
			clearInterval (slideIntervalId);
            getPrevImage(currentImageNum, totalImages);
            setLightboxLink(currentImageNum, "backwards");
            updateNum(currentImageNum, "backwards");
        }

        else
        {
            return false;
        }
    });


    // bind click event to 'next image' button
    $("#mini-gallery .pt-next").click(function()
    {
        if (allowClick == "yes")
        {
			clearInterval (slideIntervalId);
            getNextImage(currentImageNum, totalImages);
            setLightboxLink(currentImageNum, "fowards");
            updateNum(currentImageNum, "fowards");
        }

        else
        {
            return false;
        }
    });

	if(totalImages > 0) {
		slideIntervalId = setInterval("nextSlide()", slideSpeed);
	}
});



function nextSlide(){
	slideShowActive = true;
	$("#mini-gallery .pt-next").trigger("click");
}

