var startMultiteaser = new Class({
    activeTeaser: null,
    wrapper: {},
    scroller: {},
    vor: {},
    zurueck: {},
    left: {},
    teaserStep: 3,
    pixelStep: 237,

    initialize: function(activeTeaser, teaserStep, pixelStep) {
        // Buttons, Inhalt
        var first, last = null;
        this.teaserStep = teaserStep;
        this.pixelStep = pixelStep;
        $$('#multiteaser .kopf li a').each(function(el) {
            var _this = this;
            var li = el.getParent();
            li.set('class', 'but-inaktiv');
            var index = li.id.replace(/multiteaser-button-/g, '');
            if((this.activeTeaser == null) && (index == activeTeaser)) {
                this.activeTeaser = index;
            };

            $('multiteaser-inhalt-' + index).setStyle('display', 'none');

            el.addEvent('click', function(e) {
                el.blur();
                e.preventDefault();
                if(this.getParent().id.replace(/multiteaser-button-/g, '') != _this.activeTeaser) {
                    _this.toggleTeaser(index, true);
                };
            });

            last = li;
            if(first == null) {
                first = index;
            };
        }.bind(this));

        if(last != null) {
            last.set('class', 'but-inaktiv');
        };

        if(this.activeTeaser != null) {
            $('multiteaser-button-' + this.activeTeaser).set('class', $('multiteaser-button-' + this.activeTeaser).get('class').replace(/-inaktiv/g, '-aktiv'));
            $('multiteaser-inhalt-' + this.activeTeaser).setStyle('display', 'block');
        } else {
            this.activeTeaser = first;
        };

        // Scroller
        $$('#multiteaser .inhalt .multiteaser-scroll').each(function(el) {
            var index = el.id.replace(/multiteaser-inhalt-/g, '');
            this.wrapper[index] = el.getElement('.scroll-wrapper');
            this.scroller[index] = el.getElement('.scroll');
            this.vor[index] = el.getElement('.scroll-vor');
            this.zurueck[index] = el.getElement('.scroll-zurueck');
            this.left[index] = 0;
            this.vor[index].setStyles({'visibility': 'visible', 'cursor': 'pointer'});
            this.vor[index].set('title', trans.getTranslation('scrollForward'));
            this.vor[index].addEvent('mousedown', function() {
                this.scroll(index, -this.teaserStep);
            }.bind(this));
            this.zurueck[index].setStyles({'visibility': 'visible', 'cursor': 'pointer'});
            this.zurueck[index].set('title', trans.getTranslation('scrollBackward'));
            this.zurueck[index].addEvent('mousedown', function() {
                this.scroll(index, this.teaserStep);
            }.bind(this));
        }.bind(this));

        if((this.wrapper[this.activeTeaser].getSize().x >= this.scroller[this.activeTeaser].getSize().x) && !this.vor[this.activeTeaser].hasClass('vor-inaktiv')) {
            this.vor[this.activeTeaser].addClass('vor-inaktiv');
            this.vor[this.activeTeaser].setStyle('cursor', 'default');
            this.vor[this.activeTeaser].set('title', '');
            this.zurueck[this.activeTeaser].addClass('zurueck-inaktiv');
            this.zurueck[this.activeTeaser].setStyle('cursor', 'default');
            this.zurueck[this.activeTeaser].set('title', '');
        };

        if(Browser.Engine.trident4) {
            $('multiteaser-inhalt-' + this.activeTeaser).setStyles({
                'display': 'block',
                'opacity' : 1
            });
        };
    },

    toggleTeaser: function(teaser, crossfade) {
        $('multiteaser-button-' + this.activeTeaser).set('class', $('multiteaser-button-' + this.activeTeaser).get('class').replace(/-aktiv/g, '-inaktiv'));
        $('multiteaser-button-' + teaser).set('class', $('multiteaser-button-' + teaser).get('class').replace(/-inaktiv/g, '-aktiv'));
        if(!crossfade) {
            $('multiteaser-inhalt-' + this.activeTeaser).setStyle('display', 'none');
            $('multiteaser-inhalt-' + teaser).setStyles({
                'display': 'block',
                'opacity': 1
            });
            this.activeTeaser = teaser;
        } else {
            var fader = new Fx.Tween($('multiteaser-inhalt-' + this.activeTeaser), {
                'duration': 250,
                onComplete: function() {
                    $('multiteaser-inhalt-' + this.activeTeaser).setStyle('display', 'none');
                    var temp = $('multiteaser-inhalt-' + teaser);
                    temp.setStyles({
                        'display': 'block',
                        'opacity' : 0.01
                    });
                    temp.set('tween', {duration: 250});
                    temp.fade('in');
                    this.activeTeaser = teaser;

                    if((this.wrapper[teaser].getSize().x >= this.scroller[teaser].getSize().x) && !this.vor[teaser].hasClass('vor-inaktiv')) {
                        this.vor[teaser].addClass('vor-inaktiv');
                        this.vor[teaser].setStyle('cursor', 'default');
                        this.vor[teaser].set('title', '');
                        this.zurueck[teaser].addClass('zurueck-inaktiv');
                        this.zurueck[teaser].setStyle('cursor', 'default');
                        this.zurueck[teaser].set('title', '');
                    };
                }.bind(this)
            });

            fader.start('opacity', 0);
        };
    },

    scroll: function(index, teaserStep) {
        var oldLeft = this.left[index];
        var newLeft = oldLeft + (this.pixelStep * teaserStep);
        var limes = this.wrapper[index].getSize().x - this.scroller[index].getSize().x;
        newLeft = Math.min(Math.max(newLeft, limes), 0);
        this.left[index] = newLeft;
        var duration = Math.abs(Math.round(250 * (oldLeft - newLeft) / this.pixelStep));
        this.scroller[index].set('tween', {'duration': duration});
        this.scroller[index].tween('margin-left', this.left[index]);
    }
});


