(function ($) {
    $.addSparkles = function (element, options) {
        var defaults = {
            flakeCount: 35
        },
			options = $.extend(defaults, options);

        $(element).data("addSparkles", this);

        var elHeight = $(element).height();
        var elWidth = $(element).width();

        $(window).bind("resize", function () {
            elHeight = $(element).height();
            elWidth = $(element).width();
            $('#sparkle-0').css("left", (elWidth / 2) - 247);
            $('#sparkle-1').css("left", (elWidth / 2) - 47);
            $('#sparkle-2').css("left", (elWidth / 2) + 220);
            //$('#sparkle-3').css("left", (elWidth / 2) + 128);
        });

        function createElements() {
            var sparkle0markup = $(document.createElement("div")).attr({ 'class': 'sparkles', 'id': 'sparkle-0' }).css({ 'width': '38px', 'height': '42px', 'background': 'url(/images/sparkle.png) no-repeat top left', 'position': 'absolute', 'top': 86, "left": (elWidth / 2) - 247, 'fontSize': 0, 'z-index': 999 });
            var sparkle1markup = $(document.createElement("div")).attr({ 'class': 'sparkles', 'id': 'sparkle-1' }).css({ 'width': '38px', 'height': '42px', 'background': 'url(/images/sparkle.png) no-repeat top left', 'position': 'absolute', 'top': 86, "left": (elWidth / 2) - 47, 'fontSize': 0, 'z-index': 999 });
            var sparkle2markup = $(document.createElement("div")).attr({ 'class': 'sparkles', 'id': 'sparkle-2' }).css({ 'width': '38px', 'height': '42px', 'background': 'url(/images/sparkle.png) no-repeat top left', 'position': 'absolute', 'top': 86, "left": (elWidth / 2) + 220, 'fontSize': 0, 'z-index': 999 });
            var sparkle3markup = $(document.createElement("div")).attr({ 'class': 'sparkles', 'id': 'sparkle-3' }).css({ 'width': '38px', 'height': '42px', 'background': 'url(/images/sparkle.png) no-repeat top left', 'position': 'absolute', 'top': 25, "left": (elWidth / 2) + 128, 'fontSize': 0, 'z-index': 999 });
            $(element).append(sparkle0markup);
            $(element).append(sparkle1markup);
            $(element).append(sparkle2markup);
            //$(element).append(sparkle3markup);
        }


        //add sparkles function
        function sparkle() {

            var pause1 = 1500;
            var pause2 = 500;
            var pause3 = 4000;
            var pause4 = 3000;

            var sparkle1 = null;
            var sparkle2 = null;
            var sparkle3 = null;
            var sparkle4 = null;

            $(element).load('nothing',
                function () { sparkle1 = setInterval(hoverfade1, pause1); sparkle2 = setInterval(hoverfade2, pause2); sparkle3 = setInterval(hoverfade3, pause3); sparkle4 = setInterval(hoverfade4, pause4); },
                function () {
                    clearInterval(sparkle1, sparkle2, sparkle3, sparkle4);
                });

            function hoverfade1() {
                $('#sparkle-0').fadeOut(1000).fadeIn(1000);
            }
            function hoverfade2() {
                $('#sparkle-1').fadeOut(1500).fadeIn(1500);
            }
            function hoverfade3() {
                $('#sparkle-2').fadeOut(1200).fadeIn(1200);
            }
            function hoverfade4() {
                $('#sparkle-3').fadeOut(1500).fadeIn(1500);
            }
        }

        createElements();
        sparkle();

    };

    // Initialize the options and the plugin
    $.fn.addSparkles = function (options) {
        if (typeof (options) == "object") {
            return this.each(function (i) {
                (new $.addSparkles(this, options));
            });
        } else if (typeof (options) == "string") {
            return this.each(function (i) {
                var snow = $(this).data('addSparkles');
                if (snow) {
                    snow.clear();
                }
            });
        }
    };
})(jQuery);
