var aougScriptLoader = {

	debug : false,
	
	basePath : '/js/',
	
	loaded : new Array(),
	waitingQueue : new Array(),
	
	readyScriptsRequested : false,
	scriptsRequested : 0,
	ready : false,
	
	version : '08042011', 
	enableTwisting : false, //?ver=

	scripts : {
		
		ie : [
			'ie7_zIndex.js'/*,					// z-index fix to correct the bug below IE7
			'iepngfix_tilebg.js',				// PNG transparency fix
			'jquery.bgiframe.js',				// iframe z-index fix
			'minmax.js'							// allows min/max-width in IE*/
		],
		
		lib : [
			'jquery.loading.js', 				// loading field
			'jquery.MultiFile.js', 				// multi file upload
			'jquery.lightbox-0.5.js',			// lightbox gallery
			'jquery.backgroundPosition.js',		// allows background-position effects
			'jquery.smoothanchors2.js', 		// scrolls to anchors
			'jquery.scrollTo.js',				// scrolling effects
			'jquery.tablesorter.js',			// allows to sort tables with javascript
			'jquery.tablesorter.pager.js',		// paging for tables
			'tablesorter_filter.js',			// to filter tables
			'jquery.metadata.js',				// to create own table sortings
			'farbtastic.js'					// color picker				
		],
		
		portal : [
			'aoug_general.js',
			'aoug_page_index.js',
			'aoug_ui_objects.js',
			'aoug_global_objects.js'
		],	
		
		pages : {
			Register : [
				'aoug_page_register.js'
			],			
			Account : [
				'aoug_page_account.js'
			],			
			Event : [
				'aoug_page_event.js'
			]
			
		},
		
		backend : [
			'aoug_admin_general.js',
			'aoug_tinymce.js'
		],
		
		admin : {
			'Person' : [
				'aoug_admin_person.js'
			],
			'Sponsor' : [
				'aoug_admin_sponsor.js'
			],
			'Subscription' : [
				'aoug_admin_subscription.js'
			],
			'Themeboard' : [
				'aoug_admin_themeboard.js'
			],
			'Event' : [
				'aoug_admin_event.js'
			],
			'Newsletter' : [
				'aoug_admin_newsletter.js'
			],
			'Company' : [
		
			],
			'Committee' : [
			
			],
			'Server' : [
				'aoug_admin_server.js'
			]		
		}
				
	},
	
	loadScriptsArray : function(scripts, path) {
		$.each(scripts, function(index, value) {
		
			if ($.inArray(path + value, aougScriptLoader.loaded)) {
				
				var scriptUrl = path + value;
				aougScriptLoader.scriptsRequested++;
				
				$.ajax({
					type : 'get',
					url : scriptUrl,
					success : function(data, textStatus) { 
						if (textStatus == 'success') {
							aougScriptLoader.loaded.push(scriptUrl);
							aougScriptLoader.scriptsRequested--;
							
						} else {
							alert('Loading of script failed: ' + scriptUrl); 
						}
					},
					dataType : 'script',
					cache : true
				});
			}
			
		});
	},
	
	/* public load functions */
	
	loadLibrary : function(path) {
		aougScriptLoader.loadScriptsArray(aougScriptLoader.scripts.lib, path + aougScriptLoader.basePath + 'lib/');
	},
	
	loadIEbugfix : function(path) {
		aougScriptLoader.loadScriptsArray(aougScriptLoader.scripts.ie, path + aougScriptLoader.basePath + 'ie/');
	},
	
	loadPortal : function(path) {
		aougScriptLoader.loadScriptsArray(aougScriptLoader.scripts.portal, path + aougScriptLoader.basePath + 'portal/');
	},
	
	loadPage : function(path, page) {
		if (page in aougScriptLoader.scripts.pages)
			aougScriptLoader.loadScriptsArray(aougScriptLoader.scripts.pages[page], path + aougScriptLoader.basePath + 'page/');
	},
	
	loadBackend : function(path) {
		aougScriptLoader.loadScriptsArray(aougScriptLoader.scripts.backend, path + aougScriptLoader.basePath + 'backend/');
	},
	
	loadAdmin : function(path, page) {
		if (page in aougScriptLoader.scripts.admin)
			aougScriptLoader.loadScriptsArray(aougScriptLoader.scripts.admin[page], path + aougScriptLoader.basePath + 'admin/');
	},
	
	/* / public load functions */
	
	wait : function() {
		if(aougScriptLoader.readyScriptsRequested && aougScriptLoader.scriptsRequested == 0) {
			aougScriptLoader.ready = true;
			$.each(aougScriptLoader.waitingQueue, function() {
				this();
			});
		}
		else {
			setTimeout('aougScriptLoader.wait()', 100);
		}
	},
	
	execute : function (func) {
		if(!aougScriptLoader.ready) {
			aougScriptLoader.waitingQueue.push(func);
		}
		else {
			func();
		}
	}

}

/* auto load */
$(document).ready(function() {
	/* get page */
	var path = window.location.pathname.replace(pageUrl, '').split('/');

	/*if ($.browser.msie && ($.browser.version.substring(0, 1) == '6' || $.browser.version.substring(0, 1) == '7')) {
		return;
	}
	else {*/
		aougScriptLoader.readyScriptsRequested = false;

		/* load basic librarys */
		aougScriptLoader.loadLibrary(pageUrl);
		aougScriptLoader.loadPortal(pageUrl);
		

		if (path.length > 0) {
			if (path[1] == 'Admin') {
				aougScriptLoader.loadBackend(pageUrl);
				aougScriptLoader.loadAdmin(pageUrl, path[2]);
			} else {
				aougScriptLoader.loadPage(pageUrl, path[1]);
			}
		}
		
		aougScriptLoader.readyScriptsRequested = true;
		
		/* wait until load is done */
		aougScriptLoader.wait();
	//}
});
