/**
 * @namespace
 * Namespace holds functions and methods relating to user related widgets on the mapquest site.
 */
m2.widget.user = {

    token : null,
    
	/**
	 * Set this variable to display the welcome page in place of the welcome message on the current page
	 * @type boolean
	 */	
	showWelcomePage : false,

	/**
	 * Function will set the captcha image in the page for the registration and reset password screens
	 * @scope httpsChild
	 */	
	getCaptchaImage : function() {
		// set the captcha image
		var captchaImage = m2.$('captchaImage');
		captchaImage.src = "";
		var location = window.location;
		var captchaUrl = m2.util.getSecureUrl() + '/captcha?' + (new Date()).getTime();
		captchaImage.src = captchaUrl;
		
		var ap = m2.$('captchaAudio');
		if (ap) {
			m2.util.Event.add(ap,"click",function() {
				m2.util.playAudio(captchaUrl + "&f=a");
			});
		}
	},

	/**
	 * Function will process the getUser service and 
	 * @param {Object} data  the data object response from sns
	 * @scope httpsChild
	 */
	getUser : function(data) {	
		var self = this;
        this.token = data.token;
		m2.util.DWR.callService(AuthService.getUser,self.handleGetUser,data.token);
	},
	
	/**
	 * Function handles the dwr response from getUser call
	 * @param {Object} r the response
	 * @scope httpsChild
	 */
	handleGetUser : {
		SUCCESS: function(r){
			// update Session Cookie with User Data
			alert("In Success of get user "+r);
            m2.widget.user.setUserToken();

            // allow user request without dialog support (e.g., auto-login)
            if (typeof m2.dialog != "undefined") {
    			m2.dialog.hideLoading();
            }

            // send to the parent to refresh the welcome message
            m2.util.Iframe.sendMessage("loggedIn", {showWelcomePage:m2.widget.user.showWelcomePage});
		},
		
		AUTH_ERROR : function(r) {
            // allow user request without dialog support (e.g., auto-login)
            if (typeof m2.dialog != "undefined") {
    			// show error message and close login dialog
                m2.dialog.hideLoading();
                m2.dialog.loading("Status Code: " + r.statusCode + "<br />Detail Code: " + r.detailCode + "<br />There was a problem during login processing.  Please try again later.");
    			setTimeout(function() {m2.util.Iframe.sendMessage("loginFailure");},2000);
            }
		},
			
		PROFILE_STORE_ERROR : function(r) {
            // allow user request without dialog support (e.g., auto-login)
            if (typeof m2.dialog != "undefined") {
    			// show error message and close login dialog
                m2.dialog.hideLoading();
                m2.dialog.loading("Status Code: " + r.statusCode + "<br />Detail Code: " + r.detailCode + "<br />There was a problem during login processing.  Please try again later.");
    			setTimeout(function() {m2.util.Iframe.sendMessage("loginFailure");},2000);
            }
		},
						
		FAILURE : function(r) {
            // allow user request without dialog support (e.g., auto-login)
            if (typeof m2.dialog != "undefined") {
    			// show error message and close login dialog
                m2.dialog.hideLoading();
                m2.dialog.loading("Status Code: " + r.statusCode + "<br />Detail Code: " + r.detailCode + "<br />There was a problem during login processing.  Please try again later.");
    			setTimeout(function() {m2.util.Iframe.sendMessage("loginFailure");},2000);
            }
		}
	},

	/**
	 * Sets the user token in the cookie
	 */
	setUserToken : function() {
        var expiresIn = this.token.expiresIn;
    	m2.Cookie.setJsonCookie("USER_TOKEN", this.token, (expiresIn ? (expiresIn / 86400) : 0), "/" );
	},
    
    /**
     * Deletes the user token from the cookie.
     */
    deleteUserToken : function() {
        m2.Cookie.deleteCookie("USER_TOKEN", "/" );
    },

	/**
	 * Sets the welcome message on the page
	 */
	setWelcomeMessage: function(data){
        var welcomeUrl = (data && data.showWelcomePage) ? m2.util.getWelcomeUrl() : null;
        
        if (welcomeUrl) {
            // Show the welcome page.
            window.location = welcomeUrl;            
        } else {
            // Reload the current page to refresh user-specific items.
            window.location.reload(true);
        }
	},
	
	/**
	 * 
	 */
	showDefaultImage: function(){
		var imgAv = m2.$("userImageEle");
		if(null!=imgAv){
			imgAv.src="/cdn/mqcommon/images/mymq_avatar_yellow.gif";
		}
	}
};
