// requires: popUp.js, jquery.js, jquery.modal.js, jquery.validity.js, SessionService.js
// optional: IDXSService.js

$(document).ready(function () {
	mod_login.init();
});

var mod_login = {

	lead: null,

	init: function() {
		if (!$("#login-area").length) return;
		if (window.location.hostname.toLowerCase().indexOf("www.superlativehost") != -1) {
			popUp.proxy = "/proxy.aspx?url=";
			popUp.rootUrl = "http://" + window.location.hostname.toLowerCase().replace("www","public");
			sessionSvc.rootUrl = "http://" + window.location.hostname.toLowerCase().replace("www","public");
		}
		sessionSvc.getLead(popUp.getUid(), "mod_login.getLeadCB");
	},

	getLeadCB: function(args) {
		this.lead = args.result;
		this.print();
		if ($("#savedItemSearchSideModule").length && $("#loginSideModule").length && mod_side)
			mod_side.print(this.lead);
	},
	
	print: function() {
		if (this.lead.leadID != 0) {
			$("#login-area").html("<a href='" + popUp.rootUrl + "/Summary/Default.aspx?uid=" + popUp.getUid() + "' target='_blank'>Welcome " + this.lead.fName + "</a>&nbsp; | &nbsp;<a href='javascript:mod_login.logout()'>Logout</a>");
		}
		else {
			$("#login-area").html("Welcome!&nbsp; | &nbsp;<a href=\"javascript:popUp.signUp('')\">Register</a>&nbsp; | &nbsp;<a href=\"javascript:popUp.login('')\">Login</a>");
		}
	},
	
	logout: function() {
		sessionSvc.logout(popUp.getUid(), "mod_login.logoutCB");
	},
	
	logoutCB: function(args) {
		popUp.doMsg("You are logged out successfully.", "window.location.reload()");
	}

}
