// "Logged in user" drop-down list (header menu) functionality
if ( typeof mov  === "undefined" ) 
    var mov = {};

if ( typeof mov.member  === "undefined" ) 
    mov.member = {};

mov.member.initLoggedInUserMenu = function()
{
    $menuWrapper = $('#user-dd-menu');
    $topMenu = $menuWrapper.find('#top-level');
    $subMenu = $menuWrapper.find('#sub-menu');

    $topMenu
    	.hover(
			function() { $(this).addClass   ("hovered"); },
            function() { $(this).removeClass("hovered"); }
        )
        .click( function() {
            $subMenu.show();
            $menuWrapper.addClass("opened");
        });

    $subMenu.hover(
		function() {
            // Do nothing
        },
        function() {
            $(this).hide();
            $menuWrapper.removeClass("opened");
        }
    );
}

mov.member.loggedIn = function()
{
    if( $.cookie( ckName+'_id' ) && $.cookie( ckName ) )
        return true;
    return false;
}

$(document).ready(function(){

    $( "#auth_table-moustache" ).show(); // Make sure moustache is visible...
    // 
    //Setup header...
    if( $.cookie( ckName+'_id' ) && $.cookie( ckName ) )
    {
        $( "#hd_auth" ).hide();
        $( "#hd-auth-user" ).show(); // Move to bottom...
        
        // Display their logged in picture...
        if( $.cookie( ckName + '_pic' ) )
        {
            var picUrl = $.cookie( ckName + '_pic' );
            $( "#hd-auth-user-pp" ).attr( 'src', assetUrl + picUrl );
        }
        
        // Display their logged in name...
        if ( typeof nsBaseUrl != 'undefined' && nsBaseUrl != null)
        {
            var movName = $.cookie( ckName+'_name' );
            if (movName.length > 19)
                movName = movName.slice(0,19)+'...';
            //$( "#auth_logged_in_who_description" ).html( '<a href="'+nsBaseUrl+'mospace/">' + $P.urldecode( movName ) + '</a>' );
            $( "#auth_logged_in_who_description" ).html($P.urldecode( movName ));
        }
        else
        {
            if ($.cookie( ckName+'name' ))
                //$( "#auth_logged_in_who_description" ).html( $P.urldecode( $.cookie( ckName+'name' )));
            	$( "#auth_logged_in_who_description" ).html($P.urldecode( movName ));
        }
        $( "#hd_mospace_nav" ).css( 'visibility', 'inherit' );


        $('#menu-topnav-register').addClass('hidden');
        $('#menu-topnav-mospace').removeClass('hidden');
        $('#sidebar-image-register').hide();
        $('#sidebar-image-donate').show();
    } else {
        $( "#hd-auth-user" ).hide(); // Move to bottom...
        // Shouldn't have to do anything...
        $( "#auth_table-register" ).show();
        $( "#auth_table-register-spacer" ).show();
        $( "#auth_table-login" ).show();

        //$('#menu-topnav-register').show();
        //$('#menu-topnav-mospace').hide();
    }
    
    // Chrome: force width update on this element
	window.setTimeout(function(){
		$( "#auth_logged_in_who_description" ).css("position", "relative");
	},0)
});

/**
 * Log the user out of the site, but first check if they are FB connected and if
 * so, log them out of FB first
 *
 * Note:  JS FB.logout() doesn't seem to be working, so doing a FB logout via a
 * redirect to the graph logout facebook url in /auth/logout/ instead before then
 * doing Movember logout.
 * Also, we seem to still have a valid App::facebook() even after redirecting through
 * the facebook logout URL, so passing through a ?facebook=true param here, that will
 * be removed from the 'next' URL that facebook redirects back to us after logging out
 */
function auth_logout()
{
    location.href = baseUrl + 'auth/logout/?facebook=true';
//    FB.getLoginStatus(function(response) {
//        if (response.session) {
//            FB.logout();
//            location.href = baseUrl + 'auth/logout/';
//        } else {
//            location.href = baseUrl + 'auth/logout/';
//        }
//    });
}



