


$(document).ready( function(){

// Create namespace if not existing
if(typeof mt === 'undefined') {
	mt = {	'controllers' : {}, 
			'models' : {}, 
			'extra' : {}
		}
	}

	// Create controller
	mt.controllers.index = new index();
	
	// Set the paths
	mt.controllers.index.paths = {};	
	mt.controllers.index.paths.loadDataUrl = 'mongo.php';

	
	// Init this page
	mt.controllers.index.init();

	
	

});

/***
 * Main controller class for managing the index page 
 */
function index(){
	
	this.init = function(){
		
		// Load prettyPhoto video container	
		var flashObject = '<object style="height: 390px; width: 640px">' +
							'<param name="movie" value="https://www.youtube.com/v/dK9VlF7Dz3o?version=3&feature=player_embedded&autohide=1&autoplay=1&feature=player_embedded">'+
							'<param name="allowFullScreen" value="true">'+
							'<param name="allowScriptAccess" value="always">'+
							'<embed src="https://www.youtube.com/v/dK9VlF7Dz3o?version=3&feature=player_embedded&autohide=1&autoplay=1&feature=player_embedded" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="360">'+
						'</object>';
		
		$("#introVideo").prettyPhoto({
			"default_width": 700,
			"default_height": 425,
			"show_title": true,
			"flash_markup" : flashObject,
			"social_tools" : ""
		});
				
				
		// Create the model
		mt.models.index = new modelBase();	
		
		// Load data				
		var indexRequest = {"page" : "index"}
		mt.models.index.loadData(mt.controllers.index.paths.loadDataUrl, createGUIControls, this, indexRequest); 
	
		// Load redirection information					
		if($.getUrlVar("loginRequiredFor")){
			
			var parameters = $.getUrlVars();
			console.info($.getUrlVar("loginRequiredFor") + ".php?" + parameters[1] + "=" + $.getUrlVar(parameters[1]));
			
			var redirectMsg = $("<div class='panel attentionPanel'></div>");
			redirectMsg.prepend("<h1> You need to sign in </h1>");

			if($.getUrlVar("reason")){
				redirectMsg.append("<p>"+ decodeURI($.getUrlVar("reason"))+"</p>");
			}

			
			if($.getUrlVar("msg")){
				redirectMsg.append("<p>"+ decodeURI($.getUrlVar("msg"))+"</p>");
			}
			
			redirectMsg.append(" Use the sign in panel in the top right corner.");
			$("#main").prepend(redirectMsg);
			
			
			// Set redirect
			mt.controllers.mainMenu.setsuccessRedirectUrl($.getUrlVar("loginRequiredFor") + ".php?" + parameters[1] + "=" + $.getUrlVar(parameters[1]));
		} 
		
	}
	
	/***
	 * createGUIControls is called via the model callback the data needed to create the GUI controls. 
	 */
	var createGUIControls = function(){
		var data = mt.models.index.getData();
		$("#nrUsers").html(data.users);
		$("#nrAssignments").html(data.assignments);
				

		
	}	
	

 
}



