var ModalLoginManager = {
		
	UA_FE_APP : "/ua-fe/modal.shtml",
	
	UA_FE_MODAL_LOGIN : "/ua-fe/do-login-modal.shtml",
	
	elements : {
		DNI_MODAL_CONTAINER : "dni-modal-container",
		MODAL_INNER_CONTAINER : "modal-inner-container",
		LOGIN_MODAL_OVERLAY : "login-modal-overlay",
		DNI_LOGIN_FORM : "dnilogin-form",
		FB_LOGIN_FORM : "fblogin-form",
		DNI_TAB : "login-modal-discovery-account",
		FB_TAB : "login-modal-facebook-account",
		DNI_EMAIL_INPUT_CONT : "dni-login-input-email-container",
		DNI_EMAIL_INPUT : "dnilogin-input-email",
		DNI_EMAIL_ERROR : "dnilogin-input-email-error",
		DNI_PASSWORD_INPUT_CONT : "dni-login-input-password-container",
		DNI_PASSWORD_INPUT : "dnilogin-input-password",
		DNI_PASSWORD_ERROR : "dnilogin-input-password-error",
		DNI_REMEMBER_INPUT : "dnilogin-input-remember",
		DNI_LOGIN_ERROR_MESSAGE : "dnilogin-login-error-message",
		MODAL_DIALOG_CONTAINER : "modal-dialog-container"
	},
	
	util : {
		assignControllers : function(elements) {
			for (var element in elements) {
				var eventProperties = elements[element];
				var event = eventProperties[0];
				var fn = eventProperties[1];
				var object = document.getElementById(element);
				ModalLoginManager.util.addEventListener(object, event, fn);
			}
		},
		addEventListener : function(element, event, fn) {
			if (element != null && event != null && fn != null){
				if (element.addEventListener) {
					element.addEventListener(event,fn,false);
				} else {
					element.attachEvent('on'+event,fn);
				}
			}
		}
	},
	
	uiFunctions : {
		getLoginModalDni : function() {
			if(document.getElementById('login-modal-form') != null) {
				ModalLoginManager.uiFunctions.displayLoginModalDni();
			}else{
				ModalLoginManager.uiFunctions.displayLoginModalContainer();
				ModalLoginManager.dataFunctions.getModalAJAX(ModalLoginManager.uiFunctions.displayLoginModalDni);
			}
		},
		getLoginModalFb : function() {
			if(document.getElementById('login-modal-form') != null) {
				ModalLoginManager.uiFunctions.displayLoginModalFb();
			}else{
				ModalLoginManager.uiFunctions.displayLoginModalContainer();
				ModalLoginManager.dataFunctions.getModalAJAX(ModalLoginManager.uiFunctions.displayLoginModalFb);
			}
		},
		displayLoginModalContainer : function() {
			var dialogContainer = document.getElementById(ModalLoginManager.elements.MODAL_DIALOG_CONTAINER);
			dialogContainer.style.height = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight) + "px";
			dialogContainer.style.width = document.body.parentNode.scrollWidth + "px";
			
			var modalContainer = document.getElementById(ModalLoginManager.elements.DNI_MODAL_CONTAINER);
			modalContainer.style.display = 'block';
		},
		hideLoginModalContainer : function() {
			var modalContainer = document.getElementById(ModalLoginManager.elements.DNI_MODAL_CONTAINER);
			modalContainer.style.display = 'none';
			document.getElementById(ModalLoginManager.elements.DNI_EMAIL_INPUT).value = '';
			document.getElementById(ModalLoginManager.elements.DNI_PASSWORD_INPUT).value = '';
		},
		displayLoginModalDni : function() {
			ModalLoginManager.uiFunctions.refreshForm();
			ModalLoginManager.uiFunctions.displayDniLogin();
			ModalLoginManager.uiFunctions.displayLoginModalContainer();
		},
		displayLoginModalFb : function() {
			ModalLoginManager.uiFunctions.refreshForm();
			ModalLoginManager.uiFunctions.displayFbLogin();
			ModalLoginManager.uiFunctions.displayLoginModalContainer();
		},
		displayDniLogin : function() {
			document.getElementById(ModalLoginManager.elements.DNI_LOGIN_FORM).style.display = 'block';
			document.getElementById(ModalLoginManager.elements.FB_LOGIN_FORM).style.display = 'none';
			document.getElementById(ModalLoginManager.elements.DNI_TAB).className = 'active';
			document.getElementById(ModalLoginManager.elements.FB_TAB).className = 'inactive';
		},
		displayFbLogin : function() {
			document.getElementById(ModalLoginManager.elements.DNI_LOGIN_FORM).style.display = 'none';
			document.getElementById(ModalLoginManager.elements.FB_LOGIN_FORM).style.display = 'block';
			document.getElementById(ModalLoginManager.elements.DNI_TAB).className = 'inactive';
			document.getElementById(ModalLoginManager.elements.FB_TAB).className = 'active';
		},
		checkform : function(evt) {
			ModalLoginManager.uiFunctions.refreshForm();
			var valid = true;
			if (document.getElementById(ModalLoginManager.elements.DNI_EMAIL_INPUT).value == '') {
				document.getElementById(ModalLoginManager.elements.DNI_EMAIL_INPUT_CONT).className = 'invalid';
				document.getElementById(ModalLoginManager.elements.DNI_EMAIL_ERROR).style.display = 'block';
				valid = false;
			}
			if (document.getElementById(ModalLoginManager.elements.DNI_PASSWORD_INPUT).value == '') {
				document.getElementById(ModalLoginManager.elements.DNI_PASSWORD_INPUT_CONT).className = 'invalid';
				document.getElementById(ModalLoginManager.elements.DNI_PASSWORD_ERROR).style.display = 'block';
				valid = false;
			}
			if(evt.preventDefault) evt.preventDefault();
			evt.returnValue = false;
			if(valid){
				ModalLoginManager.dataFunctions.getLoginAJAX(null);
			}
		},
		displayLoginError : function() {
			document.getElementById(ModalLoginManager.elements.DNI_LOGIN_ERROR_MESSAGE).style.display = 'block';
		},
		refreshForm : function() {
			document.getElementById(ModalLoginManager.elements.DNI_EMAIL_INPUT_CONT).className = 'valid';
			document.getElementById(ModalLoginManager.elements.DNI_PASSWORD_INPUT_CONT).className = 'valid';
			document.getElementById(ModalLoginManager.elements.DNI_EMAIL_ERROR).style.display = 'none';
			document.getElementById(ModalLoginManager.elements.DNI_PASSWORD_ERROR).style.display = 'none';
			document.getElementById(ModalLoginManager.elements.DNI_LOGIN_ERROR_MESSAGE).style.display = 'none';
		}
	},
	
	dataFunctions : {
		getModalAJAX : function(callback) {
			if (window.$TORA) {
				var sitePath = $TORA("PAGE").getSitePath();
				$TORA("common").xhr.httpGet(sitePath+ModalLoginManager.UA_FE_APP,function(xhr){
					document.getElementById(ModalLoginManager.elements.MODAL_INNER_CONTAINER).innerHTML=xhr.responseText;
					ModalLoginManager.controlFunctions.registerFbControl();
					ModalLoginManager.controlFunctions.registerEvents();
					ModalLoginManager.uiFunctions.refreshForm();
					if (callback != null) {
						callback();
					}
				}, false);
			}
		},
		getLoginAJAX : function() {
			if (window.$TORA) {
				document.getElementById(ModalLoginManager.elements.LOGIN_MODAL_OVERLAY).style.display = 'block';
				var em = document.getElementById(ModalLoginManager.elements.DNI_EMAIL_INPUT).value;
				var pw = document.getElementById(ModalLoginManager.elements.DNI_PASSWORD_INPUT).value;
				var rm = document.getElementById(ModalLoginManager.elements.DNI_REMEMBER_INPUT).value;
				var ru = window.location.href.replace(/^(?:http\:\/\/|)(.*?)(?:#.*$|$)/, "$1");
				var dataString = "email="+em+"&password="+pw+"&rememberMe="+rm+"&returnUrl="+ru;
				var sitePath = $TORA("PAGE").getSitePath();
				$TORA("common").xhr.httpPost(sitePath+ModalLoginManager.UA_FE_MODAL_LOGIN+"?"+dataString,function(xhr){
					var responseText = xhr.responseText;
					if (responseText != null && responseText != ""){
						document.getElementById(ModalLoginManager.elements.DNI_LOGIN_ERROR_MESSAGE).innerHTML = responseText;
						ModalLoginManager.uiFunctions.displayLoginError();
						document.getElementById(ModalLoginManager.elements.LOGIN_MODAL_OVERLAY).style.display = 'none';
					}else {
						window.location.reload();
					}
				}, false);
			}
		}
	},
	
	controlFunctions : {
		registerFbControl : function() {
			if ((typeof(discovery.authentication) !== "undefined") 
				&& (discovery.authentication.ui)) {
				//REGISTER CONTROL TYPES
				augment(discovery.authentication.ui, [], function(){
					this.core.registerType("ua-profile-management-connectFacebook", this.lib.FBLoginButton);
				});
				discovery.authentication.ui.core.processElement(window.document);
			}
		},
		registerEvents : function() {
			// EVENT HANDLERS FOR ELEMENTS BROUGHT IN VIA AJAX CALL
			var elements = {
				'login-modal-close' : ['click',ModalLoginManager.uiFunctions.hideLoginModalContainer],
				'login-modal-discovery-account' : ['click',ModalLoginManager.uiFunctions.displayDniLogin],
				'login-modal-facebook-account' : ['click',ModalLoginManager.uiFunctions.displayFbLogin],
				'login-facebook-button' : ['click',ModalLoginManager.uiFunctions.hideLoginModalContainer],
				'dnilogin-form' : ['submit',ModalLoginManager.uiFunctions.checkform]
			};
			ModalLoginManager.util.assignControllers(elements);
		}
	},
	
	init : function(){
		var elements = {
				'profile-widget-login-dni' : ['click',ModalLoginManager.uiFunctions.getLoginModalDni],
				'profile-widget-login-fb' : ['click',ModalLoginManager.uiFunctions.getLoginModalFb]                                  
			};
		ModalLoginManager.util.assignControllers(elements);
	}
	
};
// EVENT HANDLERS FOR ELEMENTS THAT ARE ALWAYS PRESENT ON PAGE


$TORA.whenReady(function() {
	if(!$TORA("USER").isLoggedIn())
		ModalLoginManager.init();
});

