﻿jQuery.fn.glow = function(options) {

    var settings = $.extend({}, options);

    return this.each(function() {

        var origColor = $(this).css('color');

        var words = $(this).text().split("");

        var text = words.join("</span><span>");

        $(this).html("<span>" + text + "</span>");

        var letters = $(this).find('span');

        function glowAll(elems) {

            var i = -1;

            if ($.browser.msie) {


                function next() {

                    i = i + 1;

                    if (i < elems.length)

                        $(elems[i]).delay(0).animate({

                            color: options.glowColor

                        }, 10, next).prev().animate({

                            color: origColor

                        }, 1000).end().last().animate({

                            color: origColor

                        }, 1000);

                    // I tried setting the ie filter glow here but the result were not
                    // nice. Have a go if you like and let me know if you get better results.
                }
            }

            else {

                function next() {

                    i = i + 1;

                    if (i < elems.length)

                        $(elems[i]).delay(0).animate({

                            color: options.glowColor

                        }, 40, next).css({ 'text-shadow': '0 0 50px #FFFFFF' }).prev().animate({

                            color: origColor

                        }, 2000).css({ 'text-shadow': 'none' }).end().last().animate({

                            color: origColor

                        }, 800, function() { $(this).css({ 'text-shadow': 'none' }) });
                }
            }

            next();
        }


        function spark() {

            function createInterval() {

                var interval = window.setInterval(function() { sparkle(); }, 1);

            }

            function sparkle() {
                letters.eq(Math.round(Math.random() * letters.size())).animate({ color: options.glowColor }, 300, function() { $(this).animate({ color: origColor }) });
            }
            createInterval();
        }


        if (options.glow) { glowAll(letters); }
        else if (options.sparkle) { spark(); }



    });
};
