/**	HISTORY
 *	
 *	17-12-2002	Added loaded parameter to addPackage (= true if the package is loaded in the controller).
 *	
**/


/** Package
 *	
 *	Holds information about the packages
 *	
 *	name		is the name of the package's Contructor
 *	srcFile		is the path and filename of the script
 *	scriptId	is the id of the script-tag, where the package will be loaded
 *	loaded		becomes true when the script is loaded
 *	ins			is an instance of the package(object)
 *	
 */
function Package(pObj,pFile,pLoaded)
{
	this.name = pObj;
	this.srcFile = pFile;
	this.scriptId = "s" + this.name;
	this.loaded = pLoaded;
	this.ins = null;			// Instance
}


/** CommonLibrary
 *	
 *	packages store information about all available packages
 *	requestedPackages store packages requested by a pageset in order to
 *		load it. Each element is an object with a pageset- and a packages-property
 *	
 *	
 */
function CommonLibrary()
{
	this.refString = "";
	this.packages = new Array();
	this.requestedPackages = new Array();

	this.addPackage			=	_CommonLibrary_addPackage;
	//this.call				=	_CommonLibrary_call;
	this.loadPackages		=	_CommonLibrary_loadPackages;
	this.control			=	_CommonLibrary_control;
	this.checkLoadedStatus	=	_CommonLibrary_checkLoadedStatus;
}

/** addPackage
 *	
 *	Adds a package at the end of the packages collection
 *	
 */
function _CommonLibrary_addPackage(pPackage,pFile,pLoaded)
{
	if(new String(pLoaded)=="undefined")pLoaded=false;
	//ctrl.addToLog("_CommonLibrary_addPackage","controller","pPackage: "+pPackage+", pFile: "+pFile+", pLoaded: "+pLoaded);
	this.packages[this.packages.length] = new Package(pPackage,pFile,pLoaded);
}

/** loadPackages
 *	
 *	Loads the requested packages
 *	If all requested packages are already loaded, it calls checkLoaderStatus
 */
function _CommonLibrary_loadPackages(pPageset, pPackages)
{
	var i, j;
	var allLoaded = true;
	var req = new Object();
	if (pPackages.length>0)
	{
		req.pageset = pPageset;
		this.requestedPackages[this.requestedPackages.length] = req;
		for (i = 0; i < this.packages.length; i++)
			for (j = 0; j < pPackages.length; j++)
				if (pPackages[j] == this.packages[i].name)
				{
					req.packages = this.packages[i];
					if (!this.packages[i].loaded)
					{
						allLoaded = false;
						ctrl.addToLog("_CommonLibrary_loadPackages","controller","this.packages["+i+"].name: "+this.packages[i].name);
						//debug(""
						//	+"\nthis.packages["+i+"].scriptId: "+this.packages[i].scriptId
						//	+"\nthis.packages["+i+"].srcFile: "+this.packages[i].srcFile
						//	,"_CommonLibrary_loadPackages");
						//document.getElementsByTagName("head")[0].appendChild(document.createElement("script"));
						document.getElementById(this.packages[i].scriptId).src = this.packages[i].srcFile;
						//function fnLoadScript(url){
						//	var
						//		q='\u0022',
						//		d=document,
						//		n=navigator,
						//		e;
						//	if(/mac/i.test(n.platform)&&/msie/i.test(n.userAgent))// if ie5m, do it this way
						//		(d.createElement('div')).innerHTML='\u003cscript type='+q+t+q+' src='+q+url+q+'>\u003c/script>';
						//	else{
						//		(e=d.createElement('script')).src=url;
						//		e.type='text/javascript';
						//		d.getElementsByTagName('head')[0].appendChild(e);
						//	}
						//}
					}
				}
	}
	if (allLoaded)
		this.checkLoadedStatus();
}

/** control
 *	
 *	Invoked by the package after it has been loaded (Every package must have this call at the end of the script.)
 *		Ex; c.control("Parser", "loaded");
 */
function _CommonLibrary_control(pPackage, pStatus)
{
	var i;
	for (i = 0; i < this.packages.length; i++) {
		if (pPackage == this.packages[i].name) {
			if (pStatus == "loaded")
			{
				ctrl.addToLog("_CommonLibrary_control","controller","pPackage: "+pPackage+", pStatus: "+pStatus);
				this.packages[i].loaded = true;
				this.checkLoadedStatus();
				return;
			}
		}
	}
	alert("_CommonLibrary_control Package not found: " + pPackage
		+"\n\nVerify the package name in jscriptss/_Site.js (case sensitive!).");
}

/** checkLoadedStatus
 *	
 *	Invoked by the control or loadPackages
 *	Checks if all requested packages are loaded for each pageset
 *	If so, it calls the control function of the pageset that requested them.
 */
function _CommonLibrary_checkLoadedStatus()
{
	var i, j;
	var loaded = true;
	var removed=[];
	for (i = 0; i < this.requestedPackages.length; i++)
	{
		loaded = true;
		for (j = 0; j < this.requestedPackages[i].packages.length; j++)
			if (!this.requestedPackages[i].packages[j].loaded)
				loaded = false;
		if (loaded)
		{
			ctrl.setPSPackages(this.requestedPackages[i].pageset, true);
			if (typeof this.requestedPackages.splice!="undefined") {
				this.requestedPackages.splice(i,1);
			}else {
				removed[removed.length]=i;
			}
		}
	}
	if (typeof this.requestedPackages.splice=="undefined") {
		function isRemoved(idx) {
			for (var i=0;i<removed.length;i++) {
				if (removed[i]==idx) {
					return true;
				}
			}
			return false;
		}
		alert("IE5.0 array functionality is not included!\n\nDetected in CommonLibrary, which is IE5.0 compatible");
		var arr=[];
		for (var i=0;i<this.requestedPackages.length;i++) {
			if (!isRemoved(i)) {
				arr[j]=this.requestedPackages[i];
			}
		}
	}
}

