Glider = Class.create();
Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
    initialize: function (d, b) {
        this.scrolling = false;
        this.clicked = null;
        this.selectedTab = null;
        this.wrapper = $(d);
        this.scroller = this.wrapper.down("div.scroller");
        this.sections = this.wrapper.select("div.section");
        this.options = Object.extend({
            duration: 1,
            frequency: 3
        }, b || {});
        this.sections.each(function (f, e) {
            f._index = e
        });
        this.events = {
            click: this.click.bindAsEventListener(this)
        };
        this.addObservers();
        if (this.options.initialSection) {
            if (this.options.selectedTab) {
                this.selectedTab = $(this.options.selectedTab)
            }
            this.moveTo(this.options.initialSection, this.scroller, {
                duration: this.options.duration
            })
        }
        if (this.options.autoGlide) {
            this.start()
        }
        var a = $(this.options.arrowNext);
        if (a) {
            this.arrowNext = a;
            Event.observe(a, "click", this.next.bindAsEventListener(this));
        }
        var c = $(this.options.arrowPrevious);
        if (c) {
            this.arrowPrevious = this.wrapper.select("div.arrowPrevious");
            Event.observe(c, "click", this.previous.bindAsEventListener(this));
        }
    },
    addObservers: function () {
        var a = this.wrapper.getElementsBySelector("div.controls span.control");
        a.invoke("observe", "click", this.events.click);
    },
    click: function (b) {
        this.stop();
        var a = Event.findElement(b, "a");
        this.selectedTab = a.ancestors()[0];
        if (this.scrolling) {
            this.scrolling.cancel()
        }
        this.moveTo(a.href.split("#")[1], this.scroller, {
            duration: this.options.duration
        });
        Event.stop(b)
    },
    moveTo: function (a, e, c) {
        this.current = $(a);
        Position.prepare();
        var d = Position.cumulativeOffset(e),
            b = Position.cumulativeOffset($(a));
        this.scrolling = new Effect.SmoothScroll(e, {
            duration: c.duration,
            x: (b[0] - d[0]),
            y: (b[1] - d[1])
        });
        this.activeCurrent();
        return false
    },
    activeCurrent: function () {
        if (this.options.activeSection && this.selectedTab != null) {
            this.cleanTabs();
            this.selectedTab.addClassName(this.options.activeSection)
        }
    },
    cleanTabs: function () {
        if (this.selectedTab) {
            var b = this.selectedTab.siblings();
            var c = b.size();
            for (var a = 0; a < c; a++) {
                b[a].removeClassName(this.options.activeSection)
            }
        }
    },
    markNextClicked: function (b, a) {
        if (this.selectedTab) {
            if (b < a) {
                var c = this.selectedTab.nextSiblings();
                this.selectedTab = c[a - b - 1]
            } else {
                if (b > a) {
                    var c = this.selectedTab.previousSiblings();
                    this.selectedTab = c[b - a - 1]
                }
            }
        }
    },
    next: function () {
        this.stop();
        //this.startAgain();
        if (this.current) {
            var b = this.current._index;
            var a = (this.sections.length - 1 == b) ? 0 : b + 1
        } else {
            var a = 1
        }
        this.markNextClicked(b, a);
        this.moveTo(this.sections[a], this.scroller, {
            duration: this.options.duration
        })
    },
    previous: function () {
        this.stop();
        //this.startAgain();
        if (this.current) {
            var a = this.current._index;
            var b = (a == 0) ? this.sections.length - 1 : a - 1
        } else {
            var b = this.sections.length - 1
        }
        this.markNextClicked(a, b);
        this.moveTo(this.sections[b], this.scroller, {
            duration: this.options.duration
        })
    },
    stop: function () {
        clearTimeout(this.timer)
    },
    start: function () {
        this.periodicallyUpdate()
    },
    startAgain: function() {
        if(this.timer != null) {
            clearTimeout(this.timer);
        } 
        this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 3000);
    },
    periodicallyUpdate: function () {
        if (this.timer != null) {
            clearTimeout(this.timer);
            this.next()
        }
        this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 1000)
    }
});
Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
    initialize: function (a) {
        this.element = $(a);
        var b = Object.extend({
            x: 0,
            y: 0,
            mode: "absolute"
        }, arguments[1] || {});
        this.start(b)
    },
    setup: function () {
        if (this.options.continuous && !this.element._ext) {
            this.element.cleanWhitespace();
            this.element._ext = true;
            this.element.appendChild(this.element.firstChild)
        }
        this.originalLeft = this.element.scrollLeft;
        this.originalTop = this.element.scrollTop;
        if (this.options.mode == "absolute") {
            this.options.x -= this.originalLeft;
            this.options.y -= this.originalTop
        }
    },
    update: function (a) {
        this.element.scrollLeft = this.options.x * a + this.originalLeft;
        this.element.scrollTop = this.options.y * a + this.originalTop
    }
});
