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

YUKU.rainbowLinks = {
   obj : null,     // The object which event occured in
   active : 0,          // Flag during the action
   elmH : 0,         // Hue
   elmS : 128,       // Saturation
   elmV : 255,       // Value
   color : null,     // A color before the change
   timer : null,    // Timer ID
   rate : 25,

   init : function (rate)
   {
      this.rate = rate;

      addEvent(document, 'mouseover', YUKU.rainbowLinks.startRainbow, false);
      addEvent(document, 'mouseout', YUKU.rainbowLinks.stopRainbow, false);
   },

   startRainbow : function (e)
   {
      var me = YUKU.rainbowLinks;

      if (me.active == 0)
      {
         var obj = get_srcEl(e);
         if (obj.nodeName && obj.nodeName.toUpperCase() != 'A' && obj.nodeName.toUpperCase() != 'BODY')
            obj = get_parent_by_tag(obj, 'A');

         if (obj && obj.nodeName.toUpperCase() == 'A' && obj.href && obj.href != '' && !className.test(obj, 'no-rainbow'))
         {
            me.obj = obj;
            me.active = 1;
            me.color = obj.style.color;
            me.timer = window.setInterval("YUKU.rainbowLinks.changeColor()", 50);
         }
      }
   },

   stopRainbow : function (e)
   {
      var me = YUKU.rainbowLinks;
      if (me.active == 1)
      {
         if (me.obj.nodeName.toUpperCase() == 'A')
         {
            me.obj.style.color = me.color;
            window.clearInterval(me.timer);
            me.active = 0;
         }
      }
   },

   changeColor : function ()
   {
      this.obj.style.color = this.makeColor();
   },

   makeColor : function ()
   {
      // Don't you think Color Gamut to look like Rainbow?

      // HSVtoRGB
      if (this.elmS == 0) {
         elmR = this.elmV;
         elmG = this.elmV;
         elmB = this.elmV;
      }
      else
      {
         t1 = this.elmV;
         t2 = (255 - this.elmS) * this.elmV / 255;
         t3 = this.elmH % 60;
         t3 = (t1 - t2) * t3 / 60;

         if (this.elmH < 60) {
            elmR = t1;  elmB = t2;  elmG = t2 + t3;
         }
         else if (this.elmH < 120) {
            elmG = t1;  elmB = t2;  elmR = t1 - t3;
         }
         else if (this.elmH < 180) {
            elmG = t1;  elmR = t2;  elmB = t2 + t3;
         }
         else if (this.elmH < 240) {
            elmB = t1;  elmR = t2;  elmG = t1 - t3;
         }
         else if (this.elmH < 300) {
            elmB = t1;  elmG = t2;  elmR = t2 + t3;
         }
         else if (this.elmH < 360) {
            elmR = t1;  elmG = t2;  elmB = t1 - t3;
         }
         else
         {
            elmR = 0;  elmG = 0;  elmB = 0;
         }
      }

      elmR = Math.floor(elmR).toString(16);
      elmG = Math.floor(elmG).toString(16);
      elmB = Math.floor(elmB).toString(16);

      if (elmR.length == 1) elmR = "0" + elmR;
      if (elmG.length == 1) elmG = "0" + elmG;
      if (elmB.length == 1) elmB = "0" + elmB;

      this.elmH = this.elmH + this.rate;
      if (this.elmH >= 360)
         this.elmH = 0;

      return '#' + elmR + elmG + elmB;
   }
}
