/**
 * @namespace
 */
m2.widget.user.Logout = {

	HTML : {
		signOutConfirmDialog : 
			'<div class="confirmDialog">' + 
					'Are you sure you want to sign out from mapquest? </br></br>' +
					'<form name="signoutConfirm" id="signoutConfirm" onsubmit="m2.widget.user.Logout.logoutConfirm(this);return false;">' +
						'<div class="actionItems">' +
	                    '<input id="NoConfirmBox" name="NoConfirmBox" type="checkbox" />' +
	                    '<label for="remember">Don\'t show this again</label>' +
	                    '<a href="javascript:void(0)" onclick="m2.dialog.close();return false;"><span>Cancel</span></a>' +
	                    '<button type="submit" name="yes" ><span>Yes</span></button>' +
	                    '</div>' +
					'</form>' +
			'</div>',		
	    signOutSuccessDialog : 
			'<div class="confirmDialog">' + 
					'You are cuccessfully logged out!' +
			'</div>'		
	},
/* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* LOGOUT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */

	/**
	 * Function will call the logout dwr service
	 * @scope httpParent
	 */
	logout : function() {
        $a('MQ08MySignOut');
		var token = m2.Cookie.getJsonCookie("USER_TOKEN");
		var self = this;
		m2.util.DWR.callService(AuthService.logout,self.handleLogout,token);
	},
	
	logoutConfirm : function(form) {
		var self = this;
		//Check for SingOut_NoConfirmBox Option
		if (form.NoConfirmBox.checked) { // Set the option to Cookie
			var userPreferenceObj = m2.Cookie.getJsonCookie("USER_PREFERENCE");
    		if (!userPreferenceObj || (typeof userPreferenceObj == "string")) { userPreferenceObj = { singIn_RememberMe_AOL : "no", singIn_RememberMe_OID : "no" }; }
			userPreferenceObj.singOut_NoConfirmBox ="on";
    		this.userPreference = userPreferenceObj;

			// Write to cookie
			m2.Cookie.setJsonCookie("USER_PREFERENCE", this.userPreference, 365, "/");
		}
		if(m2.dialog.hasDialog("signOutDialog"))
			m2.dialog.close("signOutDialog");
		m2.dialog.loading("Processing ...");
		this.token = m2.Cookie.getJsonCookie("USER_TOKEN");
		m2.util.DWR.callService(AuthService.logout,self.handleLogout,this.token);
	},

	/**
	 * Function will handle the response from the logout service call
	 * @param {Object} r
	 * @scope httpParent
	 */
	handleLogout : {
		SUCCESS : function(r) {
            // call the sns logout - will redirect to the logout ftl which in turn calls logoutSuccess
            if (m2.isSafari) {
                // Full redirect in Safari since things break when using a hidden frame.
                m2.util.Iframe.sendMessage("dispatchLogout", r);
            } else {
    			m2.util.Iframe.create({
    				id:"snsFrame",
    				src:decodeURIComponent(r.data.clientUrl) + '&succUrl=' + encodeURIComponent(m2.util.getSecureUrl() + "/logout"),
    				hidden: true
    			});
            }
    	},

		AUTH_ERROR : function(r) {
		},

		FAILURE : function(r) {
			//do nothin
		}
	},
    
    /**
     * Dispatch a logout request using a redirect.
     * 
     * @param {Object} r  response data
     */
    dispatchLogout : function(r) {
        window.location = r.data.clientUrl + '&succUrl=' + encodeURIComponent(m2.util.getSecureUrl() + "/logout?redirectUrl=" + encodeURIComponent(location.href));
    },

	/**
	 * Function will handle the processing of cleaning up the data from a successful logout
	 * @scope httpParent
	 */	
	logoutSuccess : function() {
		// clear the user data
		m2.User.token = null;
		m2.User.data = null;
		m2.User.isLoggedIn = false;
		// clean the cookie
		m2.widget.user.deleteUserToken();
        // send to the parent to refresh the welcome message
        m2.util.Iframe.sendMessage("loggedOut");
	}

};
