/* IN PAGE CODE:

	$(document).ready(function () {
        $("#supersize").simpleslideshow({ transition_interval: 7000 });
	});

*/

(function ($) {
    $.fn.simpleslideshow = function (simpleslideshowoptions) {
        var options = $.extend($.fn.simpleslideshow.defaults, simpleslideshowoptions);
        options.selector = this;

        if ($(this).find("img").length == 1) {
            options.transition = true;
            options.carousel = false;
        }

        $.isPaused = false;

        if (options.transition) {
            $.loadedImages = new Array();
            $.loadedImagesIndex = 0;
            $.totalImages = $(this).find("img").length;

            //Lazy load the images and show them as they are ready, keep the orig height and width for ratio.
            //the jQuery height and width return zero because they are not in the DOM.
            $(this).find("img").each(function () {
                $("<img src=\"" + this.src + "\" />").load(function () {
                    $.loadedImages.push(this);
                    nextSlide();
                });
            });

            $(options.selector).empty();
        }
    };

    $.fn.simpleslideshow.defaults = {
        selector: null,
        carousel: false,
        transition: true,
        vertical_center: 0,
        slide_interval: 10,
        initial_transition_interval: 1000,
        transition_interval: 5000,
        transition_animation_interval: 750
    };
})(jQuery);

function stopSlides() {
    $.isPaused = true;
}

function restartSlides() {
    $.isPaused = false;
}

function nextSlide() {
    var options = $.extend($.fn.simpleslideshow.defaults, $.fn.simpleslideshow.options);

    if ($.totalImages != $.loadedImages.length) return;

    if (options.transition) {
        if ($.loadedImages.length == 0 && $.totalImages == 1) {
            //The first pass the image might not load so we have to ensure that we come back.
            //Use timeout instead of interval so we don't stop in the fadeout and it just continues.
            $.slideInterval = setTimeout(nextSlide, options.initial_transition_interval);
            return;
        } else if ($.loadedImages.length == 0) {
            return;
        }

        if ($.loadedImagesIndex >= $.loadedImages.length - 1) {
            $.loadedImagesIndex = 0;
        } else {
            $.loadedImagesIndex++;
        }
		
		// executes for first image load
        if ($(options.selector).find("img").length == 0 && $.loadedImages.length > 0) {
            //console.log("first run");
            $(options.selector).hide().empty().append($($.loadedImages[$.loadedImagesIndex]));
            $(options.selector).find("img").hide();
            $(options.selector).show();
            $(options.selector).find("img").removeAttr("width").removeAttr("height");
            $(options.selector).find("img").fadeIn(options.transition_animation_interval);

            if ($.totalImages == 1) {
                //We used a timeout so that it didn't cross intervals.
                clearTimeout($.slideInterval);
                return;
            }
            resizeBg();
        } else {	// executes for every shift between images
            //console.log("every other run");
			$(options.selector).find("img").fadeOut(options.transition_animation_interval, function() {
                $(this).remove();
				
				if ($.totalImages == 1) {
                    //We used a timeout so that it didn't cross intervals.
                    clearTimeout($.slideInterval);
                    return;
                }
			});
			$(options.selector).prepend($($.loadedImages[$.loadedImagesIndex]));
			$(options.selector).find("img").show().removeAttr("width").removeAttr("height");
			resizeBg();
        }

        if (!$.slideInterval) {
            $.slideInterval = setInterval(nextSlide, options.transition_interval);
        }
    }

    if (options.carousel) {
        if ($.isPaused) {
            return;
        }

        var d = $(options.selector).find("div");
        var w = $(window).width();

        var t = 0; d.find("img").each(function() { t += $(this).width(); });

        //See if we have to clone the elements to the end if the front of the div is nearing.
        if (t <= Math.abs(d.offset().left) + w) {
            //Add the width of the current div again because we are cloning it.
            d.css("width", d.width() + d.width());

            //If the end is near the clone the child nodes.  This will leak memory but keep the animation from jumping.
            d.children().clone().appendTo(d);
        }

        //Animate only 1 pixel so IE redraw doesn't look like crap and the pause is in place.
        d.css("left", --d.offset().left);
    }

    if ($.totalImages == 1) {
        clearInterval($.slideInterval);
    }
}
