/**
 * preloader.js
 * PB 24/04/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/
 * jQuery.js http://http://jquery.com/
 */

var Preloader = Base.extend({

    wrap: null,
    $: null,
    
    /**
     * @param   ${Function}  jQuery ref;
     */
    constructor: function() {
        // requires jQuery;
        if( ! jQuery ){
            return false;
        }
        this.$ = $;
	},
	
	/**
     * @param   url{String}  ref preloader visual representation, animated gif,
     * @param   url{String}  width of preloader representation;
     * @param   url{String}  height of preloader representation;
     */
	init: function( url, w, h ){	
	    var css = { 
	            position: 'absolute', 
	            top: "0", 
	            left: "0"
	        };
	    this.wrap = this.$('<div></div>')
				    .addClass('ui-preloader')
				    .css( css )
				    .appendTo("body");
	        
        this.$('<img></img>')
		    .attr( "height", h )
			.attr( "width", w )
			.attr( "src", url )
			.appendTo( this.wrap );
    },
    
    getHTML: function(){
        if( ! this.wrap ){
            this.build();
        }
        return this.wrap;
    },
	
	show: function(){
	    this.wrap.show();
	},
	
	hide: function(){
	    this.wrap.hide();
	},
	
	/**
     * 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( url, callback, callee ){
	    var image = new Image();
	    addEvent( image, "load", function(){
	         callback.call( callee, image );          
	    });
	    image.src = url;
	}
});