/**
 * RokNewsFlash Module
 *
 * @package		Joomla
 * @subpackage	RokNewsFlash Module
 * @copyright Copyright (C) 2009 RocketTheme. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see RT-LICENSE.php
 * @author RocketTheme, LLC
 *
 */

var RokNewsFlash = new Class({
    Implements: [Options, Events],
    version: 1.2,
    options: {
        controls: true,
        delay: 2000,
        duration: 800
    },
    initialize: function (b, c) {
        this.setOptions(c);
        this.element = document.id(b) || null;
        this.element.addEvents({
            'mouseenter': this.stop.bind(this),
            'mouseleave': this.play.bind(this)
        });
        this.news = this.element.getElements('ul li');
        this.current = 0;
        this.fx = [];
        this.news.getParent().setStyle('position', 'relative');
        var d = this;
        this.news.each(function (a, i) {
            a.setStyle('position', 'absolute');
            this.fx[i] = new Fx.Tween(a, {
                duration: this.options.duration,
                onStart: function () {
                    d.transitioning = true
                },
                onComplete: function () {
                    d.transitioning = false
                }
            }).set('opacity', 1);
            if (!i) return;
            a.setStyle('opacity', 0)
        }, this);
        if (this.options.controls) this.addControls();
        this.status = 'stop';
        window.addEvent('domready', this.play.bind(this));
        return this
    },
    addControls: function () {
        var a = new Element('div', {
            'class': 'controls'
        }).inject(this.element, 'top');
        this.arrowPrev = new Element('div', {
            'class': 'control-prev'
        //}).inject(a).set('html', '<span>&lt;-</span>');
        }).inject(a).set('html', '');
        this.arrowNext = new Element('div', {
            'class': 'control-next'
        //}).inject(a).set('html', '<span>-&gt;</span>');
        }).inject(a).set('html', '');
        this.arrowPrev.addEvent('click', this.previous.bind(this));
        this.arrowNext.addEvent('click', this.next.bind(this));
        this.arrowPrev.addEvents({
            'mouseenter': function () {
                this.addClass('control-prev-hover').setStyle('cursor' , 'pointer')
            },
            'mouseleave': function () {
                this.removeClass('control-prev-hover').removeClass('control-prev-down')
            },
            'mousedown': function () {
                this.addClass('control-prev-down')
            },
            'mouseup': function () {
                {
                    this.removeClass('control-prev-down')
                }
            }
        });
        this.arrowNext.addEvents({
            'mouseenter': function () {
                this.addClass('control-next-hover').setStyle('cursor' , 'pointer')
            },
            'mouseleave': function () {
                this.removeClass('control-next-hover').removeClass('control-next-down')
            },
            'mousedown': function () {
                this.addClass('control-next-down')
            },
            'mouseup': function () {
                {
                    this.removeClass('control-next-down')
                }
            }
        });
        return this
    },
    previous: function () {
        if (this.transitioning) return this;
        var a = (!this.current) ? this.news.length - 1 : this.current - 1;
        this.fx[this.current].start('opacity', 0);
        this.fx[a].start('opacity', 1);
        this.current = a;
        return this
    },
    next: function () {
        if (this.transitioning) return this;
        var a = (this.current == this.news.length - 1) ? 0 : this.current + 1;
        this.fx[this.current].start('opacity', 0);
        this.fx[a].start('opacity', 1);
        this.current = a;
        return this
    },
    play: function () {
        if (this.status == 'play') return this;
        this.status = 'play';
        this.timer = this.next.periodical(this.options.delay + this.options.duration, this);
        return this
    },
    stop: function () {
        this.status = 'stop';
        $clear(this.timer);
        return this
    }
});

