
if (!window.YUKU) var YUKU = { }

YUKU.fireworks =
{
   colors : ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FFFFFF'],
   sizes : [5, 6, 7, 8, 9],
   chars : ['&bull;'],
   num : 40,
   startTimer : -1,
   resetTimer : -1,
   holder : null,
   sparks : [],

   init : function (number, colors, sizes, chars)
   {
      if (colors && colors.length)
         this.colors = colors;

      if (sizes && sizes.length)
         this.sizes = sizes;

      if (chars && chars.length)
         this.chars = chars;

      if (number > 0)
         this.num = number;

      var col = this.colors[Math.floor(Math.random() * this.colors.length)];
      var size = this.sizes[Math.floor(Math.random() * this.sizes.length)] + 'px';
      var char = this.chars[Math.floor(Math.random() * this.chars.length)];

      var holder = YUKU.make_el('DIV', {id : 'firework-holder', style : {position : 'absolute', zIndex : '10050'}}, document.body);

      for (var i = 0; i < this.num; i++)
      {
         var spark = YUKU.make_el('SPAN', {id : 'spark-' + i, style :
                                 {fontWeight : 'bold', color : col, fontSize : size, position : 'absolute', zIndex : '10050'},
                                 innerHTML : char}, holder);
         this.sparks[i] = spark;
      }

      this.start();
   },

   animate : function ()
   {
      for (var i = 0; i < this.num; i++)
      {
         var spark = this.sparks[i];
         spark.style.left = (spark.x += spark.dx) + 'px';
         spark.style.top = (spark.y += (spark.dy += 0.09)) + 'px';
      }
   },

   start : function ()
   {
      var me = this;

      this.reset();
      this.startTimer = window.setInterval(function () { me.animate(); }, 40);
      this.resetTimer = window.setInterval(function () { me.reset(); }, 2500);
   },

   stop : function ()
   {
      if (this.startTimer != -1)
         window.clearInterval(this.startTimer);

      if (this.resetTimer != -1)
         window.clearInterval(this.resetTimer);

      for (var i = 0; i < this.num; i++)
      {
         this.sparks[i].style.top = '-10px';
         this.sparks[i].style.left = '-10px';
      }
   },

   reset : function ()
   {
      var scroll = getScrollPos();
      var win = window_size();
      var doc = doc_size();

      var startX = Math.max(50, Math.random() * win.x - 150);
      var startY = -doc.y + (Math.random() * win.y - 50 + scroll.y);

      var col = this.colors[Math.floor(Math.random() * this.colors.length)];
      var size = this.sizes[Math.floor(Math.random() * this.sizes.length)] + 'px';
      var char = this.chars[Math.floor(Math.random() * this.chars.length)];

      for (var i = 0; i < this.num; i++)
      {
         var spark = this.sparks[i];
         var b = Math.random() * 6.294;
         var a = (Math.random() > 0.6) ? 2 : Math.random() * 2;

         spark.dx = a * Math.sin(b);
         spark.dy = a * Math.cos(b) - 2;

         spark.x = startX;
         spark.y = startY;

         spark.style.color = col;
         spark.style.fontSize = size;
         spark.innerHTML = char;
      }
   }
}