﻿var BlinkText = new Class({

    //implements
    Implements: [Options, Events],

    //options
    options: {
        from: '#000000',
        to: '#f00000',
        duration: 500,
        times: 3
    },

    //initialization
    initialize: function (el, options) {
        //set options
        this.setOptions(options);
        this.element = $(el);
        this.times = 0;
    },

    //starts the blink effect
    start: function (times) {
        this.element.setStyle('color', this.options.from);

        if (!times) times = this.options.times * 2;
        this.running = 1;
        // Fire start event
        this.fireEvent('start').run(times - 1);
    },

    //stops the blink effect
    stop: function () {
        this.running = 0;
        // Fire stop event
        this.fireEvent('stop');
    },

    run: function (times) {
        //make it happen
        var self = this;
        var new_to = self.element.getStyle('color') == self.options.from ? self.options.to : self.options.from;

        self.fx = new Fx.Tween(self.element, {
            duration: self.options.duration / 2,
            onComplete: function () {
                // Fire tick event
                self.fireEvent('tick');
                if (self.running && times) {
                    self.run(times - 1);
                }
                else {
                    if (self.element.getStyle('color') != self.options.from) self.run(0);
                    self.fireEvent('complete');
                }
            }
        }).start('color', new_to);
    }
});
