//requires jb_DHTMLLib.js

function jb_img(imgloader, imgpath, imgname, imgid, display_callback) {
	this.imgloader = imgloader;
	this.imgpath = imgpath;
	this.imgname = imgname;
	this.imgid = imgid;
	this.display_callback = display_callback;
	this.isLoaded = false;
	this.stopLoading = false;
	this.myimg = new Image();
	//crazy, no? this creates a function where the instance of jb_img gets stored in "binding" and then calls it with "this"
//	this.myimg.onload = (function(binding) { return function() { return binding.loaded.apply(binding, arguments); } } )(this);
	this.myimg.onload = (function(binding) { return function() { 
		var ret = binding.loaded.apply(binding, arguments); 
		//and supposedly this clears up memory leaks in IE:
		binding.myimg.onload = null; 
		binding = null;
		return ret;
} } )(this);

}

jb_img.prototype.load = function() {
	this.myimg.src = this.imgpath + "/" + this.imgname;
};

jb_img.prototype.loaded = function() {
	this.imgloader.imgsLoaded++;
//	alert("from loaded " + this.imgid);
	this.display();
	this.isLoaded = true;
	if (!this.stopLoading) {
		imgToLoad = this.imgloader.whichImg + 1;
		this.imgloader.loadImgAtIndex(imgToLoad);
	} else {
		//stop loading and reset flag.
		this.stopLoading = false;
	}
}

jb_img.prototype.display = function() {
	//alert(this.imgid);
	if (obj = getRawObject(this.imgid)) {
		obj.src = this.myimg.src;
		if (this.display_callback) {
			eval(this.display_callback + '(this.imgid)');
		}
	}
};

function jb_imgloader(id) {
	this.id = id;
	this.imgArr = new Array();
	this.numImgs = 0;
	this.stackCount = 0;//sigh: the recursivity causes stack overflows in IE and Safari, so we'll limit it to only x iterations
	this.stackMax = 12;//I'm not sure, but I think this needs to be ~50/# of imageloader objects
	this.imgsLoaded = 0;
	this.whichImg = 0;
	this.isLoading = -1;
}


jb_imgloader.prototype.addImg = function(imgpath, imgname, imgid, display_callback) {
	this.imgArr.push(new jb_img(this, imgpath, imgname, imgid, display_callback));
	this.numImgs++;
};

jb_imgloader.prototype.loadImgAtIndex = function(which) {
	which = parseInt(which);
	for (i=which;i<which+this.numImgs;i++) {
		this.whichImg = i;
		if (this.whichImg >= this.numImgs) {
			this.whichImg = i - this.numImgs;//wrap around to beginning
		}
		if (this.imgArr[this.whichImg].isLoaded) {
			this.imgArr[this.whichImg].display();
		} else {
			//add loadcounter to prevent stack overflow
			this.stackCount++;
			if (this.stackCount < this.stackMax) {
				this.isLoading = this.whichImg;//store loading image so we can interrupt the load
				this.imgArr[this.whichImg].load();
			} else {
				this.stackCount = 0;
			}
			break;
		}
	}
};

jb_imgloader.prototype.seek = function(which) {
	//stop current load
	this.stackCount = 0;
	if (this.isLoading >= 0) {
		this.imgArr[this.isLoading].stopLoading = true;
	}
	this.loadImgAtIndex(which);
}

jb_imgloader.prototype.getImgnameForIndex = function(which) {
	return this.imgArr[which].imgname;
}

jb_imgloader.prototype.getImgpathForIndex = function(which) {
	return this.imgArr[which].imgpath;
}


