//////////////////////////////////////////////
// Login Control
//////////////////////////////////////////////
//
// The mSpace Login control handles some 
// basic user functionality.  Session handling 
// is done via the mspace.php Front End 
// Server script, but login.js provides a 
// couple of methods used for directing the 
// user to their account page, and also 
// initiating an external login (i.e. via 
// Athens/Shiboleth).
//
//////////////////////////////////////////////

var Login = Class.create();
Login.prototype = {
//
// Member Variables

//
// Constructor
initialize: function(oo)
	{
		
		//Override the defatul object parameters, with those from the objectOptions.
		if (oo)
			Object.extend(oo, this);		
		
		//Event subscription
		document.observe('LoginControl:MyAccount',			this.ShowMyAccount.bindAsEventListener(this));
		document.observe('LoginControl:ExternalLogin',	this.ExternalLogin.bindAsEventListener(this));
		document.observe('LoginControl:Register',				this.Register.bindAsEventListener(this));
		document.observe('LoginControl:Logout',					this.Logout.bindAsEventListener(this));
		document.observe('LoginControl:Refresh',				this.Refresh.bindAsEventListener(this));
		
		//create a new tag controller
		new TagControl();
		
	},
	
//Show the user acount page
ShowMyAccount: function(e)
	{
			var params = e.memo;
			var loginUrl = window.mSpaceApplication.GetServerUrl("userinfo");
			loginUrl += "&page=login";
			document.fire('InformationControl:SetUrl', loginUrl);		
	},
	
//Show the user acount page
Register: function()
	{
			var registerUrl = window.mSpaceApplication.GetServerUrl("userinfo");
			registerUrl += "&page=register";
			document.fire('InformationControl:SetUrl', registerUrl);	
	},

//external login
ExternalLogin: function(e)
	{	
		//get a js history string
		var histStr = window.mSpaceHistory.GetHistoryHash();
		//forward to external login page
		document.location.href = "/?externallogin=true&jslogin=true&history=" + URLEncode(histStr);
	},
	
//Show the user acount page
Logout: function(e)
	{
			var url = window.mSpaceApplication.GetServerUrl("logout");
			document.fire('InformationControl:SetUrl', url);
	},

//Checks if a change in login state has occured and updates the elements on the page that need to be
Refresh: function(e)
	{
		var params = e.memo;
		//Fire off a data request
		var tagUrl = window.mSpaceApplication.GetServerUrl("username");
		document.fire('DataController:Request', { 'sender' : this, 'id' : "refresh", 'type' : "GET", 'url' : tagUrl, 'returnParameter' : params});
	},

// Handles the response from the REFRESH request
// For a username element to be changed it must have the classname 'mspaceuser'
// For a login/logout element to be changed it must have the classname 'mspaceloginout'
HandleResponse: function(id, data, returnParameter)
	{
		if(data == "")
			return;
			
		// Get all elements with class name 'mspaceuser'
		var username_els = $$(".mspaceuser");
		for(var i=0; i < username_els.length; i++ )
			username_els[i].update(data);	

		// Get all elements with class name 'mspaceloginout'
		var loginout_els = $$(".mspaceloginout");
		
		if(data == "Guest")
			var newA = "<a onclick=\"document.fire('LoginControl:MyAccount',null); return false;\" href=\"/myaccount/?&firstload=true\"><span class=\"mspaceloginout\">Login</span></a>";
		else
			var newA = "<a href=\"/Logout/people/?&jsmode=true&\" onclick=\"document.fire('InformationControl:SetUrl', './inc/mspace.php?action=logout');return false;\"><span class=\"mspaceloginout\">Logout</span></a>";
			
		for(var i=0; i < loginout_els.length; i++ )
		{
			// Get the enclosing <a> tag for each item
			var alink = loginout_els[i].up("a");
			
			// Replace the old link with the new one
			alink.replace(newA);
		}
	}
	
};

