
// Used for separate testing of this page
$(document).ready( function(){

// Create namespace if not existing
if(typeof mt === 'undefined') {
	mt = {	'controllers' : {}, 
			'models' : {}, 
			'extra' : {}
		}
	}
	
	
	// Create controller
	mt.controllers.mainMenu = new mainMenu();
	
	// Set constants
	//mt.controllers.mainMenu.x = "x";
	
	// Set the paths
	mt.controllers.mainMenu.paths = {};		
	mt.controllers.mainMenu.paths.loginUrl = mt.constants.SECURE_URL+'mongo.php?action=update&page=login';	
	
	mt.controllers.mainMenu.paths.successLoginRedirectUrl = 'myOverview.php';
	mt.controllers.mainMenu.paths.failureLoginRedirectUrl = 'recover.php?login=failed';
	
	// init
	mt.controllers.mainMenu.init();


});

/***
 * Main controller class  
 */
function mainMenu(){
	
	// variables
	// successRedirectUrl can be overridden outside this object and if it has an empty value
	// it means that no redirect will take place.
	var successRedirectUrl; 
	
	// INIT the page		
	this.init = function(){
		
		// Create the model
		mt.models.mainMenu = new modelBase(); 		
		// Set save URL so that the model knows where to save its data, in this case the login credentials
		mt.models.mainMenu.setSaveUrl(mt.controllers.mainMenu.paths.loginUrl)
		
		// Set default redirect url. 
		successRedirectUrl = mt.controllers.mainMenu.paths.successLoginRedirectUrl; 

		// Create the controls
				
		// Create simple GUI controls		
		var emailAddress = new GUIControle(mt.models.mainMenu,"loginEmailAddress", "#loginEmailAddress");
		$("#loginEmailAddress").change(function(evtObj){
			$(evtObj.target).next().show();
		})
		var rememberMe = new GUIControle(mt.models.mainMenu,"rememberLogin", "#rememberLogin", "checkbox");

				
		var password = new GUIControle(mt.models.mainMenu,"loginPassword", "#loginPassword");
		
		// Enter key to log in
		$("#loginPassword").keyup(function(e){
			if ((e.keyCode || e.which) == 13){
				$('#loginButton').click();
			}
		})
		
		// Create a login button
		var loginButton = $('#loginButton');
		loginButton.click(function(){
			// Send user to recover psw/failed login page if any of the credentials are missing  
			var loginCreds = mt.models.mainMenu.getData();
			console.log(loginCreds);			
			if(!loginCreds.loginPassword || !loginCreds.loginEmailAddress){
				window.location.href=mt.controllers.mainMenu.paths.failureLoginRedirectUrl+"&msg=emptyPswOrEmail";			
			}
			
			else{
				mt.models.mainMenu.saveSecure( function(responseData){
					// Show logoutbox if ligin was successful
					console.info(responseData);					
					if(responseData.status && responseData.status === "ok"){
						
						
						// USER LOGGED IN
						setLoggedIn(responseData.data.displayName);
					}
					// If credentials weren't accepted on the server we redirect
					else{
												
						window.location.href=mt.controllers.mainMenu.paths.failureLoginRedirectUrl;
					}
					
				}, this);
			} 
		})

	}// End init
	
	
	
	var setLoggedIn = function (displayName){
																		
						// Redirect
						if(successRedirectUrl){							
							window.location.href=successRedirectUrl;
						}
						// Stay on the page where the user logged in
						else{
							mt.userAuthenticated = true;
							$('.loginbox').hide();
							$('#myOverviewSubMenu').show();
							
						
							// there might be other controllers interested in listening. 
							// E.g. the newAssignmentController and viewAssignmentController
							mt.extra.globalListener.triggerEvent("userAutenticated");
	
							var logoutbox = $('#login-logout-container');
							logoutbox.html(
								"<div id=logout-container>"+						    
						            "Signed in as <span id='loggedInUser'>"+displayName+"</span>" +
						            "<a href='logout.php' id='logout'> Sign out </a>" +
						            "<div class='clearfix'></div>" +
						        "</div>"
							);						
							logoutbox.show();
						}			
	}
	
	
	// Public function that can be used by other controller to let the 
	// main menu know that the user had been logged in by some other means.
	this.setLoggedIn = function(displayName){
		setLoggedIn(displayName);
	}
	
	// Public function that can be used by other controller to change the successRedirectUrl 
	this.setsuccessRedirectUrl = function(url){		
		successRedirectUrl = url;
	}
	
	

}



