/**
* jquery.simpleshow. A really simple slideshow that does exactly that.
* Copyright (c) 2009 Liam Gooding
* http://goodingsmedia.com/labs/simpleshow/
* Dual licensed under MIT and GPL 3 licenses
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-3.0.html
* This copyright notice must remain intact.
* Launch  : April 2009
* Version : 0.2.0 - April 18th 2009
*/
(function ($) {
    $.fn.simpleshow = function (b) {
        var c = $.extend({},
        $.fn.simpleshow.defaults, b);
        var d = this;
        var e = null;
        d.css({
            position: 'relative'
        });
        d.children().each(function () {
            $(this).fadeOut(0);
            $(this).css({
                position: 'absolute',
                top: 0,
                left: 0
            })
        });
        var f = d.children('.active');
        if (f.length == 0) {
            f = d.children(':first');
            f.addClass('active')
        }
        if (c.pagination != null) {
            $(c.pagination).children(':first').addClass('active')
        }
        f.fadeIn(0);
        $.extend(this, {
            start: function () {
                if (e == null) {
                    e = setInterval(function () {
						var f = d.children('.active');
                        _switchSlide(d, c)
                    },
                    c.speed)
                }
            },
            pause: function (a) {
                clearInterval(e);
                e = null
            },
            next: function () {
                this.pause();
                _switchSlide(d, c);
                this.start()
            },
            prev: function () {
                this.pause();
                _switchSlide(d, c, 'prev');
                this.start()
            },
            goTo: function (a) {
                this.pause();
                _switchSlide(d, c, null, a);
                this.start()
            }
        });
        return this
    };
    function _switchSlide(a, b, c, d) {
        var c = (c == null) ? "next" : c;
        if (b.pagination != null) {
            var e = $(b.pagination);
            var f = e.children('.active');
            if (d != null) {
                $nextPagi = e.children().eq(d)
            } else { if (c == 'next') {
                    $nextPagi = f.next().length ? f.next() : e.children(':first')
                } else if (c == 'prev') {
                    $nextPagi = f.prev().length ? f.prev() : e.children(':last')
                }
            }
            f.removeClass('active');
            $nextPagi.addClass('active')
        }
        var g = a.children('.active');
        var h = null;
        if (d != null) {
            h = a.children().eq(d)
        } else { if (c == 'next') {
                h = g.next().length ? g.next() : a.children(':first')
            } else if (c == 'prev') {
                h = g.prev().length ? g.prev() : a.children(':last')
            }
        }
        g.fadeOut(b.fadeSpeed, function () {
            g.removeClass('active')
        });
        h.fadeIn(b.fadeSpeed, function () {
            h.addClass('active')
			// If the slideshow laggs and start displaying 2 pictures it inactivate the first one
			if (h.length == 2) {
				i = a.children('.active:first');
				i.removeClass('active')
				i.css({
					display: 'none'
				})
			}
        })
    }
    $.fn.simpleshow.defaults = {
        speed: 5000,
        childType: 'img',
        fadeSpeed: 1000,
        pagination: null
    }
})(jQuery);
