(function(){
// No dojo modules are loaded, need to do everything by hand....
//   see http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format/4673436#4673436
var format = function(){
   var args = arguments;
   return this.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined' ? args[number] : match;    
   });
};

var data = {
	"product": "Link|NXG",
	"disclaimer" : "<b>DISCLAIMER:</b> {0}, makes no representation or warranties, express or implied, with respect to the use or reuse of the data provided herewith, regardless of its format or the means of its transmission.  THE DATA IS PROVIDED \"AS IS\" WITH NO GUARANTEE OR REPRESENTATION ABOUT THE ACCURACY, CURRENCY, SUITABILITY, PERFORMANCE, MERCHANTABILITY, RELIABILITY, OR FITNESS OF THE DATA FOR ANY PARTICULAR PURPOSE. {0}, shall not be liable for any direct, indirect, special, incidental, compensatory or consequential damages or third party claims resulting from  the use of this data, even if {0}, has been advised of the possibility of such potential loss or damage. This data may not be used in states that do not allow the exclusion or limitation of incidental or consequential damages.",
	"title": 'Welcome to {0}{1} GIS <span class="role"></span>Map Service'
};

// Alternate title text: <span class="client"></span> <span class="product">LINK</span>

// Merge with the current app config
appconfig = dojo.mixin(appconfig || {}, data);

// Query for the nodes to populate with the custom data
var titleNode = dojo.query("#preloader .title")[0];
if ( titleNode ) {
	dojo.attr( titleNode, "innerHTML", format.call(appconfig.title || "", appconfig.client || "", appconfig.clientNameModifier || "" ));
}

var clientNode = dojo.query("#preloader .client")[0];
var productNode = dojo.query("#preloader .product")[0];
var disclaimerNode = dojo.query("#preloader .disclaimer")[0];
var roleNode = dojo.query("#preloader .role")[0];

if ( clientNode ) { dojo.attr( clientNode, "innerHTML", appconfig.client ); }
if ( productNode ) { dojo.attr( productNode, "innerHTML", appconfig.product ); }

if ( disclaimerNode ) { 
	dojo.attr( disclaimerNode, "innerHTML", format.call( appconfig.disclaimer || "", appconfig.client || "" )); 
}

// Make an XHR request for the uses role and then change the splash screen based on
// the current user.  We have to do it this way because the Link admin tools can have
// custom loginc to override or otherwise add roles to users.
// Role values should be all lowercase the function normalizes the user's roles.
var roleMessages = [ 
	{ "role" : "subscriber", "message" : "Subscription" },
	{ "role" : "land", "message" : "Internal" },	
	{ "role" : "intranet", "message" : "Internal" },
	{ "role" : "internal", "message" : "Internal" },
	{ "role" : "*", "message" : "Public" }
];

dojo.when( getJson( appconfig.roles ), function( result ) {

	// Run through the roles are take the first match
	var message = "";	
	if ( result && dojo.isArray( result.roles )) {
	
		// Append a wildcard
		result.roles.push("*");		
	
		// Build a role message index table
		var lut = {};
		dojo.forEach( roleMessages, function(msg, idx) { lut[msg.role] = idx; });
		
		// Get the message index of each role
		var indices = dojo.map( result.roles, function(role) {
			var index = lut[role.toLowerCase()];
			return (typeof index !== "undefined") ? index : Infinity;
		});
		
		// Take the one with the highest index
		var best = Math.min.apply(Math, indices);
		message = (roleMessages[best] && roleMessages[best].message) || message;
	}
		
	if ( message && roleNode ) {
		dojo.attr( roleNode, "innerHTML", message + " " );
	}
});
})();