/*

	libloader.js - load JavaScript libraries based on browser type

	NOTE: Requires that the "Ultimate client-side JavaScript client
	sniff" from Netscape (or its equivalent) be already loaded, as
	this makes decisions based on the sniff's results.

	NOTE: If you neither pass in a libs_path argument nor override
	default_libs_path before you call load_library, it will default to
	computing library filenames relative to the current path. That
	means that each library should have its own directory in the
	current path, e.g., object_access/, object_position/, etc.
	
	You probably -really- want to go ahead and set a library path, for
	cleanliness' sake.

	NOTE: If default_libs_path or a libs_path argument is non-empty,
	it MUST end with a slash ("/"), since I don't really want to hack
	around to check for that right now. Maybe later, when I'm feeling
	more user-friendly.

	NOTE: This is the one library (apart from uni_sniff, of course)
	that will probably have nasty, hackish, per-browser checking,
	because it can't rely on any generalized functionality having been
	loaded yet. However, I try to rely on as little mojo as possible
	here (get_global being the only place I'm aware of needing to
	abstract away anything, and that happens to be the same across all
	browsers right now) in an effort to keep things clean.

	However, note that _make_libpath has some nasty hardcoded rules to
	compute the library filename. Those rules are (at present)
	necessary because of the way uni_sniff computes its various
	variables. We can't just look through the namespace and snatch out
	"is_" variables that are true. We actually have to know something
	about the various quirks of how browsers are named and how
	compatible they are (or aren't). At some point, we'll codify this
	in a snazzy data structure, but that day ain't today.

*/

// We check, just in case someone defined default_libs_path -before-
// loading this script (or loaded this script multiple times?)
if (! default_libs_path) var default_libs_path = "";


/* All browsers do this the same way, so we're safe defining it here. */
function get_global (name) {
	return window[name];
}


function library_loaded (name) {
	debug.writeln("library_loaded: " + name + " [" + get_global("lib_" + name) + "]");
	return get_global("lib_" + name);
}


function library_load (name, libs_path) {
	if (library_loaded(name)) return 1;
	var libpath = _make_libpath(name, libs_path);
	document.write('<script language="JavaScript" src="' + libpath + '"></' + 'script>');
	debug.writeln("loading: " + libpath);
}


function _make_libpath (name, libs_path) {
	if (! libs_path) libs_path = default_libs_path;
	var libpath = libs_path + name + "/";

	// Yucky if/else ladder, but we have no choice, since uni_sniff
	// doesn't build us a "canonical" agent string we can use instead.

	// Note that this ladder will have to be updated every time
	// uni_sniff is updated, since we return the highest specific
	// information we can find, but over that, we just return
	// the "up" info.

	// Note that we make no attempt here to "consolidate" versions
	// based on any kind of compatibility. That's something you need
	// to do in your library layout, by parcelling out "compatible"
	// functions into individual libraries and then symlinking them
	// to each of the version-specific names.

	// I suppose, at some point, I could rewrite this to test for
	// the existence of a particular specific library, and fall back
	// to a general one, but that would probably generate an awful
	// lot of traffic. I think that's an approach better suited to
	// server-side selection of JavaScript components.

	// Okay, here's an idea. Each library can export a file that has
	// information on the compatibilities for that file. This function
	// can load that file for the library, use it to pick the most
	// specific implementation available, or return a failure. That way,
	// we reduce the process to two loads per library, instead of a
	// huge if/else ladder coupled with a load that might fail, anyway.

	// NOTE: That's not what we're doing right now, but I think it's
	// a good idea, and where I'll probably go with this.


	// NOTE: Until then, the if/else ladder is more or less in order
	// of likelihood, based on browser statistics 

	/* Internet Explorer family */
	if (is_ie) {
		if (is_ie5_5) return libpath + "ie5_5.js";
		if (is_ie5_1b1) return libpath + "ie5_1b1.js";
		if (is_ie5) return libpath + "ie5.js";
		if (is_ie6) return libpath + "ie6.js";
		if (is_ie6up) return libpath + "ie6up.js";

		if (is_ie4) return libpath + "ie4.js";
		if (is_ie3) return libpath + "ie3.js";
		if (is_ie2) return libpath + "ie2.js";

		return libpath + "ie.js";
	}


	/* Netscape Navigator family */
	if (is_nav) {
		if (is_nav7up && !is_nav7) { return libpath + "nav7up.js"; }
		if (is_nav7) { return libpath + "nav7.js"; }
		if (is_nav4) { return libpath + "nav4.js"; }
		if (is_nav6) { return libpath + "nav6.js"; }
		
		if (is_nav3) { return libpath + "nav3.js"; }	
		if (is_nav2) { return libpath + "nav2.js"; }

		return libpath + "nav.js";
	}


	/* Opera family */
	if (is_opera) {
		if (is_opera5) { return libpath + "opera5.js"; }
		if (is_opera5up) { return libpath + "opera5up.js"; }

		if (is_opera4) { return libpath + "opera4.js"; }
		if (is_opera3) { return libpath + "opera3.js"; }
		if (is_opera2) { return libpath + "opera2.js"; }

		return libpath + "opera.js";
	}

	return libpath + "unknown.js";
}
/* This is for Emacs' benefit:
 * Local Variables:  ***
 * mode: Java        ***
 * End:              ***
 */
