var SlideShow = new Class({
    getOptions: function () {
        return {
            effect: "fade",
            duration: 2000,
            transition: Fx.Transitions.linear,
            direction: "right",
            color: false,
            wait: 5000,
            loop: false,
            thumbnails: false,
            thumbnailCls: "outline",
            backgroundSlider: false,
            loadingCls: "loading",
            onClick: false
        }
    },
    initialize: function (B, A, CAP, C) {
        this.setOptions(this.getOptions(), C);
        this.container = $(B);
        this.container.setStyles({
            position: "relative",
            overflow: "hidden"
        });
		this.captions = $(CAP);	 //felicia
        if (this.options.onClick) {
            this.container.addEvent("click", function () {
                this.options.onClick(this.imageLoaded)
            }.bind(this))
        }
        this.imagesHolder = new Element("div").setStyles({
            position: "absolute",
            overflow: "hidden",
            top: this.container.getStyle("height"),
            left: 0,
            width: "0px",
            height: "0px",
            display: "none"
        }).injectInside(this.container);
        if ($type(A) == "string" && !this.options.thumbnails) {
            var E = [];
			var FE = [];
            $$("." + A).each(function (F) {
                E.push(F.src);
				FE.push(F.title);	 //felicia
                F.injectInside(this.imagesHolder)
            },
            this);
            this.images = E;
			this.titles = FE;	 //felicia
        } else {
            if ($type(A) == "string" && this.options.thumbnails) {
                var E = [];
                var D = [];
				var FEL = [];
                this.thumbnails = $$("." + A);
                this.thumbnails.each(function (G, F) {
                    D.push(G.href);
					FEL.push(G.title);	 //felicia
                    E.push(G.getElement("img"));
                    G.href = "javascript:;";
                    G.addEvent("click", function () {
                        this.stop();
                        this.play(F)
                    }.bind(this, G, F))
                },
                this);
                this.images = D;
				this.titles = FEL;	 //felicia
                this.thumbnailImages = E;
                if (this.options.backgroundSlider) {
                    this.bgSlider = new BackgroundSlider(this.thumbnailImages, {
                        mouseOver: false,
                        duration: this.options.duration,
                        className: this.options.thumbnailCls,
                        padding: {
                            top: 0,
                            right: -2,
                            bottom: -2,
                            left: 0
                        }
                    });
                    this.bgSlider.set(this.thumbnailImages[0])
                }
            } else {
                this.images = A
            }
        }
        this.loading = new Element("div").addClass(this.options.loadingCls).setStyles({
            position: "absolute",
            top: 0,
            left: 0,
            zIndex: 3,
            display: "none",
            width: this.container.getStyle("width"),
            height: this.container.getStyle("height")
        }).injectInside(this.container);
        this.oldImage = new Element("div").setStyles({
            position: "absolute",
            overflow: "hidden",
            top: 0,
            left: 0,
            opacity: 0,
            width: this.container.getStyle("width"),
            height: this.container.getStyle("height")
        }).injectInside(this.container);
        this.newImage = this.oldImage.clone();
        this.newImage.injectInside(this.container);
        this.timer = 0;
        this.image = -1;
        this.imageLoaded = 0;
        this.stopped = true;
        this.started = false;
        this.animating = false;
		
		this.caption = new Element("p").addClass("captions").injectInside(this.captions);		 //felicia
		
    },
    load: function () {
        $clear(this.timer);
        this.loading.setStyle("display", "block");
        this.image++;
        var A = this.images[this.image];
        delete this.imageObj;
        doLoad = true;
        this.imagesHolder.getElements("img").each(function (B) {
            var C = this.images[this.image];
            if (B.src == C) {
                this.imageObj = B;
                doLoad = false;
                this.add = false;
                this.show()
            }
        },
        this);
        if (doLoad) {
            this.add = true;
            this.imageObj = new Asset.image(A, {
                onload: this.show.bind(this)
            })
        }
    },
    show: function (D) {
        if (this.add) {
            this.imageObj.setProperties({title: this.titles[this.image]}).injectInside(this.imagesHolder); //felicia
        }
        this.newImage.setStyles({
            zIndex: 1,
            opacity: 0
        });
        var B = this.newImage.getElement("img");
        if (B) {
            B.replaceWith(this.imageObj.clone())
        } else {
            var C = this.imageObj.clone();
            C.injectInside(this.newImage)
        }
        this.caption.setHTML(this.titles[this.image]); //felicia
		this.imageLoaded = this.image;
        this.loading.setStyle("display", "none");
        if (this.options.thumbnails) {
            if (this.options.backgroundSlider) {
                var A = this.thumbnailImages[this.image];
                this.bgSlider.move(A);
                this.bgSlider.setStart(A)
            } else {
                this.thumbnails.each(function (F, E) {
                    F.removeClass(this.options.thumbnailCls);
                    if (E == this.image) {
                        F.addClass(this.options.thumbnailCls)
                    }
                },
                this)
            }
        }
        this.effect()
    },
    wait: function () {
        this.timer = this.load.delay(this.options.wait, this)
    },
    play: function (A) {
        if (this.stopped) {
            if (A > -1) {
                this.image = A - 1
            }
            if (this.image < this.images.length) {
                this.stopped = false;
                if (this.started) {
                    this.next()
                } else {
                    this.load()
                }
                this.started = true
            }
        }
    },
    stop: function () {
        $clear(this.timer);
        this.stopped = true
    },
    next: function (A) {
        var B = true;
        if (A && this.stopped) {
            B = false
        }
        if (this.animating) {
            B = false
        }
        if (B) {
            this.cloneImage();
            $clear(this.timer);
            if (this.image < this.images.length - 1) {
                if (A) {
                    this.wait()
                } else {
                    this.load()
                }
            } else {
                if (this.options.loop) {
                    this.image = -1;
                    if (A) {
                        this.wait()
                    } else {
                        this.load()
                    }
                } else {
                    this.stopped = true
                }
            }
        }
    },
    previous: function () {
        if (this.imageLoaded == 0) {
            this.image = this.images.length - 2
        } else {
            this.image = this.imageLoaded - 2
        }
        this.next()
    },
    cloneImage: function () {
        var A = this.oldImage.getElement("img");
        if (A) {
            A.replaceWith(this.imageObj.clone())
        } else {
            var B = this.imageObj.clone();
            B.injectInside(this.oldImage)
        }
        this.oldImage.setStyles({
            zIndex: 0,
            top: 0,
            left: 0,
            opacity: 1
        });
        this.newImage.setStyles({
            opacity: 0
        })
    },
    effect: function () {
        this.animating = true;
        this.effectObj = this.newImage.effects({
            duration: this.options.duration,
            transition: this.options.transition
        });
        var B = ["fade", "wipe", "slide"];
        var D = ["top", "right", "bottom", "left"];
        if (this.options.effect == "fade") {
            this.fade()
        } else {
            if (this.options.effect == "wipe") {
                if (this.options.direction == "random") {
                    this.setup(D[Math.floor(Math.random() * (3 + 1))])
                } else {
                    this.setup(this.options.direction)
                }
                this.wipe()
            } else {
                if (this.options.effect == "slide") {
                    if (this.options.direction == "random") {
                        this.setup(D[Math.floor(Math.random() * (3 + 1))])
                    } else {
                        this.setup(this.options.direction)
                    }
                    this.slide()
                } else {
                    if (this.options.effect == "random") {
                        var C = B[Math.floor(Math.random() * (2 + 1))];
                        if (C != "fade") {
                            var A = D[Math.floor(Math.random() * (3 + 1))];
                            if (this.options.direction == "random") {
                                this.setup(A)
                            } else {
                                this.setup(this.options.direction)
                            }
                        } else {
                            this.setup()
                        }
                        this[C]()
                    }
                }
            }
        }
    },
    setup: function (A) {
        if (A == "top") {
            this.top = -this.container.getStyle("height").toInt();
            this.left = 0;
            this.topOut = this.container.getStyle("height").toInt();
            this.leftOut = 0
        } else {
            if (A == "right") {
                this.top = 0;
                this.left = this.container.getStyle("width").toInt();
                this.topOut = 0;
                this.leftOut = -this.container.getStyle("width").toInt()
            } else {
                if (A == "bottom") {
                    this.top = this.container.getStyle("height").toInt();
                    this.left = 0;
                    this.topOut = -this.container.getStyle("height").toInt();
                    this.leftOut = 0
                } else {
                    if (A == "left") {
                        this.top = 0;
                        this.left = -this.container.getStyle("width").toInt();
                        this.topOut = 0;
                        this.leftOut = this.container.getStyle("width").toInt()
                    } else {
                        this.top = 0;
                        this.left = 0;
                        this.topOut = 0;
                        this.leftOut = 0
                    }
                }
            }
        }
    },
    fade: function () {
        this.effectObj.start({
            opacity: [0, 1]
        });
        this.resetAnimation.delay(this.options.duration + 90, this);
        if (!this.stopped) {
            this.next.delay(this.options.duration + 100, this, true)
        }
    },
    wipe: function () {
        this.oldImage.effects({
            duration: this.options.duration,
            transition: this.options.transition
        }).start({
            top: [0, this.topOut],
            left: [0, this.leftOut]
        });
        this.effectObj.start({
            top: [this.top, 0],
            left: [this.left, 0],
            opacity: [1, 1]
        },
        this);
        this.resetAnimation.delay(this.options.duration + 90, this);
        if (!this.stopped) {
            this.next.delay(this.options.duration + 100, this, true)
        }
    },
    slide: function () {
        this.effectObj.start({
            top: [this.top, 0],
            left: [this.left, 0],
            opacity: [1, 1]
        },
        this);
        this.resetAnimation.delay(this.options.duration + 90, this);
        if (!this.stopped) {
            this.next.delay(this.options.duration + 100, this, true)
        }
    },
    resetAnimation: function () {
        this.animating = false
    }
});
SlideShow.implement(new Options);
SlideShow.implement(new Events);