if (typeof(ebApiConfig) != 'object' || typeof(ebApiConfig.tools) == 'undefined') {
	alert('Um die EUROBAUSTOFF API-Tools nutzen zu können müssen Sie die gewünschten Tools über die JavaScript-Variable ebApiConfig.tools konfigurieren!');
} else {
	var ebApiLoader = {
		loadScript : function (url,el,as) {
			var ebj = document.createElement('script'); ebj.type = 'text/javascript'; ebj.async = as;
			ebj.src = url;
			if (el == 'body' && typeof(document.body) != 'object') { el = 'head' }
			document.getElementsByTagName(el)[0].appendChild(ebj);
		},
		loadCSS : function (url) {
			var ebj = document.createElement('link'); ebj.type = 'text/css'; ebj.rel = 'stylesheet'; ebj.media = 'all';
			ebj.href = url;
			document.getElementsByTagName('head')[0].appendChild(ebj);
		},
		onReady : function (fn, i) {
			if (typeof(jQuery) != 'function') {
				if (i < 100) { setTimeout(function(){ebApiLoader.onReady(fn,++i)},200) }
			} else {
				fn.call();
			}
		},
		loadTools : function(tools) {
			jQuery(document).ready(function () {
				if ((typeof(tools) == 'array' || typeof(tools) == 'object') && tools.length) {
					for(var it=0; it < tools.length; it++) {
						ebApiLoader.loadTool(tools[it]);
					};
				}
			});
		},
		loadTool : function(tool, callback) {
			if (jQuery.inArray(tool, ebApiLoader.loadedTools) == -1) {
				var appSettings = ebApps[tool];
				if (typeof(appSettings) == 'object' && typeof(appSettings.version) != 'undefined') {
					//ebApiLoader.loadScript(ebProtocol+ebApiDomain+ebToolsPath+ tool + '/v' + appSettings.version + '.app.js', 'body', true);
					jQuery.ajax(ebProtocol+ebApiDomain+ebToolsPath+ tool + '/v' + appSettings.version + '.app.js', {
						dataType:	'script',
						cache:		true,
						success:		function(data,status) {
							if (status == 'success' || status == 'notmodified') {
								ebApiLoader.loadedTools.push(tool);
								if (typeof(callback) == 'function') {
									setTimeout(function() {
											callback.call();
									}, 300);
								}
							}
						}
					});
/*
					jQuery.getScript(ebProtocol+ebApiDomain+ebToolsPath+ tool + '/v' + appSettings.version + '.app.js', 
						function() {
							ebApiLoader.loadedTools.push(tool);
							if (typeof(callback) == 'function') {
								callback.call();
							}
						}
					);
*/
				}
			} else if (typeof(callback) == 'function') {
				callback.call();	
			}
		},
		loadedTools : []
	}, ebApiDomain = 'fachhaendler.eurobaustoff.de/', ebProtocol = (document.location.protocol == 'https:' ? 'https:' : 'http:')+'//', ebApiPath = 'api/', ebToolsPath = 'api/tools/', ebLoadJS = [],
		ebApps = {
			'beilage' : {
				'version' : 1
			},
			'content': {
				'version' : 1
			},
			'search'	: {
				'version' : 2	
			}
		};

	if (typeof(ebApiConfig.domain) == 'string') {
		ebApiDomain = ebApiConfig.domain;	
	}
	
	var ebInitTools = function() {
		for(var it=0; it<ebLoadJS.length; it++) {
			jQuery.ajax(ebLoadJS[it], {
				dataType:	'script',
				cache:		true
			});
		}
		ebApiLoader.loadTools(ebApiConfig.tools);
	}

	if (typeof(jQuery) != 'function' || typeof(jQuery.event.special.swipe) != 'function') {
		ebLoadJS.push(ebProtocol + ebApiDomain + ebApiPath + 'js/jQueryTouchSupport.js');
	}
	ebLoadJS.push(ebProtocol + ebApiDomain + ebApiPath + 'js/common.js');

	if (typeof(jQuery) != 'function') {
		ebApiLoader.loadScript(ebProtocol + 'ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', 'head', true);
		ebApiLoader.onReady(function() {
			jQuery.noConflict();
			ebInitTools();
		},0);
	} else {
		ebInitTools();	
	}
}
