/* NOTE: requires jquery. */

/* Make sure we don't load data we already have... */
var quickprofile_lastuser;
var quickprofile_laststatus;

function prepareQuickProfiles()
{
	$("#whosonlinecell a").each( function() {
		if ( $(this).attr('id').match("^profileLink") == "profileLink" ) {
			data = $(this).attr('id').split(":");
			$(this).bind( "click", {user:data[1],status:data[2]}, function(e) { showQuickProfile(e); } );
		}
	} );
}

function showQuickProfile( e )
{	
	/* Create a new container div if necessary. This is the case if there is no container yet. */
	var quickpro = $("#quickprofile");
	if ( quickpro.length == 0 ) { div = document.createElement("div"); document.body.appendChild(div); quickpro = $(div); }

	/* Fetch function data. */
	user = e.data.user;	status = e.data.status;
	
	/* Only create a loading animation screen if we're looking up a new user. */
	if ( quickprofile_lastuser != user || quickprofile_laststatus != status )
	{
		quickpro.empty();
		id = status == "In-Jail" || status == "In-Max-Jail" ? "user_popup_jail" : ( status == "Dead" ? "user_popup_dead" : "user_popup" );
		html = "<div id='"+id+"'>";
		html += "<div class=\"user_crew\" id=\"crewTagCell\">Loading...</div>";
		html += "<div style='margin: 30px auto; width: 64px; height: 64px;'>";
		html += "<img src='/images/quickprofile_load.gif' />";
		html += "</div>";
		html += "</div>";
		quickpro.append( html );
	}	
	quickprofile_lastuser = user; quickprofile_laststatus = status;
	quickpro.attr('id','quickprofile').width(450).css({
		'position': 'absolute', 'z-index': '2000', 'display': 'block', 'top': (e.pageY - (quickpro.height()+50))+'px', 'left': ($(document).width()/2 - 250)+'px'
	});
	quickpro.bind( "mouseleave", function() { $(this).fadeOut( "fast" ); } );

	function onDataLoaded( userdata )
	{
		if ( userdata == "offline" ) { location.href = "/default.asp"; return; }
		quickpro.empty().append( userdata );
		quickpro.css( {top:(e.pageY - (quickpro.height()+50))+'px'} );
	}
	
	$.ajax( { "url": "/includes/profile.asp", "data": "user="+user, "success": onDataLoaded} );
}
