FadingImage.prototype = {};

function FadingImage(config)
{
    var container = jQuery("#" + config.containerId),
        image = jQuery("<img>"),
        width = -1,
        height = -1;
    
    initialize();
    
    function initialize()
    {
       image.load(prepareImage);
       image.attr("src", config.src);
       
       imgCount++;
    }
    
    function prepareImage(e)
    {
       var width = container.outerWidth(),
           height = container.outerHeight(),
           ttl = 10000 + (Math.random() * 40000),  // Time to live, fact fans!
           animationTtl = ttl * 2;
       
    
       container.append(image);
       
       image.addClass("fader pngfix");
       
       image.css({    
                      top : -280 + Math.random() * height,
                     left : -400 + Math.random() * width,
                  opacity : 0,
                   zIndex : -1
                 });
                 
       
       image.fadeTo(10000, Math.random() * 0.3, function()
                                                {
                                                  window.setTimeout(remove, ttl);
                                                });
                                                
                                                
       var deltaX = width - (Math.random() * (width * 2)),
           deltaY = height- (Math.random() * (height * 2)),
           imageX = image.css("top").toValue(),
           imageY = image.css("left").toValue();
       
       image.animate({
                        top : (imageX + deltaX) + "px",
                        left : (imageY + deltaY) + "px"
                     }, animationTtl);                  
    }
    
    function remove()
    {
       image.fadeTo(10000, 0, function()
                              {
                                jQuery(this).hide().addClass("dead");
                                imgCount--;
                              });
       
    }
}
