/*
Dodatek do jQuery by Wit

$(...).Fader(n) -- 0 < n < 100 - powoduje alphablending obiektu do podanej przezroczystosci

dst=docelowa przezroczystosc 0-100

opts={timeout_step: 50,
      opacity_step: 40,
      display: '' };

*/

$.fn.Fader=function(dst, opts)
{
  this.each(function()
    {
      if(!this.Fader)
      {
        this.Fader=new
            (function(el, dstop, opts)
            {
              var that=this;

              var SetOpacity=function()
              {
                if(typeof(this.el.style.filter)!='undefined') this.el.style.filter=( this.opacity==100 ? '' : 'alpha(opacity='+this.opacity+')' );
                if(typeof(this.el.style.opacity)!='undefined') this.el.style.opacity=( this.opacity==100 ? '' : this.opacity/100.0 );
                if(this.opacity==0)
                  el.style.display='none';
                else
                  el.style.display=this.opts.display;
              }

              var GetOpacity=function()
              {
                if(typeof(this.el.style.opacity)!='undefined')
                {
                  var p=this.el.style.opacity;
                  if(p=='')
                    return 100;
                  return p*100;
                };
                if(typeof(this.el.style.filter)!='undefined')
                {
                  var p=this.el.style.filter;
                  if(p=='')
                    return 100;
                  p=p.match(/alpha\(opacity=([0-9]+)\)/)[1];
                  return parseInt(p);
                };
              }

              var Fade=function()
              {
                this.timer=null;
                if(this.opacity>this.dstop)
                {
                  this.opacity=Math.round(this.opacity-this.opts.opacity_step);
                  if(this.opacity>this.dstop)
                    this.timer=setTimeout(function() { that.Fade(); }, this.opts.timeout_step);
                  else
                    this.opacity=this.dstop;
                  this.SetOpacity();
                }else
                if(this.opacity<this.dstop)
                {
                  this.opacity=Math.round(this.opacity+this.opts.opacity_step);
                  if(this.opacity<this.dstop)
                    this.timer=setTimeout(function() { that.Fade(); }, this.opts.timeout_step);
                  else
                    this.opacity=this.dstop;
                  this.SetOpacity();
                };
              }

              var Go=function(dstop, opts)
              {
                this.Stop();
                if(typeof(opts)!='undefined')
                  $.extend(this.opts, opts);
                this.opacity=( this.el.style.display=='none' ? 0 : this.GetOpacity() );
                this.dstop=dstop;
                this.timer=setTimeout(function() { that.Fade(); }, this.opts.timeout_step);
              }

              var Stop=function()
              {
                if(this.timer)
                  clearTimeout(this.timer);
                this.timer=null;
              }

              this.GetOpacity=GetOpacity;
              this.SetOpacity=SetOpacity;
              this.Fade=Fade;
              this.Go=Go;
              this.Stop=Stop;

              this.el=el;
              var disp=( this.el.style.display!='none' ? this.el.style.display : '' );
              this.opts={timeout_step: 50,
                         opacity_step: 40,
                         display: disp };
              this.timer=null;

              this.Go(dstop, opts);
            })(this, dst, opts);
      }else
        this.Fader.Go(dst, opts);
    });

}

