/**
 * Closure for the Available Puppies page.
 */
(function() {
    /**
     * Function runs on page load and sets up the necessary image swapping
     * event handlers.
     */
    var initialize = function() {
        $(".availablePuppyDiv").each(function(index, div) {
			$(div).on("mouseover", function() {
				imageSwitchStart(this);
			});
        });
    };

    /**
     * Function called to kick off the image swapping.  It creates the
     * interval and sets up the necessary handling to stop the swapping.
     *
     * @param container - The container that is being moused over
     */
    var imageSwitchStart = function(container) {
        //Set the interval with the appropriate time (
        var interval = setInterval(function() {
            switchImage(container);
        }, 1000);

        //Clear the interval onmouseout of the container.
        $(container).on("mouseout", function() {
        	clearInterval(interval);
        });
    }

    /**
     * Function does the physical swapping.
     *
     * @param container - The container that is being moused over
     */
    var switchImage = function(container) {
        //Get all images in the container.
        var images = container.getElementsByTagName("img");

        //Find the image that's currently being displayed.
        for (var i = 0; i < images.length; i++) {
        	var $image = $(images[i]);
            if (!$image.hasClass("hidden")) {
                $image.addClass("hidden");
                break;
            }
        }

        //Display the appropriate image.
        if (i == (images.length - 1)) {
        	$(images[0]).removeClass("hidden");
        } else {
        	$(images[i + 1]).removeClass("hidden");
        }
    };
    
    $(document).ready(initialize);
}());