var startModellteaser = new Class({
    activeTeaser: null,
    wrapper: {},
    scroller: {},
    vor: {},
    zurueck: {},
    left: {},
    teaserStep: 3,
    pixelStep: 237,

    initialize: function(activeTeaser, teaserStep, pixelStep) {
        // Buttons, Inhalt
        var first, last = null;
        this.teaserStep = teaserStep;
        this.pixelStep = pixelStep;
        $$('#modellteaser .kopf li a').each(function(el) {
            var _this = this;
            var li = el.getParent();
            li.set('class', 'but-inaktiv');
            var index = li.id.replace(/modellteaser-button-/g, '');
            if((this.activeTeaser == null) && (index == activeTeaser)) {
                this.activeTeaser = index;
            };

            $('modellteaser-inhalt-' + index).setStyle('display', 'none');

            el.addEvent('click', function(e) {
                el.blur();
                e.preventDefault();
                if(this.getParent().id.replace(/modellteaser-button-/g, '') != _this.activeTeaser) {
                    _this.toggleTeaser(index, true);
                };
            });

            last = li;
            if(first == null) {
                first = index;
            };
        }.bind(this));

        if(last != null) {
            last.set('class', 'but-inaktiv');
        };

        if(this.activeTeaser != null) {
            $('modellteaser-button-' + this.activeTeaser).set('class', $('modellteaser-button-' + this.activeTeaser).get('class').replace(/-inaktiv/g, '-aktiv'));
            $('modellteaser-inhalt-' + this.activeTeaser).setStyle('display', 'block');
        } else {
            this.activeTeaser = first;
        };

        // Scroller
        $$('#modellteaser .inhalt .multiteaser-scroll').each(function(el) {
            var index = el.id.replace(/modellteaser-inhalt-/g, '');
            this.wrapper[index] = el.getElement('.scroll-wrapper');
            this.scroller[index] = el.getElement('.scroll');
            this.vor[index] = el.getElement('.scroll-vor');
            this.zurueck[index] = el.getElement('.scroll-zurueck');
            this.left[index] = 0;
            this.vor[index].setStyles({'visibility': 'visible', 'cursor': 'pointer'});
            this.vor[index].set('title', trans.getTranslation('scrollForward'));
            this.vor[index].addEvent('mousedown', function() {
                this.scroll(index, -this.teaserStep);
            }.bind(this));
            this.zurueck[index].setStyles({'visibility': 'visible', 'cursor': 'pointer'});
            this.zurueck[index].set('title', trans.getTranslation('scrollBackward'));
            this.zurueck[index].addEvent('mousedown', function() {
                this.scroll(index, this.teaserStep);
            }.bind(this));
        }.bind(this));

        if((this.wrapper[this.activeTeaser].getSize().x >= this.scroller[this.activeTeaser].getSize().x) && !this.vor[this.activeTeaser].hasClass('vor-inaktiv')) {
            this.vor[this.activeTeaser].addClass('vor-inaktiv');
            this.vor[this.activeTeaser].setStyle('cursor', 'default');
            this.vor[this.activeTeaser].set('title', '');
            this.zurueck[this.activeTeaser].addClass('zurueck-inaktiv');
            this.zurueck[this.activeTeaser].setStyle('cursor', 'default');
            this.zurueck[this.activeTeaser].set('title', '');
        };

        if(Browser.Engine.trident4) {
            $('modellteaser-inhalt-' + this.activeTeaser).setStyles({
                'display': 'block',
                'opacity' : 1
            });
        };
    },

    toggleTeaser: function(teaser, crossfade) {
        $('modellteaser-button-' + this.activeTeaser).set('class', $('modellteaser-button-' + this.activeTeaser).get('class').replace(/-aktiv/g, '-inaktiv'));
        $('modellteaser-button-' + teaser).set('class', $('modellteaser-button-' + teaser).get('class').replace(/-inaktiv/g, '-aktiv'));
        if(!crossfade) {
            $('modellteaser-inhalt-' + this.activeTeaser).setStyle('display', 'none');
            $('modellteaser-inhalt-' + teaser).setStyles({
                'display': 'block',
                'opacity': 1
            });
            this.activeTeaser = teaser;
        } else {
            var fader = new Fx.Tween($('modellteaser-inhalt-' + this.activeTeaser), {
                'duration': 250,
                onComplete: function() {
                    $('modellteaser-inhalt-' + this.activeTeaser).setStyle('display', 'none');
                    var temp = $('modellteaser-inhalt-' + teaser);
                    temp.setStyles({
                        'display': 'block',
                        'opacity' : 0.01
                    });
                    temp.set('tween', {duration: 250});
                    temp.fade('in');
                    this.activeTeaser = teaser;

                    if((this.wrapper[teaser].getSize().x >= this.scroller[teaser].getSize().x) && !this.vor[teaser].hasClass('vor-inaktiv')) {
                        this.vor[teaser].addClass('vor-inaktiv');
                        this.vor[teaser].setStyle('cursor', 'default');
                        this.vor[teaser].set('title', '');
                        this.zurueck[teaser].addClass('zurueck-inaktiv');
                        this.zurueck[teaser].setStyle('cursor', 'default');
                        this.zurueck[teaser].set('title', '');
                    };
                }.bind(this)
            });

            fader.start('opacity', 0);
        };
    },

    scroll: function(index, teaserStep) {
        var oldLeft = this.left[index];
        var newLeft = oldLeft + (this.pixelStep * teaserStep);
        var limes = this.wrapper[index].getSize().x - this.scroller[index].getSize().x;
        newLeft = Math.min(Math.max(newLeft, limes), 0);
        this.left[index] = newLeft;
        var duration = Math.abs(Math.round(250 * (oldLeft - newLeft) / this.pixelStep));
        this.scroller[index].set('tween', {'duration': duration});
        this.scroller[index].tween('margin-left', this.left[index]);
    }
});


