var fromLeft = 500;
var zIndex = 2000;

$(document).ready
(
	function()
	{
		$(".playerPopup").click
		(
			function(e) 
			{ 
				var tqid = $(this).attr("tqid");
				var newID = "#playerPopup"+tqid;
				var fromTop = e.pageY - 80;

				// ----------------------------------
				// Change what the click button does so the next time
				// the user clicks on this player's profile, it simply
				// unhides the one that they had closed
				// ----------------------------------
				$(this).unbind("click");
				$(this).click
				(
					function()
					{
						$(newID).show();
					}
				);


				// ---------------------------------------------
				// Make an AJAX call to get the popup HTML
				// ---------------------------------------------
				$.ajax({
  					type: "GET",
  					url: "/player_popup.php",
  					data: "tqid="+tqid,
  					success: function(msg)
					{
						// ---------------------------------------------
						// Clone the dummy player popup and update
						// the div w/ the return from teh AJAX call
						// ---------------------------------------------
						var player = $('#playerPopup').clone();
						player.css({
							'display':	'block', 
							'top':		fromTop+'px', 
							'left':		fromLeft+'px', 
							'position':	'absolute', 
							'z-index':	zIndex
						});
						player.attr("id", "playerPopup"+tqid);
						player.find('.playerPopupReturn').html(msg);


						// ---------------------------------------------
						// Activate the dragging functionality
						// ---------------------------------------------
						player.draggable
						({
							handle:'.playerPopupHandle2', 
							zIndex: 10000,
							stop: function()
							{
								$(this).css("z-index", zIndex);
								zIndex++;
							}
						});

						// ---------------------------------------------
						// Add the functionality to the close button
						// ---------------------------------------------
						player.find('.playerPopupClose').click
						(
                        				function()
                        				{
								$(this).parent().parent().hide();
                        				}
                				);			

						// ---------------------------------------------
						// Add functionality that brings the popup to
						// the front when it is clicked
						// ---------------------------------------------
						player.find('*').click
						(
                                                        function()
                                                        {
								$(this).parent().css("z-index", zIndex);
								zIndex++;
                                                        }
                                                );

						// ---------------------------------------------
						// Append the popup to the BODY
						// ---------------------------------------------
						$('body').append(player);

						// ---------------------------------------------
						// Increment values for hte next popup
						// ---------------------------------------------
						fromLeft += 20;
						zIndex++;
					}
				});
			}
		);

	}
);

function getAlert(type, msg)
{
	return '<div class="alert_'+type+'">'+msg+'</div>';
}

