var isoYUILoader = function() {
	var baseURL = null;
	var modules = null;
	var loaded = false;
	var bodyAvailable = false;
	var loadEvent = null;
	var bodyAvailableEvent = null;
	var search = document.location.search;
	var mLog = search.match("[\?&]yuiLog=([^\&]*)");
	var showLogger = mLog!=null ? mLog[1]=="true" : false;
	var mDebug = search.match("[\?&]yuiDebug=([^\&]*)");
	var debug = mDebug!=null ? mDebug[1]=="true" : false;
	function init(require)
	{
		modules = require;
		var fileSuffix = debug?"-debug":"-min";
		var escURL = escape(baseURL);
		var m_js = ["yuiloader","event"];
		if (showLogger)
			m_js = m_js.concat(["dom","dragdrop","logger"]);
		var c = [];
		for (var i=0;i<m_js.length;i++)
			c = c.concat(["%3Cscript src=\"",escURL,m_js[i],"/",m_js[i],fileSuffix,".js\" type=\"text/javascript\"%3E%3C/script%3E"]);
		if (showLogger)
			c = c.concat(["%3Clink type=\"text/css\" rel=\"stylesheet\" href=\"",escURL,"logger/assets/skins/sam/logger.css\"/%3E"]);
		document.write(unescape(c.join("")));	
	}
	return {
		initPreview:function(require) {
			baseURL = window.external.getContext().tempoDir.getHttpUrlYUI()+"build/";
			init(require);
			this.init = null;
			this.initPreview = null;
		},
		init:function(base,sslBase,require) {
			baseURL = "https:" == document.location.protocol ? sslBase : base;
			init(require);
			this.init = null;
			this.initPreview = null;
		},
		load:function() {
			YAHOO.log("YUI init on "+baseURL,"info","isoYUILoader");
			bodyAvailableEvent = new YAHOO.util.CustomEvent("bodyAvailable");	/* fireOnce required last version */
			YAHOO.util.Event.onDOMReady(function () {
				var waitBody = function() {
					if (document.body == null)
						YAHOO.lang.later(10,null,waitBody);
					else
					{
						YAHOO.log("Body available","info","isoYUILoader");
						bodyAvailable = true;
						bodyAvailableEvent.fire();
					}	
				}
				waitBody();
			});
			if (showLogger)
			{
				this.onBodyAvailable(function() {
					var div = document.createElement("div");
					div.className = "yui-skin-sam";
					div.style.position = "absolute";
					div.style.left = "0";
					div.style.top = "0";
					var div2 = document.createElement("div");
					div.appendChild(div2);
					document.body.insertBefore(div,document.body.firstChild);
					new YAHOO.widget.LogReader(div2);
				});		
			}
			loadEvent = new YAHOO.util.CustomEvent("ready");	/* fireOnce required last version */
			var loader = new YAHOO.util.YUILoader({base: baseURL, require: modules, loadOptional: false, filter:debug?"DEBUG":null,
				onSuccess: function() {
					YAHOO.log("All loaded","info","isoYUILoader");
					loaded = true;
					loadEvent.fire();
				},
				onProgress: function(o) {   
					YAHOO.log("Loaded: "+ o.name,"info","isoYUILoader");
				},   
				onFailure: function(msg, xhrobj)
				{   
					var m = ["YUI LOAD FAILED: ",msg];
					if (xhrobj)
						m = m.concat([", ",YAHOO.lang.dump(xhrobj)]); 
					YAHOO.log(m.join(""),"error","isoYUILoader");  
				}
			});
			loader.insert();
			this.load = null;
		},
		onReady:function(fn,scope) {
			if (loaded) fn.call(scope);
			else loadEvent.subscribe(fn,scope);
		},
		onBodyAvailable:function(fn,scope) {
			if (bodyAvailable) fn.call(scope);
			else bodyAvailableEvent.subscribe(fn,scope);
		},
		onBodyContentReady:function(fn,scope) {
			this.onBodyAvailable(function () {
				YAHOO.util.Event.onContentReady(YAHOO.util.Dom.generateId(document.body), fn, scope);
			});
		}
	}
}();
