/*______________________________________

  mizGradationEffect #061116 (version 0.1)
  
  Copyright (C) 2006 Mizuyari All rights reserved.
  Script written by Mahiro Komura.
  http://mizuyari.jp/
______________________________________*/

function setMizAnchorEffect(tElmtName, tClassName, event, options) { // mizAnchorEffect #061115
 var options = arguments[3];
 // trace(options)
 if(!document.getElementById) return;
 if(!document.images) return;
 var tElmt = document.getElementsByTagName(tElmtName);
 for (i=0; i<tElmt.length; i++) {
  var strClassAttr = tElmt[i].className.split(" ").toString();

  if (!tClassName || strClassAttr.indexOf(tClassName) != -1) {

   tElmt[i].effect = new event(tElmt[i],options.fromColor,options.toColor);
   if (!mizuyari.ua.isWinIE) {
    mizuyari.addEvent(tElmt[i],'mouseover', function() { this.effect.effectOver(); } );
    mizuyari.addEvent(tElmt[i],'mouseout' , function() { this.effect.effectOut(); }  );
    mizuyari.addEvent(tElmt[i],'fucus'    , function() { this.effect.effectOver(); } );
    mizuyari.addEvent(tElmt[i],'blur'     , function() { this.effect.effectOut(); }  );
   } else {
    tElmt[i].effect._onmouseover = tElmt[i].onmouseover;
    tElmt[i].effect._onfocus     = tElmt[i].onfocus;
    tElmt[i].effect._onmouseout  = tElmt[i].onmouseout;
    tElmt[i].effect._onblur      = tElmt[i].onblur;
    tElmt[i].onmouseover = function() {
     if(this.effect._onmouseover) this.effect._onmouseover(); this.effect.effectOver();
    };
    tElmt[i].onmouseout = function() {
     if(this.effect._onmouseout) this.effect._onmouseout(); this.effect.effectOut();
    };
    tElmt[i].onfocus = function() {
     if(this.effect._onfocus) this.effect._onfocus(); this.effect.effectOver();
    };
    tElmt[i].onblur = function() {
     if(this.effect._onblur) this.effect._onblur(); this.effect.onblur();
    };

   }

  }
 }
}

function mizGradationEffect() { // mizGradationEffect #061115
 this.initialize.apply(this, arguments);
}
mizGradationEffect.prototype = {
  initialize : function(obj, fromColor, toColor) {
   this.obj = obj;
   this.lockId = null;
   this.fromColor = [fromColor[0],fromColor[1],fromColor[2]];
   this.toColor   = [toColor[0],toColor[1],toColor[2]];
   this.currentColor = [0,0,0];
   this.cacheColor = [fromColor[0],fromColor[1],fromColor[2]];
   this.currentFrame = 0;
   this.totalFrames = 4;
   this.aliasId = mizInstanceAliases.length;
   mizInstanceAliases[this.aliasId] = this;
   this.effectOut();
  },
  
  effectOver : function() {
   this.effectInit('effectOver');
   if (this.effectTimer) clearTimeout(this.effectTimer);
   this.effectMain(this.toColor);
  },

  effectOut : function() {
   this.effectInit('effectOut');
   if (this.effectTimer) clearTimeout(this.effectTimer);
   this.effectMain(this.fromColor);
  },

  effectMain : function(dirColor) {
   var cacheColor   = this.cacheColor;
   var currentColor = this.currentColor;
   var wait = (this.lockId == 'effectOut' && this.currentFrame == 0) ? 384 : (this.lockId == 'effectOut') ? 16 : 8;
   this.totalFrames = (this.lockId == 'effectOver') ? 4 : 16;
   for (var i=0; i<3; i++) {
    currentColor[i] = cacheColor[i] + Math.round((dirColor[i] - cacheColor[i]) / this.totalFrames * this.currentFrame)
   }
   this.obj.style.color = toHexColor(currentColor[0],currentColor[1],currentColor[2]);
   // trace("currentColor: %d, cacheColor: %d, dirColor: %d",currentColor, cacheColor, dirColor)
   if (this.totalFrames != this.currentFrame) {
    this.currentFrame++;
    mizInstanceAliases[this.aliasId] = this;
    this.effectTimer = setTimeout("mizInstanceAliases["+this.aliasId+"]."+ this.lockId +"();", wait);
   } else {
    this.currentFrame = 0;
    this.lockId = null;
   }
  },

  effectInit : function(id) {
   if (this.lockId == id) return;
   // trace("[this.aliasId: %d] this.lockId: %d (%d)", this.aliasId, this.lockId, typeof this.lockId);
   this.lockId = id;
   this.cacheColor = [this.currentColor[0],this.currentColor[1],this.currentColor[2]];
   this.currentFrame = 0;
  }

}

// example
// mizuyari.addOnLoadEvent( function() { return setMizAnchorEffect(null,null,mizGradationEffect,{fromColor : [0,0,0], toColor : [102,153,0]}); });