/*
 * jQuery Cycle Lite Plugin
 * http://malsup.com/jquery/cycle/lite/
 * Copyright (c) 2008 M. Alsup
 * Version: 1.0 (06/08/2008)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.2.3 or later
 */
var $j = jQuery.noConflict();
(function($j) {
		  

var ver = 'Lite-1.0';

$j.fn.cycle = function(options) {
    return this.each(function() {
        options = options || {};
        
        if (this.cycleTimeout) clearTimeout(this.cycleTimeout);
        this.cycleTimeout = 0;
        this.cyclePause = 0;
        
        var $jcont = $j(this);
        var $jslides = options.slideExpr ? $j(options.slideExpr, this) : $jcont.children();
        var els = $jslides.get();
        if (els.length < 2) {
            if (window.console && window.console.log)
                window.console.log('terminating; too few slides: ' + els.length);
            return; // don't bother
        }

        // support metadata plugin (v1.0 and v2.0)
        var opts = $j.extend({}, $j.fn.cycle.defaults, options || {}, $j.metadata ? $jcont.metadata() : $j.meta ? $jcont.data() : {});
            
        opts.before = opts.before ? [opts.before] : [];
        opts.after = opts.after ? [opts.after] : [];
        opts.after.unshift(function(){ opts.busy=0; });
            
        // allow shorthand overrides of width, height and timeout
        var cls = this.className;
        opts.width = parseInt((cls.match(/w:(\d+)/)||[])[1]) || opts.width;
        opts.height = parseInt((cls.match(/h:(\d+)/)||[])[1]) || opts.height;
        opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1]) || opts.timeout;

        if ($jcont.css('position') == 'static') 
            $jcont.css('position', 'relative');
        if (opts.width) 
            $jcont.width(opts.width);
        if (opts.height && opts.height != 'auto') 
            $jcont.height(opts.height);

        var first = 0;
        $jslides.css({position: 'absolute', top:0, left:0}).hide().each(function(i) { 
            $j(this).css('z-index', els.length-i) 
        });
        
        $j(els[first]).css('opacity',1).show(); // opacity bit needed to handle reinit case
        if ($j.browser.msie) els[first].style.removeAttribute('filter');

        if (opts.fit && opts.width) 
            $jslides.width(opts.width);
        if (opts.fit && opts.height && opts.height != 'auto') 
            $jslides.height(opts.height);
        if (opts.pause) 
            $jcont.hover(function(){this.cyclePause=1;}, function(){this.cyclePause=0;});

        $j.fn.cycle.transitions.fade($jcont, $jslides, opts);
        
        $jslides.each(function() {
            var $jel = $j(this);
            this.cycleH = (opts.fit && opts.height) ? opts.height : $jel.height();
            this.cycleW = (opts.fit && opts.width) ? opts.width : $jel.width();
        });

        $jslides.not(':eq('+first+')').css({opacity:0});
        if (opts.cssFirst)
            $j($jslides[first]).css(opts.cssFirst);

        if (opts.timeout) {
            // ensure that timeout and speed settings are sane
            if (opts.speed.constructor == String)
                opts.speed = {slow: 600, fast: 200}[opts.speed] || 400;
            if (!opts.sync)
                opts.speed = opts.speed / 2;
            while((opts.timeout - opts.speed) < 250)
                opts.timeout += opts.speed;
        }
        opts.speedIn = opts.speed;
        opts.speedOut = opts.speed;

 		opts.slideCount = els.length;
        opts.currSlide = first;
        opts.nextSlide = 1;

        // fire artificial events
        var e0 = $jslides[first];
        if (opts.before.length)
            opts.before[0].apply(e0, [e0, e0, opts, true]);
        if (opts.after.length > 1)
            opts.after[1].apply(e0, [e0, e0, opts, true]);
        
        if (opts.click && !opts.next)
            opts.next = opts.click;
        if (opts.next)
            $j(opts.next).bind('click', function(){return advance(els,opts,opts.rev?-1:1)});
        if (opts.prev)
            $j(opts.prev).bind('click', function(){return advance(els,opts,opts.rev?1:-1)});

        if (opts.timeout)
            this.cycleTimeout = setTimeout(function() {
                go(els,opts,0,!opts.rev)
            }, opts.timeout + (opts.delay||0));
    });
};

function go(els, opts, manual, fwd) {
    if (opts.busy) return;
    var p = els[0].parentNode, curr = els[opts.currSlide], next = els[opts.nextSlide];
    if (p.cycleTimeout === 0 && !manual) 
        return;

    if (manual || !p.cyclePause) {
        if (opts.before.length)
            $j.each(opts.before, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
        var after = function() {
            if ($j.browser.msie)
                this.style.removeAttribute('filter');
            $j.each(opts.after, function(i,o) { o.apply(next, [curr, next, opts, fwd]); });
        };

        if (opts.nextSlide != opts.currSlide) {
            opts.busy = 1;
            $j.fn.cycle.custom(curr, next, opts, after);
        }
        var roll = (opts.nextSlide + 1) == els.length;
        opts.nextSlide = roll ? 0 : opts.nextSlide+1;
        opts.currSlide = roll ? els.length-1 : opts.nextSlide-1;
    }
    if (opts.timeout)
        p.cycleTimeout = setTimeout(function() { go(els,opts,0,!opts.rev) }, opts.timeout);
};

// advance slide forward or back
function advance(els, opts, val) {
    var p = els[0].parentNode, timeout = p.cycleTimeout;
    if (timeout) {
        clearTimeout(timeout);
        p.cycleTimeout = 0;
    }
    opts.nextSlide = opts.currSlide + val;
    if (opts.nextSlide < 0) {
        opts.nextSlide = els.length - 1;
    }
    else if (opts.nextSlide >= els.length) {
        opts.nextSlide = 0;
    }
    go(els, opts, 1, val>=0);
    return false;
};

$j.fn.cycle.custom = function(curr, next, opts, cb) {
    var $jl = $j(curr), $jn = $j(next);
    $jn.css({opacity:0});
    var fn = function() {$jn.animate({opacity:1}, opts.speedIn, opts.easeIn, cb)};
    $jl.animate({opacity:0}, opts.speedOut, opts.easeOut, function() {
        $jl.css({display:'none'});
        if (!opts.sync) fn();
    });
    if (opts.sync) fn();
};

$j.fn.cycle.transitions = {
    fade: function($jcont, $jslides, opts) {
        $jslides.not(':eq(0)').css('opacity',0);
        opts.before.push(function() { $j(this).show() });
    }
};

$j.fn.cycle.ver = function() { return ver; };

// @see: http://malsup.com/jquery/cycle/lite/
$j.fn.cycle.defaults = {
    timeout:       4000, 
    speed:         1000, 
    next:          null, 
    prev:          null, 
    before:        null, 
    after:         null, 
    height:       'auto',
    sync:          1,    
    fit:           0,    
    pause:         0,    
    delay:         0,    
    slideExpr:     null  
};

})(jQuery);