/*
 * sgLib
 * v2.0.0-pre1
 * Changelog:
   * 2.0.0-pre1: Initial development version.
 */

var preventScriptCache = true, preventCSSCache = false;
var scriptLoadList = [], scrLoadProg = 0, scriptsTimeout = 0, progBar, progBarInner, scriptsLoaded = false;

/*
 @Function:    scriptsLoader
 @Parameters:  ( str pathToFile1, str objectToCheck1[, str pathToFile2[, str objectToCheck2[, ...[, ...] )
 @Description: Asynchronously loads scripts or CSS. Called in the format: scriptsLoader('/path/to/file.ext', 'Object');
   objectToCheck refers to a JavaScript object that exists only in the file being called. For example, loading
   mootools.js will create a new object called MooTools; scriptsLoader knows mootools.js was loaded by checking if
   MooTools is undefined. When objectToCheck is "css", no checking is done, the file is merely loaded as a <link> css.
 */
function scriptsLoader() {
	if (arguments.length > 1) {
		for (var i = 0; i < arguments.length; i++) {
			if (arguments[i] == false) continue;
			scriptLoadList.push([arguments[i], arguments[++i]]);
		}

		progBar = document.createElement('div');
		progBarInner = document.createElement('span');
		//progBarInner.appendChild(document.createTextNode('Loading Scripts...'));
		progBarInner.innerHTML = 'Loading scripts...';
		progBar.className = 'progress';
		progBar.id = 'scriptsProgress';
		progBarInner.style.width = (scrLoadProg/scriptLoadList.length*100)+'%';
		progBar.appendChild(progBarInner);

		return window.setTimeout(scriptsLoader, 1);
	}

	if (typeof progBar == 'object' && (!progBar.parentNode || progBar.parentNode.nodeName != 'BODY')) {
		var bod = document.getElementsByTagName('body')[0];
		if (bod) {
			if (!scriptsLoaded && !/(^ preloading ?)|( ?preloading)/.test(bod.className)) bod.className += ' preloading nosgLib';
			// ie doesn't like this
			if (!window.ActiveXObject) bod.appendChild(progBar);
		}
	}

	if (scrLoadProg && scriptLoadList[scrLoadProg-1][1] != 'css' && eval('typeof '+scriptLoadList[scrLoadProg-1][1]) == 'undefined') {
		var scriptTimeout = new Date().getTime() - scriptsTimeout;
		if (scriptTimeout > 5000)
			dynamicScript(scriptLoadList[scrLoadProg-1][0]);
	} else if (scriptLoadList[scrLoadProg]) {
		if (scriptLoadList[scrLoadProg][0]) {
			progBarInner.innerHTML = 'Loading scripts ('+scriptLoadList[scrLoadProg][0].replace(/.*\/(.*?)(\.css)|(\.js).*/, '$1')+')...';
			if (scriptLoadList[scrLoadProg][1] == 'css') {
				dynamicCSS(scriptLoadList[scrLoadProg][0]);
			} else {
				dynamicScript(scriptLoadList[scrLoadProg][0]);
			}
			scrLoadProg++;
		} else {
			progBarInner.innerHTML = 'Loading scripts...';
			scrLoadProg += 2;
		}
		progBarInner.style.width = (scrLoadProg/scriptLoadList.length*100)+'%';
	} else scrLoadProg++;
	if (typeof scriptLoadList[scrLoadProg-1] == 'undefined') {
		//var outputMsg = "";
		//for (var i = 0; i < scriptLoadList.length; i++) {
		//	outputMsg += (scriptLoadList[i][1]+"="+eval('typeof '+scriptLoadList[i][1])+"\r\n");
		//}
		//alert(outputMsg);
		progBarInner.innerHTML = 'Complete.';
		new Fx.Tween(progBar, { 'property': 'top', 'duration': 500, onComplete: function() { progBar.destroy(); delete progBarInner; delete progBar; delete scriptsProgress; delete scriptsTimeout; } }).start(-2 * progBar.getSize().y);
		var bod = document.getElementsByTagName('body')[0];
		if (bod)
			bod.className = bod.className.replace(/(^ preloading ?)|( ?preloading)/, '');
		scriptsLoaded = true;
		scrLoadProg = scriptsTimeout = 0;
		scriptLoadList = [];
		return;
	}
	window.setTimeout(scriptsLoader, 25);
}
/*
 @Function:    dynamicScript
 @Parameters:  ( str scrPath )
 @Requires:    scriptsLoader
 @Description: Attempts to load the JS by adding a remote script tag in the head.
 */
function dynamicScript(scrPath) {
	scriptsTimeout = new Date().getTime();
	var scr = document.createElement('scr'+'ipt');
	scr.src = scrPath+(preventScriptCache?'?'+Math.random():'');
	scr.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(scr);
}
/*
 @Function:    dynamicCSS
 @Parameters:  ( str scrPath )
 @Requires:    scriptsLoader
 @Description: Attempts to load the CSS by adding a link stylesheet in the head.
 */
function dynamicCSS(scrPath) {
	scriptsTimeout = new Date().getTime();
	var scr = document.createElement('link');
	scr.rel = 'stylesheet';
	scr.href = scrPath+(preventCSSCache?'?'+Math.random():'');
	scr.type = 'text/css';
	scr.media = 'Screen';
	document.getElementsByTagName('head')[0].appendChild(scr);
}
/*
 @Function:    imgLoader
 @Parameters:  ( str basePath, str pathToFile1[, str pathToFile2[, ...]] )
 @Description: Preloads images, useful for preloading hover/down states. basePath is the root of all images, ie. /images/design/
 */
function imgLoader() {
	// <FF3 memory leak
	if (Browser.Engine.gecko == true && Browser.Engine.version < 19) return;
	if (arguments.length > 2) {
		var basePath = arguments[0];
		for (var i = 1; i < arguments.length; i++) {
			(new Image()).src=basePath+arguments[i];
		}
	}
}
var sclo=scriptsLoader, imglo=imgLoader;
