var NewsTicker = new Class({

    Implements: [Options],
    options: {

        autoScroll: true,
        duration: 1000,
        pause: 5000,
        transition: Fx.Transitions.Sine.easeIn,
        direction: 'up', //can be up,down,left,right
        forwardButton: null,
        backwardButton: null
    },

    newsTicker: { // holding the dom elements
        items: null, // holding the items
        overAllFrame: null, // the frame around the scrolled element, working as a mask
        itemGroup: null // the element that is scrolled, contains the items
    },

    initialize: function(options) {

        this.setOptions(options);
        this.newsTicker.overAllFrame = $(this.options.frameElement); // functions as a frame should have specified height and width and overflow hidden
        this.newsTicker.itemGroup = $(this.options.groupElement); // has all items in it
        this.newsTicker.itemGroup.setStyles({
           'position':'relative',
           'top': 0,
           'left': 0
        });

        if(this.options.forwardButton != '' && this.options.forwardButton != null) {

            this.forwardButton = $(this.options.forwardButton);
            this.forwardButton.addEvent('click', function(e) {

                e.preventDefault();
                this.scrollForward();
                this.stopAutoScroll();
				this.startAutoScroll();

            }.bindWithEvent(this));
        }

        if(this.options.backwardButton != '' && this.options.backwardButton != null) {

            this.backwardButton = $(this.options.backwardButton);
            this.backwardButton.addEvent('click', function(e) {

                e.preventDefault();
                this.scrollBackward();
                this.stopAutoScroll();
				this.startAutoScroll();
				
            }.bindWithEvent(this));
        }
        
        this.newsTicker.items = this.newsTicker.itemGroup.getChildren();
        this.newsTicker.currentItem = this.newsTicker.itemGroup.getFirst();
        this.options.itemHeight = this.newsTicker.items[0].getHeight();
        this.options.itemWidth = this.newsTicker.items[0].getWidth();

        if(this.options.autoScroll) {

            this.prepareAutoScroll();
            this.startAutoScroll();
        }
    },

    scrollForward: function() {

        switch(this.options.direction) {

            case 'up':

                this.scrollFx.start({
                    'top': [this.newsTicker.itemGroup.getStyle('top').toInt(), this.newsTicker.itemGroup.getStyle('top').toInt() - this.options.itemHeight]
                }).chain(function() {
                    this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup);
                    this.newsTicker.currentItem = this.newsTicker.itemGroup.getFirst();
                    this.newsTicker.itemGroup.setStyle('top', 0);
                }.bind(this));
                break;

            case 'down':

                this.scrollFx.start({
                    'top': [this.newsTicker.itemGroup.getStyle('top').toInt(), this.newsTicker.itemGroup.getStyle('top').toInt() + this.options.itemHeight]
                }).chain(function() {
                   this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup, 'top') ;
                   this.newsTicker.currentItem = this.newsTicker.itemGroup.getFirst();
                   this.newsTicker.itemGroup.setStyle('top',0);
                }.bind(this));
                break;

            case 'left':

                this.scrollFx.start({
                    'left': [this.newsTicker.itemGroup.getStyle('left').toInt(), this.newsTicker.itemGroup.getStyle('left').toInt() - this.options.itemWidth]
                }).chain(function() {
                   this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup) ;
                   this.newsTicker.currentItem = this.newsTicker.itemGroup.getFirst();
                   this.newsTicker.itemGroup.setStyle('left',0);
                }.bind(this));
                break;

            case 'right':

                this.scrollFx.start({
                    'left': [this.newsTicker.itemGroup.getStyle('left').toInt(), this.newsTicker.itemGroup.getStyle('left').toInt() + this.options.itemWidth]
                }).chain(function() {
                   this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup, 'top') ;
                   this.newsTicker.currentItem = this.newsTicker.itemGroup.getFirst();
                   this.newsTicker.itemGroup.setStyle('left',0);
                }.bind(this));
                break;
        }
    },

    // like scrollForward: just the +/- are changed
    scrollBackward: function() {

        switch(this.options.direction) {

            case 'up':

                this.scrollFx.start({
                    'top': [this.newsTicker.itemGroup.getStyle('top').toInt(), this.newsTicker.itemGroup.getStyle('top').toInt() + this.options.itemHeight]
                }).chain(function() {
                    this.newsTicker.currentItem = this.newsTicker.itemGroup.getLast();
                    this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup,'top');
                    this.newsTicker.itemGroup.setStyle('top', 0);
                }.bind(this));
                break;

            case 'down':

                this.scrollFx.start({
                    'top': [this.newsTicker.itemGroup.getStyle('top').toInt(), this.newsTicker.itemGroup.getStyle('top').toInt() - this.options.itemHeight]
                }).chain(function() {
                   this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup) ;
                   this.newsTicker.currentItem = this.newsTicker.itemGroup.getFirst();
                   this.newsTicker.itemGroup.setStyle('top',0);
                }.bind(this));
                break;

            case 'left':

                this.scrollFx.start({
                    'left': [this.newsTicker.itemGroup.getStyle('left').toInt(), this.newsTicker.itemGroup.getStyle('left').toInt() + this.options.itemWidth]
                }).chain(function() {
                   this.newsTicker.currentItem = this.newsTicker.itemGroup.getLast();
                   this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup) ;
                   this.newsTicker.itemGroup.setStyle('left',0);
                }.bind(this));
                break;

            case 'right':

                this.scrollFx.start({
                    'left': [this.newsTicker.itemGroup.getStyle('left').toInt(), this.newsTicker.itemGroup.getStyle('left').toInt() - this.options.itemWidth]
                }).chain(function() {
                   this.newsTicker.currentItem.dispose().inject(this.newsTicker.itemGroup, 'top') ;
                   this.newsTicker.currentItem = this.newsTicker.itemGroup.getFirst();
                   this.newsTicker.itemGroup.setStyle('left',0);
                }.bind(this));
                break;
        }
    },

    prepareAutoScroll: function() {

        this.scrollFx = new Fx.Morph(this.newsTicker.itemGroup, { transition: this.options.transition, duration: this.options.duration, newsTicker: this});
    },

    startAutoScroll: function() {

        this.timer = this.scrollForward.periodical(this.options.pause, this);
    },

    stopAutoScroll: function() {

        this.timer = $clear(this.timer);
    }
});
