﻿(function ($) {
    $.fn.slideFadeAsh = function (options) {

        var defaults = {
            random: false,
            height: 350,
            width: 400,
            fadeTime: 1000,
            loopTime: 3000
        };

        var options = $.extend(defaults, options);
        var $sibs, rndNum, $next
        var obj = $(this);
        var isRandom = 0;

        obj.css({ 'width': options.width + 'px', 'height': options.height + 'px' });


        if (options.random == true) { isRandom = (Math.random() * obj.children('IMG').length) };

        obj.find('IMG').eq(isRandom).addClass('active');

        function slideSwitch() {

            var $active = obj.find('IMG.active');
            if ($active.length == 0) { $active = obj.find('IMG').last(); };
            $next = $active.next().length ? $active.next() : obj.find('IMG').first();
            $sibs = $active.siblings();

            if (options.random == true) {
                rndNum = Math.floor(Math.random() * $sibs.length);
                $next = $($sibs[rndNum]);
            };

            $active.addClass('last-active');
            $next.css({ opacity: 0.0 })
                                       .addClass('active')
                                       .animate({ opacity: 1.0 }, options.fadeTime, function () {
                                           $active.removeClass('active last-active');
                                       });
        };


        $(window).load(function () {
            setInterval(slideSwitch, options.loopTime);
        });

    };
})(jQuery);
