tot - jquery/jquery.alternation.js 18
Not logged in
[Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Timeline
  [Raw
jquery/jquery.alternation.js 18
/*
**  jquery.alternation.js -- jQuery plugin for DOM element alternation
**  Copyright (c) 2007 Ralf S. Engelschall <rse@engelschall.com> 
**  Copyright (c) 2006 Torsten Baldes & Matt Oakes
**  Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
**
**  $LastChangedDate$
**  $LastChangedRevision$
*/

/*
 *  <div class="alternation"> 
 *      <div>content 1</div>
 *      <div>content 2</div>
 *      <div>content 3</div>
 *  </div>
 *
 *  $('.alternation').alternation({
 *      children:    optional children selector                          (default: 'null')
 *      alternation: alternation type: "sequence" or "random"            (default: 'sequence')
 *      timeout:     alternation time (in ms)                            (default: 2000)
 *      animation:   animation type: "fade" or "slide"                   (default: "fade")
 *      speed:       animation speed (in ms or "slow", "normal", "fast") (default: "normal")
 *      class:       container element CSS class                         (default: "alternation") 
 *      height:      container element CSS height                        (default: "auto")
 *  });
 */

(function($) {

    /* jQuery dynamic object method */
    $.fn.alternation = function(options) {
        return this.each(function() {   
            $.alternation(this, options);
        });
    };

    /* jQuery static function */
    $.alternation = function(container, options) {
        var settings = {
            'children':    null,
            'alternation': 'sequence',
            'timeout':     2000,
            'animation':   'fade',
            'speed':       'normal',
            'class':       'alternation',
            'height':      'auto'
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative');
            $(container).css('height', settings["height"]);
            $(container).addClass(settings["class"]);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i));
                $(elements[i]).css('position', 'absolute');
                $(elements[i]).hide();
            };
            if (settings["alternation"] == "sequence") {
                setTimeout(function() {
                    $.alternation._next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings["alternation"] == "random") {
                setTimeout(function() {
                    do {
                        current = Math.floor(Math.random() * elements.length);
                    } while (current == 0)
                    $.alternation._next(elements, settings, current, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else
                alert('jQuery: alternation: parameter "alternation" must either be "sequence" or "random"');
        }
    };

    /* jQuery static function */
    $.alternation._next = function(elements, settings, current, last) {
        if (settings["animation"] == 'slide') {
            $(elements[last]).slideUp(settings.speed,
                $(elements[current]).slideDown(settings.speed));
        } else if (settings["animation"] == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed);
        } else
            alert('jQuery: alternation: parameter "animation" must either be "slide" or "fade"');
        if (settings["alternation"] == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings["alternation"] == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('jQuery: alternation: parameter "alternation" must either be "sequence" or "random"');
        setTimeout((function() {
            $.alternation._next(elements, settings, current, last);
        }), settings.timeout);
    };

})(jQuery);