(function() {
	// a list of YouDiligence pages
	// and how they can be identified
	var PAGES = {
		// pages that don't require a login
		'Login': 				'loginPage',
		'yd/HowItWorks': 		'howItWorksPage',
		'yd/Internet-Safety': 	'socialFeedPage',
		'yd/Pricing': 			'pricingPage',
		'yd/FAQ': 				'faqPage',
		'yd/ContactUs': 		'contactUsPage',
		'yd/SignUp': 			'signUpPage',
		
		// pages that require a user login
		'yd/Reports': 		'reportsPage',
		'yd/MyAccount': 	'myAccountPage',
		'yd/Friends': 		'friendsPage',
		'yd/ViewReport':	'viewReportPage'
	};
	
	var EVENTS = {
		'PAGE_LOADED': 			'pageLoaded',
		'ADD_MODULE': 			'addModule',
		'ALL_MODULES_LOADED': 	'allModulesLoaded',
		'MODULE_LOADED': 		'moduleLoaded'
	};
	
	var ROOT = documentRoot + scriptsDir + 'yd';
	var MODULES = {
		'YD_PANEL_SKIN': {
			name: 'yd-panel-skin',
			type: 'css',
			fullpath: ROOT + '/bc/yd-panels/assets/css/yd-panel.css'
		},
		'YD_PANEL': {
			name: 'yd-panel',
			type: 'js',
			fullpath: ROOT + '/bc/yd-panels/yd-panel.js',
			requires: ['container','yd-panel-skin'],
//			removed dragdrop, it's extraordinarily expensive to operate
			optional: ['dragdrop','animation'] 
		},
		'YD_CONFIG': {
			name: 'yd-config',
			type: 'js',
			fullpath: ROOT + '/bc/config/config.js',
			requires: ['container']
		},
		'YD_OFFENDING_TERMS': {
			name: 'yd-offendingterms',
			type: 'js',
			fullpath: ROOT + '/bc/yd-panels/yd-offendingterms.js',
			requires: ['yd-panel','button']
		},
		'YD_ADDRESS_BOOK': {
			name: 'yd-addressbook',
			type: 'js',
			fullpath: ROOT + '/bc/yd-panels/yd-addressbook.js',
			requires: ['yd-panel','datatable','connection']
		},
		'DWR_ACTOR_WRAPPER': {
			name: 'dwr-actor-wrapper',
			type: 'js',
			fullpath: ROOT + '/bc/dwr/dwr-actor-wrapper.js'
		},
		'REPORTS_PAGE_FUNCTIONS': {
			name: 'reports-page-functions',
			type: 'js',
			fullpath: ROOT + '/reports-page-functions.js'
		},
		
		'JSONP': {
			name: 'jsonp',
			type: 'js',
			fullpath: ROOT + '/bc/io/jsonp.js'
		},
		'YD_NETWORK_MANAGER': {
			name: 'yd-network-manager',
			type: 'js',
			fullpath: ROOT + '/bc/yd-panels/list.js',
			requires: ['yd-panel', 'button','yd-network-panels','yd-network-editors','dwr-actor-wrapper']
		},
		'YD_NETWORK_PANELS': {
			name: 'yd-network-panels',
			type: 'js',
			fullpath: ROOT + '/bc/yd-panels/panels.js',
			requires: ['yd-network-editors']
		},
		'YD_NETWORK_EDITORS': {
			name: 'yd-network-editors',
			type: 'js',
			fullpath: ROOT + '/bc/yd-panels/editors.js',
			requires: ['jsonp']
		}
	};
	
	var LOADER = null;
	var pagesetup = function() {
		this.init();
	};
	pagesetup.prototype = {
		initDefaultEvents: function() {
			// initialize events
			var id = '';
			for(id in PAGES) {
				this[PAGES[id]+'Event'] = this.createEvent(PAGES[id]);
			}
			
			for(id in EVENTS) {
				this[EVENTS[id]+'Event'] = this.createEvent(EVENTS[id]);
			}
		},
		init: function() {
			var that = this;
			this.initDefaultEvents();

//			useful for debugging
			LOADER = new YAHOO.util.YUILoader({ base: ROOT + '/yui/' });
//			LOADER = new YAHOO.util.YUILoader({ filter: 'DEBUG', base: ROOT + '/yui' });
//			LOADER = new YAHOO.util.YUILoader({ filter: 'RAW', base: ROOT + '/yui' });
//			LOADER = new YAHOO.util.YUILoader();
			this.initModules();
			
			LOADER.onSuccess = function() {
				that.fireEvent(EVENTS.ALL_MODULES_LOADED);
			};
			LOADER.onProgress = function(args) {
				that.fireEvent(EVENTS.MODULE_LOADED, args);
			};
			YAHOO.util.Event.onDOMReady(this.go, this, true);
			
		},
		initModules: function() {
			for(var moduleName in MODULES) {
				var module = MODULES[moduleName];
				
				LOADER.addModule(module);
			}
		},
		go: function() {
			var location = window.location.pathname;
			var extensionIndex = location.lastIndexOf('.');
			
			if(extensionIndex > 0) {
				location = location.substring(0,extensionIndex);
			}
			location = location.split('/');
			location = location[location.length - 2] +
				'/' + location[location.length - 1];
			
			if(typeof PAGES[location] !== 'undefined') {
				this.fireEvent(PAGES[location]);
			}
			
			this.fireEvent(EVENTS.PAGE_LOADED, PAGES[location]);
			this.loadModules();
		},
		addModule: function(o) {
			LOADER.addModule(o);
		},
		requireModule: function(module) {
			LOADER.require(module);
		},
		loadModules: function() {
//			LOADER.combine = true;
			
			LOADER.insert();
		}
	}
	YAHOO.lang.augment(pagesetup, YAHOO.util.EventProvider);

	PAGESETUP = new pagesetup();
})();