//nemTicker.js
//2008 Paul Graves paul.graves@nemisys.uk.com
//Nemisys ticker widget

//nemTicker class
var nemTicker2 = new Class({
    initialize: function(el) {
        this.currentId = 1;
        this.inProgress = false;
        this.ticker = el;
        this.tickerContent = el.getElement(".nemTickerContent");
        this.tickerHolder = new Element("DIV", { 'class': 'nemTickerHolder' });
        this.ticker.grab(this.tickerHolder, 'top');
        this.pause = false;
        //this.ticker.addEvent('mouseover', (function() { this.pause = true; }).bind(this));
        //this.ticker.addEvent('mouseout', (function() { this.pause = false; (function() { this.scrollTicker(); }).delay(3000, this); }).bind(this));
        this.scrollerHeight = 0;
        this.elementWidth = this.tickerContent.getElement('span').getSize().x + this.tickerContent.getElement('span').getStyle('margin-right').toInt() + this.tickerContent.getElement('span').getStyle('margin-left').toInt();
        this.direction = 1;
        this.scrollX = 1;
        this.tickerContent2 = this.tickerContent.clone();
        this.tickerContent3 = this.tickerContent.clone();
        this.tickerHolder.setStyle('left', 0);
        this.tickerContent.setStyle('left', 0);
        this.tickerContent2.setStyle('left', this.tickerContent.getStyle('width').toInt() + this.tickerContent.getStyle('margin-right').toInt());
        this.tickerContent3.setStyle('left', -(this.tickerContent.getStyle('width').toInt() + this.tickerContent.getStyle('margin-right').toInt()));
        var myTransition = new Fx.Transition(Fx.Transitions.Elastic, 1);
        this.scrollFx = new Fx.Tween(this.tickerHolder, { transition: myTransition.easeOut, duration: 3000 });
        this.tickerHolder.grab(this.tickerContent3, 'bottom');
        this.tickerHolder.grab(this.tickerContent, 'bottom');
        this.tickerHolder.grab(this.tickerContent2, 'bottom');

        if (this.ticker.hasClass('nemControls')) {
            this.scrollRight = new Element("DIV", { 'html': 'next', 'class': 'nemScrollRight',
                'events': { 'mousedown': (function() { this.pause = false; this.scrollTicker(); }).bind(this),
                    'mouseup': (function() { this.pause = true; }).bind(this),
                    'mouseover': (function() { this.pause = true; }).bind(this),
                    'mouseout': (function() { this.pause = false; }).bind(this)
                }
            });

            this.scrollRight.inject(this.ticker, 'after');
            this.scrollLeft = new Element("DIV", { 'html': 'previous', 'class': 'nemScrollLeft',
                'events': { 'mousedown': (function() { this.pause = false; this.direction = -1; this.scrollTicker(); }).bind(this),
                    'mouseup': (function() { this.pause = true; this.direction = 1; }).bind(this),
                    'mouseover': (function() { this.pause = true; }).bind(this),
                    'mouseout': (function() { this.pause = false; }).bind(this)
                }
            });
            this.scrollLeft.inject(this.ticker, 'before');
            this.ticker.setStyle('left', this.ticker.getStyle('left').toInt() + this.scrollLeft.getStyle('width').toInt());
            this.scrollRight.setStyle('top', this.ticker.getStyle('top').toInt());
            this.scrollLeft.setStyle('top', this.ticker.getStyle('top').toInt());
            this.scrollLeft.setStyle('left', this.ticker.getStyle('left').toInt() - this.scrollLeft.getStyle('width').toInt() - this.scrollLeft.getStyle('margin-left').toInt() - this.scrollLeft.getStyle('margin-right').toInt());
            this.scrollRight.setStyle('left', this.ticker.getStyle('width').toInt() + this.ticker.getStyle('left').toInt());
        }
        $(document.body).getElements('.promo' + this.currentId).addClass("active");
        //(function() { this.scrollTicker(); }).delay(3000, this);
    },
    scrollTicker: function() {
        if (!this.pause && !this.inProgress) {
            this.inProgress = true;
            this.scrollFx.start('left', this.tickerHolder.getStyle('left').toInt(), this.tickerHolder.getStyle('left').toInt() - (this.direction * this.elementWidth)).chain(
                (function() {
                    // alert(this.ticker.getStyle("width").toInt() + " " + this.tickerHolder.getStyle('left').toInt());
                    // $('result').innerHTML = this.tickerHolder.getStyle('left').toInt() +" "+ -this.ticker.getStyle("width").toInt();
                    if (this.tickerHolder.getStyle('left').toInt() <= -this.tickerContent.getStyle("width").toInt())
                        this.tickerHolder.setStyle('left', 0);

                    if (this.tickerHolder.getStyle('left').toInt() >= this.tickerContent.getStyle("width").toInt()) {
                        this.tickerHolder.setStyle('left', 0);
                    }

                    this.inProgress = false;
                    //(function() { this.scrollTicker(); }).delay(6000, this);
                }).bind(this)
            );
        }
    },
    flashCall: function(id) {
        $(document.body).getElements('.promo' + this.currentId).removeClass("active");
        this.currentId = id;
        $(document.body).getElements('.promo' + this.currentId).addClass("active");

    }
});

