/**
 * @author Paul Bakaus
 */
jQuery.fn.animateClass = function(c1,c2,c3) {
 var colorIntervals = [];
 var colorTimers = [];
 return jQuery(this).each(function(){
  for(var i=0;i<this.className.split(" ").length;i++)
  { if(this.className.split(" ")[i] == c1) return; }
  var aniObj = {};
  var aniDuration = (c2 && typeof c2 != "string") ? c2 : c3;
  var oldStyleAttr = ($(this).attr("style") || '');
  if(typeof oldStyleAttr == 'object') oldStyleAttr = oldStyleAttr["cssText"];
  var dummyEl = this.cloneNode(true);
  $(dummyEl).html($(this).html());
  $(dummyEl).css("visibility", "hidden").css("position", "absolute");
  $(this.parentNode).append(dummyEl);
  if(typeof c2 != "string") $(dummyEl).get(0).className = this.className; else $(dummyEl).get(0).className = "";
  $(dummyEl).addClass(c1);
  if(document.defaultView) {
   var oldStyle = document.defaultView.getComputedStyle(this,null);
   var newStyle = document.defaultView.getComputedStyle(dummyEl,null);
  } else {
   var oldStyle = this.currentStyle;
   var newStyle = dummyEl.currentStyle;
  }
  if(window.console != undefined) console.log("Animating element to class "+c1+" with the following properties:");
  
  for(var n in newStyle) {
   if( typeof newStyle[n] != "function" && newStyle[n] && n.indexOf("Moz") == -1 && n.indexOf("length") == -1 && newStyle[n] != oldStyle[n]
   )
   { 
    if(n.indexOf("Color") == -1 && n.indexOf("color") == -1) {
     if(!isNaN(parseInt(newStyle[n].replace(/px/, "")))) {
                     if(oldStyle.position != "static"  || (oldStyle.position == "static" && n != "left" && n != "top" && n != "bottom" && n != "right"))
                     {
       if(window.console != undefined) console.log(n+": "+parseInt(newStyle[n].replace(/px/, "")));  /* Debug line */
       aniObj[n] = parseInt(newStyle[n].replace(/px/, ""));
                  }
                 }
    } else {
     animateColor(this,n,oldStyle[n],newStyle[n], aniDuration, oldStyleAttr);
     if(window.console != undefined) console.log(n+": "+newStyle[n]);  /* Debug line */
    }
   }
  }
  $(this).animate(aniObj, aniDuration, function() {   
   if(typeof c2 == "string") $(this).removeClass(c2);
   $(this).addClass(c1);
   if(typeof $(this).attr("style") == 'object') {
    $(this).attr("style")["cssText"] = "";
    $(this).attr("style")["cssText"] = oldStyleAttr;
   } else {
    $(this).attr("style", oldStyleAttr); 
   }
   $(dummyEl).remove();
  });
 });
 function animateColor(that,prop,oldColor,newColor, aniDuration, oldStyleAttr) {
    
  var nSC,oSC;
  if(oldColor == "transparent") { oSC = [255,255,255]; } else {
   if(oldColor.substr(0, 3) == "rgb") oSC = oldColor.substr(4).replace(/\)/, "").split(","); /* It's a gecko rgb value */
   if(oldColor.substr(0, 1) == "#" && oldColor.length == 7) oSC = [parseInt(oldColor.substr(1,2),16),parseInt(oldColor.substr(3,2),16),parseInt(oldColor.substr(5,2),16)]; /* it's a hex value.. */
   if(oldColor.substr(0, 1) == "#" && oldColor.length == 4) oSC = [parseInt(oldColor.substr(1,1)+oldColor.substr(1,1),16),parseInt(oldColor.substr(2,1)+oldColor.substr(2,1),16),parseInt(oldColor.substr(3,1)+oldColor.substr(3,1),16)]; /* it's a short hex value.. */
  }
  if(newColor == "transparent") {
   nSC = [255,255,255] 
  } else {
   if(newColor.substr(0, 3) == "rgb") nSC = newColor.substr(4).replace(/\)/, "").split(","); /* It's a gecko rgb value */
   if(newColor.substr(0, 1) == "#" && newColor.length == 7) nSC = [parseInt(newColor.substr(1,2),16),parseInt(newColor.substr(3,2),16),parseInt(newColor.substr(5,2),16)]; /* it's a hex value.. */
   if(newColor.substr(0, 1) == "#" && newColor.length == 4) nSC = [parseInt(newColor.substr(1,1)+newColor.substr(1,1),16),parseInt(newColor.substr(2,1)+newColor.substr(2,1),16),parseInt(newColor.substr(3,1)+newColor.substr(3,1),16)]; /* it's a short hex value.. */
  }
  var diffR = parseInt(nSC[0]) - parseInt(oSC[0]);
  var diffG = parseInt(nSC[1]) - parseInt(oSC[1]);
  var diffB = parseInt(nSC[2]) - parseInt(oSC[2]);
  colorTimers[prop] = 0;
  colorIntervals[prop] = window.setInterval(intervalColor,20);
  function intervalColor() {
   colorTimers[prop] = colorTimers[prop] + 20;
   var newR = Math.round(parseInt(oSC[0]) + (diffR/aniDuration)*colorTimers[prop]);
   var newG = Math.round(parseInt(oSC[1]) + (diffG/aniDuration)*colorTimers[prop]);
   var newB = Math.round(parseInt(oSC[2]) + (diffB/aniDuration)*colorTimers[prop]);
   $(that).css(prop, "rgb("+newR+","+newG+","+newB+")");
   if(colorTimers[prop] == aniDuration) {
    window.clearInterval(colorIntervals[prop]);
    if(typeof $(that).attr("style") == 'object') {
     $(that).attr("style")["cssText"] = "";
     $(that).attr("style")["cssText"] = oldStyleAttr;
    }
   }
  };
 };
};
