﻿/**
 * jQuery ButtonBlink
 *
 * Blinks a button for a certain amount of time, after which it stops blinking but the background-color
 * remains in a changed state.  If the button is clicked, the background-color is restored.
 */
(function($) {
    $.fn.buttonBlink = function(options) {
        if (options)
            $.extend(settings, options);

        var settings = {
            blinkRate: 750,
            blinkColor: '#E17009',
            blinkLimit: 10,
            blinkTextColor: 'white'
        };

        return this.each(function() {
            var $this = $(this); 
            var blinked = false;
            var originalColor = $this.css('background-color');            
            var originalTextColor = $this.css('color');
            var count = 1;

            blink();

            function blink() {
                if (blinked) {
                    $this.css('background-color', originalColor);
                    $this.css('color', originalTextColor);
                }
                else {
                    count++;

                    $this.css('background-color', settings.blinkColor);
                    $this.css('color', settings.blinkTextColor);

                    if (settings.blinkLimit != -1 && count >= settings.blinkLimit) {
                        clearInterval(interval);
                    }
                }
                blinked = !blinked;
            }

            var interval = setInterval(blink, settings.blinkRate);

            $this.click(function() {
                clearInterval(interval);
                
                $this.css('background-color', originalColor);
                $this.css('color', originalTextColor);
            });
        });
    };
})(jQuery);
