Scroller = new Class({
    Implements: Options,
    options: {
        duration: 500,
        selectBy: "child",
        autostart: false,
        autostart_dir: "forward",
        sleep: 1000,
        transition: "sine:in:out",
        leftm: 0,
        topm: 0
    },
    length: 0,
    initialize: function (windowElement, options) {
        this.setOptions(options);
        this.windowElement = windowElement;
        this.slideStrip = windowElement.getChildren()[0];
        this.cChildIdx = 0;
        this.fx = new Fx.Scroll(windowElement, { duration: this.options.duration, link: 'cancel', transition: this.options.transition });
        if (this.options.selectBy == "class") {
            this.childs = this.slideStrip.getElements(".slide");
        } else {
            this.childs = this.slideStrip.getChildren();
        }
        this.length = this.childs.length;
        var myClass = this;

        this.childs.addEvent('mouseenter', function () { myClass.stop(); }).addEvent('mouseleave', function () { myClass.restart(); });
        
        this.periodicalID = 0;
        this.startdir = false;
        if (this.options.autostart == true) {
            if (this.options.autostart_dir != "back") {
                this.periodicalID = this.next.periodical(this.options.sleep + this.options.duration, this);
            } else {
                this.startdir = true;
                this.periodicalID = this.prev.periodical(this.options.sleep + this.options.duration, this);
            }
        }
    },
    updateTransition: function (opts) {
        this.setOptions(opts);
        this.fx.options.duration = this.options.duration;
        this.fx.options.transition = this.options.transision;
        if (this.periodicalID)
            this.restart();
    },
    stop: function () {
        if (this.periodicalID) {
            $clear(this.periodicalID);
            this.periodicalID = 0;
        }
    },
    start: function (back) {
        if (this.periodicalID) {
            $clear(this.periodicalID);
        }
        if (back) {
            this.startdir = true;
            this.periodicalID = this.prev.periodical(this.options.sleep + this.options.duration, this);
        } else {
            this.startdir = false;
            this.periodicalID = this.next.periodical(this.options.sleep + this.options.duration, this);
        }
    },
    next: function (stop) {
        if (stop) this.stop();
        this.cChildIdx++;
        if (this.cChildIdx >= this.childs.length) { this.cChildIdx = this.childs.length - 1; if (this.options.autostart == true) { this.startdir = true; this.restart(); } }
        else { this.gotoIdx(); }
    },
    restart: function () {
        this.stop();
        this.start(this.startdir);
    },
    prev: function (stop) {
        if (stop) this.stop();
        this.cChildIdx--;
        if (this.cChildIdx < 0) { this.cChildIdx = 0; if (this.options.autostart == true) { this.startdir = false; this.restart(); } }
        else { this.gotoIdx(); }
    },
    gotoIdx: function () {
        var s = this.childs[this.cChildIdx];
        var bl = s.getStyle('border-left').toInt();
        if (isNaN(bl)) bl = 0;
        var bt = s.getStyle('border-top').toInt();
        if (isNaN(bt)) bt = 0;
        this.curTrans = this.fx.start(
						s.getCoordinates(this.windowElement)['left'] - bl - this.options.leftm,
						s.getCoordinates(this.windowElement)['top'] - bt - this.options.topm
						);
    }
});

