// JavaScript Document
$(document).bind('click', function(e) {

	var $clicked=$(e.target); // get the element clicked
	
	// Close the user status menu
	// upon extraneous click
	if( !$clicked.hasClass('change-user-status') )
	{
		$('#user_status_menu').hide();
	}
	
	// Deactive User status input
	// upon extraneous click
	if( $('#user_status_input').hasClass('active_status_text') && ! ( $clicked.attr('id')=='user_status_post_button' || $clicked.attr('id')=='user_status_input' || $clicked.hasClass('status-saving') ) )
	{
		userStatusTextToggle(0);
	}
});

// Toggle the user status custom
// dropdown menu when the user status
// change arrow is clicked
function toggleUserStatusMenu()
{
	var menu = $('#user_status_menu');
	var $status = $('#bottombar_status');
	
	if (menu.is(':visible'))
		menu.hide();
	else
	{	
		var pos = $status.position();
        var top = pos.top - menu.outerHeight();
		var left = pos.left + $status.outerWidth() - menu.outerWidth();
		
		menu.css( 'top', top+'px' ).css( 'left', left+'px' ).show();
	}
}

// toggle active/inactive appearance of
// user status text input
function userStatusTextToggle(active)
{	
	var $statusContainer = $('#user_status_block');
	var status_input = $('#user_status_input');
	var $btnContainer = $('#user_status_button_container');
	
	if (active==1)
	{
		// Make sure the 'post' button is enabled
		$('#user_status_post_button').removeAttr('disabled');
		
		if (status_input.val()=='What are you up to now?')
		{	
			status_input.val('');
			status_input[0].focus();
		}
		status_input.addClass('active_status_text');
		$btnContainer.show();//post_btn.css('visibility', 'visible');
	}
	else
	{
		if (status_input.val()=='')
			status_input.val('What are you up to now?');
		//status_input.css('height', '13px');
		status_input.removeClass('active_status_text');
		$btnContainer.hide();//post_btn.css('visibility', 'hidden');
	}
	
	// *If the User is viewing the homepage (i.e. the Community module exists)*
	if ($('#community_floating_wrapper').length > 0)
	{
		// If the Community was originally adjusted to account for the Status container,
		// and the Status is now closed, readjust it
		if (active!=1 && communityAdjustedForStatus==true)
		{
			adjustFloatingCommunityModule({ noAnimation: true });
		}
		else
		{
			// Get Community's top position
			var commOffset = $('#community_floating_wrapper').offset();
			var commTop = commOffset.top;
			
			// Get Status container's bottom position
			var statusOffset = $statusContainer.offset();
			var statusBottom = statusOffset.top + $statusContainer.outerHeight();
			
			// If the expanded Status container would overlap the Community
			// immediately (noAnimation) adjust the appearance of the floating Community module
			if (commTop < statusBottom)
			{
				adjustFloatingCommunityModule({ noAnimation: true });
				communityAdjustedForStatus = true;
			}
		}
	}
}

// AJAX : update user status
function updateUserStatus(opts)
{
	var status = opts.status;
	var called = opts.called;
      
	$.getJSON('scripts/users/update_user_status.php', { status: status, called:called }, function (json){
		
		// Status is finished updating
		userStatusUpdating = false;
		
		$('#user_curr_status').text(json.status).attr('class', 'change-user-status user-status-text '+json.status.toLowerCase()+'_status');
		if(called == 'manually')
        {
             $('#user_curr_status').addClass('status-changed-manually');
        }
		
	});
}
