/**
 * preloader.js
 * PB 13/03/08
 * preloads a supplied image;
 *
 * Dependancies:
 * ===================================
 * base.js  http://dean.edwards.name/weblog/2006/03/base/
 * event.js http://dean.edwards.name/weblog/2005/10/add-event2/
 */

var Preloader = Base.extend({

    wrap: null,
    image: null,
    width: null,
    height: null,
    
    /**
     * @param   url{String}  ref preloader visual representation, animated gif,
     * @param   url{String}  width of preloader representation;
     * @param   url{String}  height of preloader representation;
     */
    constructor: function( url, w, h ) {
        if( ! document.getElementById ||
            ! document.getElementsByTagName ){
            return false;
        }
         
        this.img = url;
        this.width = w;
        this.height = h;
        this.init(url);
	},
	
	init: function(url){
	
	    var img, parent;
	    
	    this.wrap = document.createElement("div");
	    this.wrap.id = "preloader";
	    this.wrap.style.position = "absolute";
	    this.wrap.style.top = "0";
	    this.wrap.style.left = "0";
	    
        img = document.createElement("img");
        img.width = this.width;
        img.height = this.height;
        img.src = url;

        this.wrap.appendChild( img );
    },
    
    getHTML: function(){
        if( ! this.wrap ){
            this.build();
        }
        return this.wrap;
    },
	
	show: function(){
	    this.wrap.style.display = "block";
	},
	
	hide: function(){
	    this.wrap.style.display = "none";
	},
	
	/**
     * Load the requested image,
     * @param   url{String}         ref to image to load;
     * @param   callback{Function}  function to call when loading complete;
     * @param   callee{Object}      originator of request;
     */
	load: function( img, callback, callee ){
	    this.callee = callee;
	    this.image = new Image();
	   addEvent( this.image, "load", this.complete( this, callback ) );	
	   this.image.src = img;
	},
	
	complete: function( preloader, callback ){
	    return function(e){
	        callback.call( preloader.callee, preloader.image );
	    }
	}
});