var loginPopupToggle = 0;

$(document).ready(function(){
	$('body').append("<"+"div id='loginPopup'><"+"/div><"+"div id='loginPopupMask'><"+"div><"+"/div><"+"/div>");
	loginInit();
});

function loginInit() {
	$(".menuMain.secure a:contains('Login'), a.launchLogin").click(function(){
		login('login');
		return false;
	});
}

function loginLoad(data) {
	$("#loginPopupMask").show();
	$("#loginPopupMask div").css('background-position','center center');
	$('#loginPopup').html("<div class='loading' style='height:"+$('#loginPopup').height()+"px'><"+"/div>");

	$.ajax({
		url: "/login.cfm",
		data: data,
		dataType: 'text',
		type: 'post',
		success: function(data, textStatus, XMLHttpRequest){
			$("#loginPopupMask div").css('background-position','5000px 0px');
			$("#loginPopup").html(data);
			loginPosition();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('error\n XMLHttpRequest['+XMLHttpRequest+']\n textStatus['+textStatus+']\n errorThrown['+errorThrown+']');
			$("#loginPopup").html(XMLHttpRequest.responseText);
			loginPosition();
		}
	});
}

function loginPosition() {
	if (loginPopupToggle != 2) {
		pwidth = $("#loginPopup").width();  
		pheight = $("#loginPopup").height();  
		swidth = $("#loginPopupMask").width();
		sheight = $("#loginPopupMask").height();
		if (sheight > pheight) {
			$("#loginPopup").css('margin-top','-'+Math.round(pheight/2)+'px');
		} else {
			$("#loginPopup").css('margin-top','0');
		}
		if (swidth > pwidth) {
			$("#loginPopup").css('margin-left','-'+Math.round(pwidth/2)+'px');
		} else {
			$("#loginPopup").css('margin-left','0');
		}
		$("#loginPopup").slideDown('fast', function(){
			loginPopupToggle = 1;
		});
	}
}

function login(act,ref) {

//alert('secureRole['+secureRole+']');

	if (typeof(ref)=="undefined") ref = "";

	if (typeof($('#loginAct').val())!="undefined") {
		$('#loginAct').val(act);
		$('#loginForm').submit();
	} else {
		if (act=='login') {
			if (loginPopupToggle == 1) {
				act = 'close';
			}
		}
		if (act=='success') {
//alert(loginPopupToggle+'\n'+secureRole);
			if (loginPopupToggle==2) {
				return false;
			} else {
				loginPopupToggle = 2;
				act = 'close';

				var loginLaunchUrl = location.pathname;
				if (ref=='') {
					if (loginLaunchUrl.indexOf('/_')==-1) {
						loginLaunchUrl = "/redirect_secure.cfm";
/*						
						if (secureRole.indexOf('Distributor')>-1)	loginLaunchUrl = "/_secure/distributor/logos.fxml";
						if (secureRole.indexOf('Doctor Canada')>-1)	loginLaunchUrl = "/_secure/doctor/global/logos.fxml";
						if (secureRole.indexOf('Doctor USA')>-1)	loginLaunchUrl = "/_secure/doctor/usa/logos.fxml";
						if (secureRole.indexOf('Sales')>-1)			loginLaunchUrl = "/_secure/sales/logos.fxml";
						if (secureRole.indexOf('Administrator')>-1)	loginLaunchUrl = "/_secure/";
*/
					}
//alert('location.hostname['+location.hostname+']\nlocation.href['+location.href+']\nlocation.pathname['+location.pathname+']\nlocation.protocol['+location.protocol+']');
//alert('loginLaunchUrl['+loginLaunchUrl+']\nsecureRole['+secureRole+']');
//alert('['+location.protocol + "//" + location.hostname + loginLaunchUrl+']');
				} else {
					loginLaunchUrl = ref;
				}
				location.replace(location.protocol + "//" + location.hostname + loginLaunchUrl);
			}
		}
		if (act=='close') {
			$("#loginPopup").slideUp('fast', function(){
				$("#loginPopupMask").hide();
				loginPopupToggle = 0;
			});
		} else {
			var data = 'act=' + act;
			if (act.substr(0,2)=='do') data += '&' + $('#loginForm').serialize();
			loginLoad(data);
		}
	}
	return false;
}
