/*

$Id: login.js 103 2006-03-29 06:44:36Z lindsay $
$Revision: 103 $
$Date: 2006-03-29 17:44:36 +1100 (Wed, 29 Mar 2006) $
$Author: lindsay $

*/

var loginRequest;

/* IE < 5.5 doesn't support encodeURIComponent */
if(encodeURIComponent == null){
	function encodeURIComponent(s){
		return escape(s); // This won't handle + chars or UTF
	}
}

function login(emailAddress, password){
/*alert(emailAddress+', '+password)
return false;*/
	var t = new Date();
	loginRequest = new AJAXRequest();
	loginRequest.url = siteRootPath + 'api/login?emailAddress=' + encodeURIComponent(emailAddress) + '&password=' + encodeURIComponent(password) + '&t=' + t.toString(); // TODO: should be sending this as an XML packet 
	loginRequest.resultHandler = loginResult;
	loginRequest.errorHandler = loginError;
	loginRequest.send();
//	document.body.style.cursor = 'wait';
}

function logout(){
	logoutRequest = new AJAXRequest();
	logoutRequest.url = siteRootPath + 'api/login?action=logout'; // TODO: probably should be sending this as an XML packet 
	logoutRequest.resultHandler = logoutResult;
	logoutRequest.errorHandler = loginError;
	logoutRequest.send();
//	document.body.style.cursor = 'wait';
	return false;
}

function loginResult(result){
	var loginBar = document.getElementById('login');
	var loginForm = document.getElementById('loginForm');
	var loginError = document.getElementById('loginError');
	if(!isShow && bodyID == 'homepage'){
		var welcomeBack = document.getElementById('welcomeBack');
		var welcomeBackHeader = welcomeBack.getElementsByTagName('H2')[0];
		var loginAndControl = document.getElementById('loginAndControl');
	}
	var notifyIFrame;
	var isShowOwner = false;
	var yourWarblNavItem = document.getElementById('pnYourWarbl');

	if(result.success == 'true'){
		loginForm.style.display = 'none';
//		loginBar.innerHTML = '<span id="welcomeBackBar" style="display:none;">Welcome back '+result.firstName+'! <a href="' + siteRootPath + 'logout" onclick="return logout();">Log out</a> <a href="' + mainSiteURL + 'mywarbl">My Warbl</a></span>';
//		new Effect.Appear('welcomeBackBar');
//		new Effect.Fade('loginForm');

		if(!isShow && bodyID == 'homepage'){
//			new Effect.BlindUp('loginAndControl');
//			new Effect.BlindDown('welcomeBack', {queue: 'end'});
			welcomeBack.style.display = 'block';
			loginAndControl.style.display = 'none';
			welcomeBackHeader.innerHTML = 'Wassup ' + result.firstName + '!';
		}

		for(var i = 0; i < result.notifyURLs.length; i++){
			notifyIFrame = document.createElement('IFRAME');
			if(!isShowOwner){
				isShowOwner = result.notifyURLs[i].toLowerCase().indexOf(showDomain.toLowerCase()) == 7;
			}
			notifyIFrame.src = result.notifyURLs[i] + '?token=' + result.token;
			notifyIFrame.className = 'hiddenFrame';
			document.body.appendChild(notifyIFrame);
		}
		if(!isShow){
			yourWarblNavItem.getElementsByTagName('A')[0].setAttribute('href', mainSiteURL + 'mywarbl');
			loginBar.innerHTML = '<span id="welcomeBackBar">Welcome back '+result.firstName+'! <a href="' + siteRootPath + 'logout" onclick="return logout();">Log out</a>';
		}
		if(isShow && isShowOwner){
//			yourWarblNavItem.style.display = 'block';
			loginBar.innerHTML = '<span id="welcomeBackBar">Welcome back '+result.firstName+'! <a href="' + siteRootPath + 'control" id="pnYourWarbl">Control</a> <a href="' + siteRootPath + 'logout" onclick="return logout();">Log out</a>';
		}
		if(isShow && !isShowOwner){
			loginBar.innerHTML = '<span id="welcomeBackBar">Welcome back '+result.firstName+'! <a href="' + siteRootPath + 'logout" onclick="return logout();">Log out</a>';
		}

	}else{
		loginError.innerHTML = 'Sorry, the email address or password you supplied was incorrect';
	}
//	document.body.style.cursor = 'auto';
}

function logoutResult(result){
	var loginBar = document.getElementById('login');
	var loginForm = document.getElementById('loginForm');
	if(!isShow && bodyID == 'homepage'){
		var welcomeBack = document.getElementById('welcomeBack');
		var loginAndControl = document.getElementById('loginAndControl');
	}
	var notifyIFrame;
	var yourWarblNavItem = document.getElementById('pnYourWarbl');

	if(result.success == 'true'){
		loginBar.innerHTML = '<a href="' + siteRootPath + 'login" onclick="return showLogin();">Log In and Control</a>';
//		loginForm.style.display = 'none';
		if(!isShow && bodyID == 'homepage'){
//			new Effect.BlindUp('welcomeBack');
//			new Effect.BlindDown('loginAndControl', {queue: 'end'});
			welcomeBack.style.display = 'none';
			loginAndControl.style.display = 'block';
		}
		if(!isShow){
			yourWarblNavItem.getElementsByTagName('A')[0].setAttribute('href', mainSiteURL + 'login');
		}
		if(isShow){
			yourWarblNavItem.style.display = 'none';
		}
		for(var i = 0; i < result.notifyURLs.length; i++){
			notifyIFrame = document.createElement('IFRAME');
			notifyIFrame.src = result.notifyURLs[i] + '?action=logout&token=' + result.token;
			notifyIFrame.className = 'hiddenFrame';
			document.body.appendChild(notifyIFrame);
		}
		if(bodyID == 'yourWarbl'){
			location.href = siteRootPath;
		}
	}else{
		alert('Couldn\'t log you out for some reason.');
	}
//	document.body.style.cursor = 'auto';
}

function loginError(message){
	alert('oh noes!! you broek teh intarweb!!1!!12:\n'+message);
}

/* TODO: look into ways to get auto-complete to play nice with AJAX in FF */
function loginSubmit(){
	login(document.getElementById('emailAddress').value, document.getElementById('password').value);
//	login(document.loginForm.emailAddress.value, document.loginForm.password.value); // breaks in application/xhtml+xml mode
	return false;
}

function loginCancel(){
	var loginForm = document.getElementById('loginForm');
	loginForm.style.display = 'none';
	return false;
}

function windowLoad_login(){
//alert('windowLoad_login')
	var loginForm = document.getElementById('loginForm');
	loginForm.onsubmit = loginSubmit;
	document.loginForm.cancelButton.onclick = loginCancel;
//	EventManager.addEventListener(loginForm, 'submit', loginSubmit, true);
}
EventManager.addEventListener(window, 'load', windowLoad_login);

function showLogin(){
	var loginForm = document.getElementById('loginForm');
	loginForm.style.display = 'block';
//	new Effect.Appear('loginForm');
	return false;
}
