(function(window) {
    
Loader = function(target) {
    
    var that;
   
    this.initialize(target);
}

var p = Loader.prototype;

// static interface

Loader._queue = [];

Loader.add = function(target) {
    
    if (target instanceof Array) {
        for (var i = 0, l = target.length; i < l; i++) {
            Loader.queue(target[i]);
        }        
    } else {
        Loader.queue(target);
    }
    
    return this;
}

Loader.queue = function(target) {
    
    var type, loader;
   
    if (target.tagName !== undefined) {
        type = target.tagName.toLowerCase();
        switch (type) {
            case "img":
                loader = new ImageLoader(target);
                break;
        }         
    }
    
    loader.onload(this.check);
    Loader._queue.push(loader);   
}

Loader.load = function() {
    
    var queue;
    
    queue = this._queue;
    
    for (var i = 0, l = queue.length; i < l; i++) {
        queue[i].load();
    }   
}

Loader.check = function() {

    Loader.remove(this);
    if (Loader._queue.length === 0) {
        Loader.onload();
    }
}

Loader.remove = function(target) {
    
    var queue;
    
    queue = Loader._queue;
    
    for (var i = 0, l = queue.length, loader; i < l; i++) {
        loader = queue[i];
        if (loader._target === target) {
            queue.splice(i, 1);
            break;
        }
    }
}

Loader.isCompatible = function() {
    
    var version;
    
    version = navigator.appVersion.match(/MSIE ([\d.]+)/);
    if (version !== null && version.length > 1) {
        version = parseFloat(version[1]);
    }
    
    return !(version > -1 && version < 9);
}

// private properties

//p._target = null;

// constructor

p.initialize = function(target) {
    
    that = this;
    
    if (target) this.add(target);
}

// public methods

/*p.load = function() {
    
    var type;
   
    if (this._target.tagName !== undefined) {
        type = this._target.tagName.toLowerCase();
        switch (type) {
            case "img":
                this._loadImage();
                break;
        }         
    }
}*/

// private methods

window.Loader = Loader;    
}(window));

(function(window) {

ImageLoader = function(target) {
    
    var that;
    
    this.initialize(target);   
}

var p = ImageLoader.prototype;

p.initialize = function(target) {
    that = this;
    this._target = target;
}

// private vars

p._target = null;

// public methods

p.load = function() {
    if (!Loader.isCompatible() && !that.complete()) {
        setTimeout(that.load, 100);
    } else {
    
    }
    return this;
}

p.complete = function() {
    return (this._target.complete !== null && this._target.complete === true);
}

p.onload = function(func) {
    if (func) {
        if (this._target.addEventListener) {
            this._target.addEventListener("load", func, false);
            this._target.addEventListener("load", func, false);
        } else if (this._target.attachEvent) {
            this._target.attachEvent("load", func);
            this._target.attachEvent("load", func);
        }    
    }
    return this;
}

window.ImageLoader = ImageLoader;
}(window));
