/*
 * jQuery Cycle Plugin for light-weight slideshows
 * Examples and documentation at: http://malsup.com/jquery/cycle/
 *
 * @author: M. Alsup
 * @version: 0.53 (6/12/2007)
 * @requires jQuery v1.1.2 or later
 *
 * Based on the work of:
 *  1) Matt Oakes (http://portfolio.gizone.co.uk/applications/slideshow/)
 *  2) Torsten Baldes (http://medienfreunde.com/lab/innerfade/)
 */
(function($) {

$.fn.cycle = function(options) {
    return this.each(function() {
        if (options && options == 'stop') {
            if (this.cycleTimeout) clearTimeout(this.cycleTimeout);
            this.cycleTimeout = 0;
            return;
        }
        var $this = $(this), els = $this.children();
        if (els.length < 2) return; // don't bother

        $(els).each(function(i) { $(this).css('z-index', els.length-i); });

        var opts = $.extend({}, $.fn.cycle.defaults, options || {}, $.meta ? $this.data() : {});
        opts.fx = opts.fade ? ['fadeTo','fadeTo'] : ['slideUp','slideDown'];

        // allow shorthand overrides of width, height and timeout
        var cls = this.className;
        var w = parseInt((cls.match(/w:(\d+)/)||[])[1]) || opts.width
        var h = parseInt((cls.match(/h:(\d+)/)||[])[1]) || opts.height;
        opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1]) || opts.timeout;

        // make sure the timeout and speed settings are sane
        if (opts.speed.constructor == String)
            opts.speed = {slow: 600, fast: 200}[opts.speed] || 400;
        while((opts.timeout - opts.speed) < 250)
            opts.timeout += opts.speed;

        if ($this.css('position') == 'static') $this.css('position', 'relative');
        if (w) $this.width(w);
        if (h && h != 'auto') $this.height(h);

        var $els = $(els).css('position','absolute').hide();
        if (opts.fit && w) $els.width(w);
        if (opts.fit && h && h != 'auto') $els.height(h);
        $(els[0]).show();

        if (opts.pause)
            $this.hover(function(){opts.paused=1;}, function(){opts.paused=0;});

        opts.current = opts.random ? (Math.floor(Math.random() * (els.length-1)))+1 : 1;
        opts.last = 0;
        this.cycleTimeout = setTimeout(function(){$.fn.cycle.go(els, opts)}, opts.timeout);
    });
};

$.fn.cycle.go = function (els, opts) {
    if (els[0].parentNode.cycleTimeout === 0) return;
    if (!opts.paused) {
        $(els[opts.last])[opts.fx[0]](opts.speed, opts.fade?0:null);
        $(els[opts.current])[opts.fx[1]](opts.speed, opts.fade?1:null);

        if (opts.random) {
            opts.last = opts.current;
            while (opts.current == opts.last)
                opts.current = Math.floor(Math.random() * els.length);
        }
        else { // sequence
            var roll = (opts.current + 1) == els.length;
            opts.current = roll ? 0 : opts.current+1;
            opts.last    = roll ? els.length-1 : opts.current-1;
        }
    }
    setTimeout(function() { $.fn.cycle.go(els, opts) }, opts.timeout);
};

$.fn.cycle.defaults = {
    height:  'auto',   // container height
    fade:     1,       // true for fade, false for slide
    speed:    400,     // any valid fx speed value
    timeout:  4000,    // ms duration for each slide
    random:   0,       // true for random, false for sequence
    fit:      0,       // force slides to fit container
    pause:    0        // true to enable "pause on hover"
};

})(jQuery);
