//================================================
//===	Wildgroup 2001 Oleg Myaskovsky
//===	Cache utils
//===	ver. 2.0
//===	requires hashmap.js
//================================================

var cache = new Cache();
var cacheManager = new CacheManager(cache);

function getImage(src, img) {
	if (genKey(src)!=genKey(img.src))
	//if (img.src.indexOf('dot.gif')>-1)
		cacheManager.addCommand(src, img);
}

//================================================
//=== CacheManager class
//================================================

function CacheManager(cache) {
	this.cache = cache;
	
	this.isReady = true;
	this.cp = 0;
	this.commands = new Array();
	this.hasCommands = function() {
		return (this.cp<this.commands.length);
	}	
	this.addCommand = function(src, img) {
		this.commands[this.commands.length] = [src, img];
		this.run();
	}
	
	this.run = function() {
		if (this.hasCommands() && this.isReady) {
			this.isReady = false;
			this.cache.get(this.commands[this.cp][0], this.commands[this.cp][1]);
			this.cp++;
			this.isReady = true;
			this.run();
		}
	}
}

//================================================
//=== Cache class
//================================================

function Cache() {
	this.images = new Hashmap();

	this.put = function (src) {
		var key = this.getKey(src);
		
		var img = new Image();
		img.src = src;
		//alert(show_props(img))
		this.images.put(key, img);
		return img;
	}

	this.get = function (src, image) {
		var key = this.getKey(src);
		
		var img = this.images.get(key);
		if (img==null) {
			img = this.put(src);
		}
		image.src = img.src;
		image.key = key;			
	}
	
	this.getKey = genKey;
}

function genKey(src) {
	if(src == null || src.substring ==null || src.lastIndexOf == null)
		return src;
	return src.substring(src.lastIndexOf('/')+1);
}
