// Mobular bootstrap ... this will read the url for any config params 
var qString = location.search;			

// this sets the version. May be changed by a build process.
var dPath = "js/dojo/dojo.js";

// for mobular specific configurations that should be used during this session or helpful functions that need to exist before
// any of the mobular toolkit loads.
eengineConfig = {
	dojoModule: dPath,
	UI: '',
	LM: '',
	LIB: 0,
	serialNum: '',
	buildSettings: {},
	dataSources: [],
	loadedCssPaths: [],
	isDebug: false,
	// debugging wrappers
	log: function(msg) {
			/*
			var dv = document.getElementById('TracingEventWindow');
			if (! dv ) {
				var border = document.createElement('div');
				border.setAttribute('id','EventsTitle');
				border.innerHTML = "<hr><b>Engine Events</b><hr>";

				document.body.appendChild(border);

				var divTag = document.createElement('div');
				divTag.setAttribute('id','TracingEventWindow');
				divTag.setAttribute('style','width:600px;height:300px;padding : 2px 2px 2px 2px;overflow:auto;');
				
				document.body.appendChild(divTag);
				
				// assign to handle
				dv = divTag;
				
				border = document.createElement('div');
				border.setAttribute('id','EventsFooter');
				
				document.body.appendChild(border);			
			}
		dv.innerHTML += msg + "<br>";
		*/
		
		// basic wrapper to log to firebug if debugging is active
		if ( eengineConfig.isDebug ) {
			try {
				console.log(msg);
			} catch (e) {}
		}
	},
	subscriptionList: [],	
	subscribe: function (callback, widget) {
		var subscription = {
			callback: '',
			widget: ''
		};
		
		/*
			this is provided in the bootstrap to handle subscriptions on the page. 
			It can act as a simple means of subscribing to all widgets or just to specific ones.
		*/
		var add = 1;		
		
		if (callback) {
			subscription.callback = callback;
			
			if (widget) {
				subscription.widget = widget;
			}			
			
			// make sure we've not done this one already.
			for ( var s=0; s < eengineConfig.subscriptionList.length; s++ ) {
				var sub = eengineConfig.subscriptionList[s];
			
				if ( sub.callback.toLowerCase() == callback.toLowerCase() ) {
					// do not add
					add = 0;
					break;
				}
			}			
			
			if (add==1) {
				// add to collection
				eengineConfig.subscriptionList[eengineConfig.subscriptionList.length] = subscription;			
			}	
		} else {
			// must have a callback specified!
			return;
		}
	},
	addInBinder: function() {
		// browsers load the js libraries at different rates. Beacuse
		// of that we cannot count on 'when' a given fileset loads. This
		// waits until we have what we need.
		try {
			if ( (eengineConfig._kitting)
				 && (eengineConfig._paramCollection) 
				 && (eengineConfig._hybrid) 
				 && (eengineConfig._extensions)
				 && (dojo) 
				 && ( mobular.common.general ) ) {
				 	// we will always need this... require is.
					dojo.require("dojo.parser");					
				 	dojo.require("mobular.layout.binder");
					dojo.require("display.messageBox.MsgBox");	
					dojo.require("display.microview.microview");	
					dojo.require("display.loading.loadingDialog");			
					
					// shouldn't be there yet... go ahead and load.	
					eengineLoading.open();
					
					binder = new binder({ id: 'kit' }, dojo.byId('kit'));
			} else {
				setTimeout("eengineConfig.addInBinder()",200);
			}			
		} catch (e) {
			setTimeout("eengineConfig.addInBinder()",200);
		}
	},
	_components: {
		_kitting: false,
		_paramCollection: false,
		_hybrid: false,
		_extensions: false
	}
};

// create the loading screen object.
// Right now, this is looking for a tag named 'loading_dialog' that is attached to the dijit.Dialog
// namespace. 
// the styles for Dialog are expdected to be in ecat.css
// TODO: Add deeper mechanism to control message and behavior of this?
var eengineLoading = {
	_templateText: 'Please be patient ... ',
	_templateString: '<div style="margin-top: 15px; margin-bottom: 15px; width: 200px; text-align:center;">[[TXT]]</div><div style="margin-top: 15px; margin-bottom: 15px; "><img src="images/loading.gif" alt="" /></div>',
	_title: 'Loading',
	_removeDialog: function() {
		try {
			if  ( dijit.byId('loading_dialog') ) {
			   dijit.byId('loading_dialog').hide();
			} else {				
			   setTimeout("eengineLoading._removeDialog()", 1000);					
			}				
		} catch (e) {}
	},
	_openDialog: function() {	
		if ( g_errMsg ) {
			// pull in custom message
			try {
				eengineLoading._templateText = g_errMsg[29];
			} catch (e) {}
		}

		var _param = {
			_templateText: eengineLoading._templateText,
			_templateString: eengineLoading._templateString,
			_title: eengineLoading._title
		}
	
		new display.loading.loadingDialog(_param);
	},
	open: function() {
		if ( display.loading.loadingDialog ) {
			dojo.require("display.loading.loadingDialog");			
			setTimeout("eengineLoading._openDialog()", 300);
		} else {
			if (! window.eengine) {
				this._openDialog();
			}
		}
	},
	close: function() {
		setTimeout("eengineLoading._removeDialog()", 1000);	
	}
};

// in IE, the cross-browser firebug doesn't load in a timely manner, so lets give it someplace to play ahead of time.
// with FF3, we don't need this so we trap for it.
try {
	console = [];	
} catch (e) {}

/* write out references to page */
// write out mapping between toolkit and baseline
document.write('<scr' + 'ipt type="text/javascript" language="Javasc' + 'ript" SRC="js/mobular/kitting.js"></scr' + 'ipt>');

// write out mapping between toolkit and baseline
document.write('<scr' + 'ipt type="text/javascript" language="Javasc' + 'ript" SRC="js/mobular/paramCollection.js"></scr' + 'ipt>');

// write out mapping between toolkit and baseline
document.write('<scr' + 'ipt type="text/javascript" language="Javasc' + 'ript" SRC="js/mobular/hybrid.js"></scr' + 'ipt>');

// write out string extensions
document.write('<scr' + 'ipt type="text/javascript" language="Javasc' + 'ript" SRC="js/mobular/extensions.js"></scr' + 'ipt>');

// write out dojo ref
document.write('<scr' + 'ipt type="text/javascript" language="Javasc' + 'ript" SRC="' + eengineConfig.dojoModule + '" djConfig="isDebug: false, usePlainJson: true, parseOnLoad: true"></scr' + 'ipt>');

// activate the 'addInLoad' to check for our libraries
eengineConfig.addInBinder();


