/**
 * Check if a visitor may visit a user's profile page.  Pop-up modal window if not.
 * @param id int
 * @param lnk - the lnk that called this function
 * @return
 */
function ut( id, lnk ){
	if( dojo.byId( 'clicked_lnk' ) )
		dojo.byId( 'clicked_lnk' ).id = '';
	lnk.id = 'clicked_lnk';
	var user_pane = dojo.byId( 'mini_user_pane' );
	if( !user_pane ){
		user_pane = document.createElement( 'div' );
		user_pane.style.display = 'none';
	// Close link
		var close_lnk = document.createElement( 'a' );
		close_lnk.onclick = function(){ 
			user_pane.style.display = 'none';
			lnk.id = '';
		}
		close_lnk.appendChild( document.createTextNode( 'Close' ) );
		close_lnk.className = 'right';
		user_pane.appendChild( close_lnk );
	
	// Content pane
		var content_pane = document.createElement( 'div' );
		content_pane.id = 'content_pane';
		user_pane.appendChild( content_pane );
		user_pane.id = 'mini_user_pane';
		dojo.byId('pfbh').appendChild( user_pane );
	}
	var content_pane = dojo.byId( 'content_pane' );
	dojo.xhrGet( { 
        url: BASE_PATH + "network/directory/query/u/" + id, 
        timeout: 5000, // Time in milliseconds

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) {
			if( response == 1 )
				window.location = BASE_PATH + "network/profile/index/u/" + id;
			else {
				user_pane.style.display = 'block';
				content_pane.innerHTML = response;
				dojo.fadeIn({node: user_pane}).play();
			}
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) { 
          console.error("HTTP status code: ", ioArgs.xhr.status);
          return response;
        }
    }
    );
}

/**
 * Register friend request.
 * 
 * @param id
 * @return
 */
function fr( id ){
	dojo.xhrPost( { 
        url: BASE_PATH + "network/friends/friendreq/u/" + id, 
        form: 'friend_form',
        timeout: 5000, // Time in milliseconds

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) {
			if( response == 1 ){
				dojo.fadeOut( { 
					node:dojo.byId( 'mini_user_pane' ),
					onEnd: function(){
						dojo.byId('mini_user_pane').style.display = 'none';
					}}).play();
				if( dojo.byId( 'clicked_lnk' ) ){
					var lnk = dojo.byId( 'clicked_lnk' );
					dojo.addClass( lnk, 'confirmation_pending' );
					var af_div = dojo.query('.add_friend', lnk.parentNode );
					af_div[0].innerHTML = 'Confirmation of friend request pending';
				}
			}
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) { 
          console.error("HTTP status code: ", ioArgs.xhr.status);
          return response;
        }
    }
    );
}

/**
 * Send message
 * 
 * @param id
 * @return
 */
function msg( id ){
	dojo.xhrPost( { 
        url: BASE_PATH + "network/mail/ajaxmsg/", 
        form: 'msg_form',
        timeout: 5000, // Time in milliseconds

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) {
			if( response == 1 ){
				dojo.fadeOut( { 
					node:dojo.byId( 'mini_user_pane' ),
					onEnd: function(){
						dojo.byId('mini_user_pane').style.display = 'none';
					}}).play();
			} else {
				alert( response );
			}
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) { 
          console.error("HTTP status code: ", ioArgs.xhr.status);
          return response;
        }
    }
    );
}

/**
 * Show the friend request form.
 * 
 * @return
 */
function show_fr_form(){
	dojo.byId( 'main_resp' ).style.display = 'none';
	dojo.byId( 'friend_form' ).style.display = 'block';
}

/**
 * Show the message form
 * 
 * @return
 */
function show_msg_form(){
	dojo.byId( 'main_resp' ).style.display = 'none';
	dojo.byId( 'msg_form' ).style.display = 'block';
}