<!--
/**
	The goal of the configuration portion of this  file is
	to minimize the overhead associated with loading unnecessary
	library files.  Ideally, the Java or PHP application should
	insert into their templates an array of appropriate modules
	needed by the page-in-question.  Next, lib.config.js should be
	attached to the template.  The rest is handled by this file...
	
	Ideally, the event stuff would be moved to its own file
	(lib.event.js) and switched on as are the other libraries.
	However, other libraries wish to use these methods - some before
	the HEAD object is completely loaded... creating problems in IE.
	The only way around this is to add MORE javascript to the HEAD
	(which I'm hoping to avoid at all costs), or I could include the
	eventstuffs here.  This is generally okay given that it is,
	by far, the most important library.
*/

/*****************************************************/
/*******   Event-related objects and methods   *******/
/*****************************************************/
function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else { alert("Handler could not be attached"); return false; }
}
function addLoadEvent( fn, useCapture ) { return addEvent( window, 'load', fn, useCapture ); }
function JimprovEvent(e) {
	this.event = (e)?e:document.event;
	this.source = (this.event.srcElement && typeof this.event.srcElement != 'undefined' ) ?
					this.event.srcElement : this.event.target;
	this.type = this.event.type;
	this.winX = this.event.clientX; this.winY = this.event.clientY;
	this.objX = this.event.x; this.objY = this.event.y;
	this.screenX = this.event.screenX; this.screenY = this.event.screenY;
	this.button = this.event.button;
	this.key = this.event.keyCode;
	this.hasAlt = this.event.altKey;
	this.hasCtrl = this.event.ctrlKey;
	this.hasShift = this.event.shiftKey;
	// Bubblin'
	this.previous = (this.event.fromElement && typeof this.event.fromElement != 'undefined') ?
						this.event.fromElement : this.event.relatedTarget;
	this.next = (this.event.toElement && typeof this.event.toElement != 'undefined') ?
						this.event.toElement : this.event.relatedTarget;
	// METHODS
	this.getSource = function() { return this.source; }
	this.getEvent = function() { return this.event; }
	this.getType = function() { return this.type; }
	this.getLocationWindow = function() {
		var ret = new Array( );
		ret['x'] = this.winX; ret['y'] = this.winY;
		return ret;
	}
	this.getLocationObject = function() {
		var ret = new Array( );
		ret['x'] = this.objX; ret['y'] = this.objY;
		return ret;
	}
	this.getLocationScreen = function() {
		var ret = new Array( );
		ret['x'] = this.screenX; ret['y'] = this.screenY;
		return ret;
	}
	this.getButton = function() { return this.button; }
	this.getKey = function() { return this.key; }
	this.hasAlt = function() { return this.hasAlt; }
	this.hasCtrl = function() { return this.hasCtrl; }
	this.hasShift = function() { return this.hasShift; }
	this.getPrevious = function() { return this.previous; }
	this.getNext = function() { return this.next; }
	this.debubble = function() {
		if (typeof this.event.cancelBubble != 'undefined') {
			this.event.cancelBubble = true;
			return true;
		} else if ( this.event.stopPropogation) {
			this.event.stopPropogation();
			return true;
		}
		return this.event.preventBubble( );
	}
	this.abort = function() {
		this.event.returnValue = false;
		if ( this.event.target ) {
			return this.event.preventDefault( );
		}
		return true;
	}
}

/*****************************************************/
/*******   Configuration objects and methods   *******/
/*****************************************************/
var JIMPROV_CONFIG_PATH_SCRIPT = JIMPROV_CONFIG_PATH_ROOT + '/resources/scripts/';
var jimprov_libraries = new Array(  // Let's see if we can't keep this alphabetized...
	'ajax',
	'common',
	'cookie',
	'debug',
	'dom',
	'error',
	'number',
	'sniff',
	'string',
	'track',
	'validate'
);
function JimprovConfiguration( ) { // class
	this.libraries = new Array( );
	this.libraries_i = new Array( );
	// construct
	for (var i=0;i<jimprov_libraries.length;i++) {
		this.libraries[jimprov_libraries[i]] = new JimprovLibrary(jimprov_libraries[i],true);
		this.libraries_i[i] = this.libraries[jimprov_libraries[i]];
	}
	this.enable = function ( libraryName ) {
		this.libraries[libraryName].activate( );
		this.libraries[libraryName].attach( );
	}
	this.enableFromArray = function ( arLibraries ) {
		for(var i=0;i<arLibraries.length;i++) { this.enable( arLibraries[i] ); }
	}
	this.addLibrary = function( library ) {
		this.libraries[ library.name ] = library;
		this.libraries_i.push( this.libraries[ library.name ] );
		this.libraries[ library.name ].attach( );
	}
	this.add = function ( libraryName ) {
		this.addLibrary(new JimprovLibrary(libraryName,false,true));
	}
}
function JimprovLibrary( name, blIsLibraryModule, blActive ) { // class
	this.name = name;
	this.isLibrary = (blIsLibraryModule) ? blIsLibraryModule : false;
	this.isActive = (blActive) ? blActive : false;
	this.isAttached = false;
	this.activate = function( ) { this.isActive = true; }
	this.disable = function( ) { this.isActive = false; }
	this.attach = function( ) {
		if (this.isActive && !this.isAttached ) {
			jimprov_attach_script( ((this.isLibrary) ? 'lib.'+this.name+'.js' : this.name+'.js') );
			this.isAttached = true;
		}
	}
}
// The global config variable
var jcfg = new JimprovConfiguration( );


/*****************************************************/
/*******            Helper functions           *******/
/*****************************************************/
function foreach( arr, func ) {
	//if ( typeof arr == 'undefined' || arr.length < 1) { return false; } // TODO: Finsih this thought... Perhaps still incomplete.
	for (var i=0; i<arr.length; i++) { func(arr[i]); }
	// TODO:  Add the ability to iterate over non-indexed arrays.
}
function jimprov_attach_script( filename ) {
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = JIMPROV_CONFIG_PATH_SCRIPT + filename;
	var head = document.getElementsByTagName('head')[0];
	head.appendChild(script);
}
//-->
