var sync_container = null;

function Slideshow(container, delay_secs) {
    this.fadeOut = true;
    if (!sync_container)
        sync_container = this;

    this.startTimer = function(long_delay) {
        this.fading = false;
        var obj = this;
        this.timer = setTimeout(function() {
            obj.next(false);
        },
        (long_delay ? this.startdelay: this.delay));
    };

    this.prev = function() {
        if (this.fading) {
            return;
        }
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
        var next_pos = this.position - 1;

        this.fading = true;
        var obj = this;
        if (next_pos < 0) {
            if (this.fadeOut) {
                $(this.slides[0]).fadeOut('slow');
            }
            $(this.slides[this.count - 1]).fadeIn('slow',
            function() {
                for (var i = 1; i < Slideshow.count - 1; i++) {
                    $(obj.slides[i]).show();
                }
                obj.startTimer();
            });
            this.position = this.count - 1;
        } else {
            if (this.fadeOut) {
                $(this.slides[next_pos]).fadeIn('slow');
            } else {
                $(this.slides[next_pos]).show();
            }
            $(this.slides[this.position]).fadeOut('slow',
            function() {
                obj.startTimer(true);
            });
            this.position = next_pos;
        }
    };

    this.next = function(manual) {
        if (this.fading) {
            return;
        }
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }

        var next_pos;
        if (this.random) {
            if (this.count > 1) {
                if (this == sync_container) {
              //  while (this.position == next_pos) {
                  do {
                    next_pos = parseInt(Math.random()*this.count);
                 }while(this.position == next_pos);
            //    }
            } else {
                next_pos = sync_container.position;
            }
            } else {
                next_pos = this.position;
            }
        } else {
            next_pos = this.position + 1;
        }
        this.fading = true;
        if (next_pos == this.count) {
            for (var i = 1; i < this.count - 1; i++) {
                $(this.slides[i]).hide();
            }
            if (this.fadeOut) {
                $(this.slides[0]).fadeIn('slow');
            }
            var obj = this;
            $(this.slides[this.count - 1]).fadeOut('slow',
            function() {
                obj.startTimer(manual);
            });
            this.position = 0;
        } else {
            var obj = this;
            $(this.slides[next_pos]).fadeIn('slow',
            function() {
                obj.startTimer(manual);
            });
            if (this.fadeOut) {
                $(this.slides[this.position]).fadeOut('slow');
            }
            this.position = next_pos;
        }
    };


    this.random = (delay_secs >= 100);
    this.position = (this.random) ? start_slide: 0;
    this.count = 0;
    this.container = container;
    this.slides = null;
    this.timer = null;
    this.delay = this.random ? (delay_secs - 100) * 1000: delay_secs * 1000;
    this.startdelay = this.delay;
    this.fading = false;

    this.slides = container.children();
    this.count = this.slides.length;
    this.container.css('position', 'relative')
    for (var i = 0; i < this.count; i++) {

        $(this.slides[i]).css('position', 'absolute');
        $(this.slides[i]).css('z-index', i);
        if (i != this.position)
        $(this.slides[i]).hide();
    }
    this.startTimer(true);
}
