var flashed=Array();
var oldbg = Array();

function decToHex(dec) {
  var hexa="0123456789abcdef";
  var hex="";

  while (dec>15) {
    tmp=dec-(Math.floor(dec/16))*16;
    hex=hexa.charAt(tmp)+hex;
    dec=Math.floor(dec/16);
  }

  hex=hexa.charAt(dec)+hex;

  return(hex);
}

function flash(what, color, dur, st) {

  if (flashed[what.id])
    return;
  else {
    flashed[what.id]=true;
    document.cookie=what.id+"=flashed";
  }

  oldbg[what.id] = what.style.background; 

  var sStart = (new Date()).getTime();
  var r = parseInt(color.substr(1,2),16);
  var g = parseInt(color.substr(3,2),16);
  var b = parseInt(color.substr(5,2),16);
  
  var r_dif = 255 - r;
  var g_dif = 255 - g;
  var b_dif = 255 - b;

  function col(lr,lg,lb) {
    return "#"+decToHex(lr)+decToHex(lg)+decToHex(lb);
  }
  
  function go_flash() {

    var el = (new Date()).getTime() - sStart;


    if (el < dur) {

      var nr = Math.floor(  r_dif*(Math.sin( (Math.PI/(dur*2))*el ))) ;
      var ng = Math.floor(  g_dif*(Math.sin( (Math.PI/(dur*2))*el ))) ;
      var nb = Math.floor(  b_dif*(Math.sin( (Math.PI/(dur*2))*el ))) ;

      what.style.background=col(r+nr,g+ng,b+nb);

      setTimeout(go_flash,10);
    } else {
      what.style.background='';

    }

  }

  setTimeout(go_flash,1);


}

function flash_reset() {
  alert("reset");
  
  for( var i in flashed )
    flashed[i] = false;
}

