// BEGIN: image preloader object ===============================================

/*
	Image Preloader Object
	used to pre-oad images into an object for easy retrieval

	use as follows:

		IMG_PRELOADER.PREFS['default_image_path'] = "/images/photos/";
		IMG_PRELOADER.add('my_image', 'some_cool_photo.jpg');
		IMG_PRELOADER.add('your_image', 'some_bad_photo.jpg');

		IMG_PRELOADER.PREFS['default_image_path'] = "/images/nav/";
		IMG_PRELOADER.batch_add_data = {
			nav_about_0 : 'nav.about.0.gif',
			nav_about_1 : 'nav.about.1.gif',
			nav_contact_0 : 'nav.contact.0.gif',
			nav_contact_1 : 'nav.contact.1.gif'
		};
		IMG_PRELOADER.loadBatch();

	access your images like so:

		document.images['blah'].src = IMG_PRELOADER.i['my_image'].src;
	
*/

var IMG_PRELOADER = {
	// define some preferences
	PREFS : {
		default_image_path : "/img/", // be sure to include leading and trailing slashes!
		allow_id_redefine : true // allow redefining of a preloaded image source
	},

	// the container for the actual images, used as an object hash
	i : { },

	// add an image and load it
	add : function(id, src) {
		// check and see if there is a conflict
		if (typeof(this.i[id]) != 'undefined') {
			if (this.PREFS['allow_id_redefine']) {
				debug('WARNING', '[IMG_PRELOADER] the image id "' + id + '" is already defined, redefining it with a new SRC');
			} else {
				debug('ERROR', '[IMG_PRELOADER] image id "' + id + '" is already defined and cannot be redefined');
				return false;
			}
		}

		// create the new JS image object
		this.i[id] = new Image();
		// check and see if we are passing in an absolute or root reference to the path and use it, otherwise use the default_image_path
		this.i[id].src = ( (src.charAt(0) == "/") || (src.indexOf('http://') == 0) ) ? src : this.PREFS['default_image_path'] + src;

		this.i[id].onload = function() {
			debug('INFO', '[IMG_PRELOADER] finished loading IMG SRC: ' + this.src);
		};

		debug('INFO', '[IMG_PRELOADER] successfully created "' + id + '" with source ' + src);
		return true;
	}, // END: add()

	// for batch adding
	batch_add_data : { },
	loadBatch : function() {
		for (var i in this.batch_add_data) {
			this.add(i, this.batch_add_data[i]);
		}
		this.batch_add_data = { }; // clear out batch add data
		this.rebuildList(); // refresh the image list data
	},

	// to remove (delete) an image from the preloader
	remove : function(id) {
		if (typeof(this.i[id]) != 'undefined') {
			delete this.i[id]; // delete the sucker

			// if the delete was successful...
			if (typeof(this.i[id]) != 'undefined') {
				debug('INFO', '[IMG_PRELOADER] successfully deleted "' + id);
				return true;

			// if it wasn't...
			} else {
				debug('WARNING', '[IMG_PRELOADER] problem deleting "' + id);
				return false;
			}

		// image id does not exist!
		} else {
			debug('WARNING', '[IMG_PRELOADER] cannot delete "' + id + '" because it does not exist.');
			return false;
		}
	}, // END: remove()

	// to refresh the image sources
	refreshImages : function() {
		for (var image in this.i) {
			this.i[image].src = this.i[image].src + "";
			debug('INFO', '[IMG_PRELOADER] refreshed ' + image + ' src ' + this.i[image].src);
		}
	}, // END: refresh()

	image_list : [],
	rebuildList : function() {
		this.image_list = [];
		for (var image in this.i) {
			this.image_list.push(image);
		}
	} // END: rebuildList()

};

// END: image preloader object -------------------------------------------------

