﻿/* ------------------------------------------------------------------------
	SimpleSlideshow for Jquery
	Developed with jQuery Version: 1.4.3
	Author: Scott Neish 
	Version: 1.0

MARKUP:
<div id="simpleslideshow"> 
<a href="#"><img src="images/image.jpg" style="width: 1500px; height: 900px;" alt="" border="0" title="image"></a>
<a href="#"><img src="images/image.jpg" style="width: 1500px; height: 900px;" alt="" border="0" title="image"></a>
<img src="images/image.jpg" style="width: 1500px; height: 900px;" alt="" border="0" title="image">
</div>

SCRIPT:
$(document).ready(function () {
$("#simpleslideshow").simpleslideshow({ crossfade: true, initial_transition_interval: 1000, transition_interval: 7000, transition_animation_interval: 750 });
});

*/

(function ($) {
    $.fn.simpleslideshow = function (simpleslideshowoptions) {
        var options = $.extend($.fn.simpleslideshow.defaults, simpleslideshowoptions);
        options.selector = this;

        $.loadedImages = new Array();
        $.loadedImagesIndex = 0;
        $.totalImages = $(this).find("img").length;

        $(this).children().css({ "position": "absolute" });
        //$(this).find("img").css({ "position": "absolute" });

        // 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 () {

            // get the image's parent tag
            var imgWrp = $(this).parent().get(0);
            var w = this.width;
            var h = this.height;

            // if there is no parent or if the parent is the container set imgWrp to just the <img>
            if ((imgWrp == null) || ($(imgWrp).attr("id") == $(options.selector).attr("id"))) {
                imgWrp = $(this);
            }

            $("<img src=\"" + this.src + "\" />").load(function () {

                if ($(imgWrp).get(0).tagName == "IMG") {
                    $(imgWrp).attr("orig-height", h).attr("orig-width", w);
                } else {
                    $(imgWrp).find("img").attr("orig-height", h).attr("orig-width", w);
                }

                // When image loads push imgWrp to array														
                $.loadedImages.push(imgWrp);

                // start slideshow
                nextSlide();
            });
        });

        // clear containter of original content
        $(options.selector).empty();

        if (options.resize) {
            $(window).bind("resize", resizeBinder);
        } else {
            $(window).unbind("resize", resizeBinder);
        }
    };

    $.fn.simpleslideshow.defaults = {
        selector: null,
        crossfade: true,
        initial_transition_interval: 1000,
        transition_interval: 5000,
        transition_animation_interval: 750,
        function_hook: null,
        resize: true
    };
})(jQuery);

$.fn.resize = function () {

    var options = $.extend($.fn.simpleslideshow.defaults, $.fn.simpleslideshow.options);

    $(this).each(function () {
        var c = $(this);

        var ratio = c.attr("orig-height") / c.attr("orig-width");

        if (isNaN(ratio))
            return false;

        var imagewidth = c.width();
        var imageheight = c.height();
        var browserwidth = $(window).width();
        var browserheight = $(window).height();

        if ((browserheight / browserwidth) > ratio) {
            c.height(browserheight);
            c.width(browserheight / ratio);
        } else {
            c.width(browserwidth);
            c.height(browserwidth * ratio);
        }
    });

    return false;
};

function resizeBinder() {
    var options = $.extend($.fn.simpleslideshow.defaults, $.fn.simpleslideshow.options);
    $(options.selector).find("img").resize();
}

function nextSlide() {

    // gives us access to plugin options
    var options = $.extend($.fn.simpleslideshow.defaults, $.fn.simpleslideshow.options);
    var callBackPointer = $.extend($.fn.simpleslideshow);

    // if not all images have been lazyloaded -> return
    if ($.totalImages != $.loadedImages.length) return;

    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;
    }

    // loop to next image in the array
    if ($.loadedImagesIndex >= $.loadedImages.length - 1) {
        $.loadedImagesIndex = 0;
    } else {
        $.loadedImagesIndex++;
    }

    // executes for first image load
    if ($(options.selector).find("img").length == 0 && $.loadedImages.length > 0) {
        $(options.selector).hide().empty().append($($.loadedImages[$.loadedImagesIndex]));
        $(options.selector).find("img").hide();
        $(options.selector).show();
        $(options.selector).find("img").fadeIn(options.transition_animation_interval);

        if (options.resize) {
            $(options.selector).find("img").resize();
        } else {
//            $(options.selector).removeAttr('style');
        }

        if ($.totalImages == 1) {
            //We used a timeout so that it didn't cross intervals.
            clearTimeout($.slideInterval);
            return;
        }

    } else {

        // executes for every shift between images
        $(options.selector).children().fadeOut(options.transition_animation_interval, function () {
            $(this).remove();
            if ($.totalImages == 1) {
                clearTimeout($.slideInterval); //We used a timeout so that it didn't cross intervals.
                return;
            }
        });

        $(options.selector).prepend($($.loadedImages[$.loadedImagesIndex]));
        $(options.selector).children().show();

        if (options.resize) {
            $(options.selector).find("img").resize();
        } else {
//            $(options.selector).removeAttr('style');
        }
    }

    if (!$.slideInterval) {
        $.slideInterval = setInterval(nextSlide, options.transition_interval);
    }
}