var startAktuellTeaser = new Class({
    wrapper: [],
    scroller: [],
    navWrap:[],
    navForward: [],
    navBackward: [],
    steps: [],
    step: [],

    initialize: function() {
        var _this = this;
        var idx = 0;
        $$('.aktuell').each(function(el) {
            var wParent  = el.getSize().x;
            var wrapper  = el.getElement('div.aktuell-wrapper');
            var scroller = el.getElement('ul.teaser-block');
            var navWrap  = el.getElement('h3');
            if($chk(wrapper) && $chk(scroller) && $chk(navWrap)) {
                var steps = [0];
                var wSum = 0;
                var mmax = 0;
                scroller.getChildren('li').each(function(el) {
                    var mr = el.getStyle('margin-right').toInt();
                    mmax   = Math.max(mmax, mr);
                    wSum  += el.getSize().x + mr;
                    steps.push(wSum * -1);
                });

                if(wSum > (wParent + mmax)) {
                    var limes = (wSum - wParent + mmax) * -1;
                    steps = steps.filter(function(item){
                        return item >= limes;
                    });

                    _this.wrapper[idx]  = wrapper;
                    _this.scroller[idx] = scroller;
                    _this.navWrap[idx]  = navWrap;
                    _this.steps[idx]    = steps;
                    _this.step[idx]     = 0;
                    _this.init(idx, wSum);
                    idx++;
                };
            };
        });
    },

    init: function(idx, breite) {
        var _this = this;
        this.scroller[idx].setStyle('width', breite + 'px');

        var wrap = new Element('span', {
            'class': 'scroll-wrap'
        }).inject(this.navWrap[idx]);

        this.navBackward[idx] = new Element('span', {
            'class': 'scroll-zurueck'
        }).inject(wrap);

        this.navBackward[idx].set('title', trans.getTranslation('scrollBackward'));
        this.navBackward[idx].addEvents({
            'mousedown': function() {
                _this.scroll(idx, -1);
            }
        });

        this.navForward[idx] = new Element('span', {
            'class': 'scroll-vor'
        }).inject(wrap);

        this.navForward[idx].set('title', trans.getTranslation('scrollForward'));
        this.navForward[idx].addEvents({
            'mousedown': function() {
                _this.scroll(idx, 1);
            }
        });
    },

    scroll: function(idx, value) {
        var delta = this.steps[idx][this.step];
        var step  = this.step[idx] + value;
        step = Math.max(Math.min(step, this.steps[idx].length - 1), 0);
        if(step != this.step) {
            delta -= this.steps[idx][step];
            this.scroller[idx].set('tween', {'duration': Math.abs(delta)});
            this.scroller[idx].tween('margin-left', this.steps[idx][step]);
            this.step[idx] = step;
        };
    }
});
