/*
*   Author: Dhaval Tawar
*   jquery.wolfblink.js
*   Blink any object in dom
*   Keep max = 0 for unstoppable blinking and stop it using stopBlinking
*/

; (function ($) {
    jQuery.fn.blink = function (options) {
        var defaults = {
            max: 1,
            minOpacity: 0.3,
            speed: 1000,
            keepBlinking: true
        }

        var opt = $.extend({}, defaults, options);
        var me = $(this);
        var cnt = 0;

        startBlinking();

        function startBlinking() {
            if (opt.keepBlinking) {
                me.animate({ opacity: opt.minOpacity }, opt.speed, function () {
                    me.animate({ opacity: 1 }, opt.speed, function () {
                        cnt++;
                        if (cnt != opt.max) startBlinking();
                        else callback();
                    });
                });
            }
        }

        function callback() {
            if(opt.complete && typeof opt.complete == "function")
                opt.complete();
        }

        var blink = {
            stopBlinking: function () {
                opt.keepBlinking = false;
                callback();
            }
        }

        return blink;
    };
})(jQuery);
