blob: 12cd903956f4f00d3af63d97c57efd3ea75d4636 [file] [log] [blame]
var ImageManager = (function(){
var images = [];
var ob = {};
function getImage(path){
var i = 0, len = images.length;
while(i<len) {
if(images[i].path === path){
images[i].count += 1;
return images[i];
}
i += 1;
}
function imageLoaded(){
imageOb.loaded = true;
}
var img = document.createElement('img');
img.addEventListener('load', imageLoaded, false);
img.addEventListener('error', imageLoaded, false);
var imageOb = {
elem: img,
path: path,
loaded: false,
count: 1
};
img.src = path;
images.push(imageOb);
return imageOb;
}
function unregisterImage(ob){
ob.count -= 1;
if(ob.count === 0) {
var ind = images.indexOf(ob);
images.splice(ind, 1);
ob.elem = null;
}
}
ob.getImage = getImage;
ob.unregisterImage = unregisterImage;
return ob;
}());