(function () {
	
	// convient variables
	var Dom = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var Lang = YAHOO.lang;
	var Widget = YAHOO.widget;

	var Util = LIGHTHOUSE.util;
	var Animation = LIGHTHOUSE.animation;
	var Config = LIGHTHOUSE.config;
	var Plugins = LIGHTHOUSE.widgetPlugins;

	LIGHTHOUSE.users = function(){
		
		var dialog = LIGHTHOUSE.templates.dialog;
		var editSettings = document.getElementById('edit-settings-dialog');
		var cashEarned = document.getElementById('cash-earned-dialog');

		
		return {
			
			database : new Util.DatabaseInterface('books', 'U'),  // creates a CRUD interface
			
			eventSubscribers : {
				onShowSettings : function(target, rowId){
					dialog.reset();
					dialog.setBody(editSettings);
					dialog.setHeader('Edit Your Settings');
					dialog.show();
				},
				onShowCash : function(target, rowId){
					dialog.reset();
					dialog.setBody(cashEarned);
					dialog.setHeader('Cash Earned');
					dialog.show();
				},
				onLogout : function(target){
					window.location = target.getAttribute('href');
				},
				// as a database function it is passed event information instead of display information
				// this should aid in abstracting the actual update function from
				// the function that may show the form that updates, or prepares the form for
				// updating, ect..
				onUpdateSuccess : function(o){
					if(o.isFromServer){
						dialog.setBody(o.response.html);
						dialog.setPermanentBody(o.response.html);
						Dom.removeClass(dialog.getBodyElement(), 'loading');
						dialog.hide();

						// messages that should update/change based on user data.. like the "no paypal account" notice in the author admin
						if(o.response.user.paypal_account != ""){
							Dom.getElementsByClassName('no-paypal-account', null, document, function(element){
								Dom.addClass(element, 'hide');						
							});
						}
					}else{
						Dom.addClass(dialog.getBodyElement(), 'loading');
					}
				},
				onUpdateFailure : function(o){
					Dom.removeClass(dialog.getBodyElement(), 'loading');
					if(o.response.html){
						dialog.setBody(o.response.html); // display the new form, with errors
					}else{ // the server returned a failure, but without json data
						dialog.setBody("There has been a connection error. Sorry about that."); // display the new form, with errors
					}
					dialog.show(); // the slim chance that the user closed the dialog before the response was returned
				},
				onRequestHelp : function(target, rowId){
					Zenbox.show();
				}
			}
		};
	}();
	// helper function to auto subscribe the eventSubscribers 
	// to the database events (based on member name) onThing maps to thing
	Util.setSubscribers(LIGHTHOUSE.users.database.events, LIGHTHOUSE.users.eventSubscribers);

	LIGHTHOUSE.books = function(){


		var dialog = LIGHTHOUSE.templates.dialog;
		var sellBook = document.getElementById('sell-book-dialog');
		var _template;
		return {
			
			database : new Util.DatabaseInterface('books', 'CRU'),  // creates a CRUD interface
			
			eventSubscribers : {
				updateLoad : function(template){
					var editorInstance;
					switch(template){
						case "update-content":
							
							if(document.getElementById('editor-book-content')){ // This is the old editor.. I added the IF statement
																				// so that if you want to test/see the old one, just uncomment
																				// the HTML and it should load fine.
								editorInstance = new Widget.Editor('editor-book-content', Config.richTextEditor.content);
								var beforeAutoSave = function(){ editorInstance.saveHTML(); };
								Util.autoSave('book-content-form', Config.autoSaveInterval, 'book-content-form-autosave', beforeAutoSave); 
							}
							break;
						case "update-settings":
							editorInstance = new Widget.Editor('editor-description', Config.richTextEditor.standard);
							LIGHTHOUSE.admin.eventSubscribers.onToggleClickbankSettings(document.getElementById('enable_clickbank_settings_checkbox'));
							break;
					}
					//editorInstance.on('editorContentLoaded', Util.applyCurrentStylesToRTE, editorInstance);
					//editorInstance.on('toolbarLoaded', Plugins.Editor.insertVideoLoader);
					//editorInstance.on('beforeOpenWindow', Config.richTextEditor.autoHeightFix.open);
					//editorInstance.on('closeWindow', Config.richTextEditor.autoHeightFix.reset);
					
					//editorInstance.render();
					
				},
				onUpdateSuccess : function(o){

				},
				onPrint : function(target){
					window.print();
				},
				onSell : function(target){
					dialog.reset();
					dialog.setHeader('You can sell this book, and make some cash!');
					dialog.setBody(sellBook);
					dialog.show();
				},
				readLoad : function(target){
					document.body.onselectstart = function() { return(false); }; // IE hack
				},
				previewLoad : function(target){
					document.body.onselectstart = function() { return(false); }; // IE hack
				}
			}
		};
	}();
	Util.setSubscribers(LIGHTHOUSE.books.database.events, LIGHTHOUSE.books.eventSubscribers);

	LIGHTHOUSE.admin = function(){
		
		return {
			
			database :{},  // creates a CRUD interface
			
			eventSubscribers : {
				emailLoad : function(template){
					editorInstance = new Widget.Editor('editor-message', Config.richTextEditor.standard);
					editorInstance.on('editorContentLoaded', Util.applyCurrentStylesToRTE, editorInstance);
					editorInstance.on('toolbarLoaded', Plugins.Editor.insertVideoLoader);
					editorInstance.on('beforeOpenWindow', Config.richTextEditor.autoHeightFix.open);
					editorInstance.on('closeWindow', Config.richTextEditor.autoHeightFix.reset);
					
					editorInstance.render();
				},
				onToggleClickbankSettings : function(target){
					if(target.checked){
						Dom.removeClass(document.getElementById('clickbank_account_li'), "hide-if-javascript");	
						Dom.removeClass(document.getElementById('clickbank_secret_key_li'), "hide-if-javascript");	
						Dom.removeClass(document.getElementById('clickbank_item_number_li'), "hide-if-javascript");	
						Dom.removeClass(document.getElementById('clickbank_notice'), "hide-if-javascript");	
					}else{
						Dom.addClass(document.getElementById('clickbank_account_li'), "hide-if-javascript");	
						Dom.addClass(document.getElementById('clickbank_secret_key_li'), "hide-if-javascript");	
						Dom.addClass(document.getElementById('clickbank_item_number_li'), "hide-if-javascript");	
						Dom.addClass(document.getElementById('clickbank_notice'), "hide-if-javascript");	
					}
					return true;
				}
			}
		}
	}();

	LIGHTHOUSE.container = function(){
		
		return {
			
			database :{},  // creates a CRUD interface
			
			eventSubscribers : {
				afterSelectChangeLanguage : function(target, tableName){
					target.form.submit();
				}
			}
		}
	}();

})(); 
