// JavaScript Document
$(function()  
{  
  var hideDelay = 500;    
  var currentID;  
  var hideTimer = null;  
  
  // One instance that's reused to show info for the current person  
  var container = $('<div id="personPopupContainer">'  
      + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="personPopupPopup">'  
      + '<tr>'  
      + '   <td class="corner topLeft"></td>'  
      + '   <td class="top"></td>'  
      + '   <td class="corner topRight"></td>'  
      + '</tr>'  
      + '<tr>'  
      + '   <td class="left">&nbsp;</td>'  
      + '   <td><div id="personPopupContent"></div></td>'  
      + '   <td class="right">&nbsp;</td>'  
      + '</tr>'  
      + '<tr>'  
      + '   <td class="corner bottomLeft">&nbsp;</td>'  
      + '   <td class="bottom">&nbsp;</td>'  
      + '   <td class="corner bottomRight"></td>'  
      + '</tr>'  
      + '</table>'  
      + '</div>');  
  
  $('body').append(container);  
  
  $('.friend-tt').live('mouseover', function()  
  {  
      // format of 'rel' tag: pageid,personguid  
      var settings = $(this).attr('rel').split(',');  
      var target_login = settings[0];  
      currentID = settings[1];  
  
      // If no guid in url rel tag, don't popup blank  
      if (currentID == '')  
          return;  
  
      if (hideTimer)  
          clearTimeout(hideTimer);  
  
      var pos = $(this).offset();  
      var width = $(this).width();  
      container.css({  
          left: (pos.left + width) + 'px',  
          top: pos.top - 5 + 'px'  
      });  
  
      $('#personPopupContent').html('&nbsp;');  
  
      $.post('/includes/api/friends_server.php',{
			 target_member_login:target_login,
			 request:'get_member_info'
			 },
          function(res) 
          {  
		 // alert(res);
		  var member_info = $.json.deserialize(res);
		   var member_info_result = "";
		  // alert(member_info["email"]);
              // Verify that we're pointed to a page that returned the expected results.  
             if (member_info["response"] == 'no') 
              {  
                  $('#personPopupContent').html('<span >Page did not return a valid result for person ' + target_login + '. Please have your administrator check the error log.</span>');  
              }  
   else {
	   for(key in member_info)
				{
	   
	     member_info_result += '<div style="font-family:Verdana, Geneva, sans-serif; font-size:12px; padding-top: 2px; height:55px; width:250px;"><img src="http://site-cdn.stirthewater.com/images/tooltip_images/pic.jpg" align="left" alt="" hspace="2" border="0" width="50" height="50" ><b>' + member_info[key].login + '</b><br>member since: ' + member_info[key].added + '<br><a href="javascript:add_friend(' + member_info[key].member_id + ')">add as friend</a></div>';
	   
				}
              // Verify requested person is this person since we could have multiple ajax  
              // requests out if the server is taking a while.  
                           
       //   var text = $(data).find('.personPopupResult').html();  
                  $('#personPopupContent').html(member_info_result);  
              }  
           
      });  
  
      container.css('display', 'block');  
  });  
  
  $('.personPopupTrigger').live('mouseout', function()  
  {  
      if (hideTimer)  
          clearTimeout(hideTimer);  
      hideTimer = setTimeout(function()  
      {  
          container.css('display', 'none');  
      }, hideDelay);  
  });  
  
  // Allow mouse over of details without hiding details  
  $('#personPopupContainer').mouseover(function()  
  {  
      if (hideTimer)  
          clearTimeout(hideTimer);  
  });  
  
  // Hide after mouseout  
  $('#personPopupContainer').mouseout(function()  
  {  
      if (hideTimer)  
          clearTimeout(hideTimer);  
      hideTimer = setTimeout(function()  
      {  
          container.css('display', 'none');  
      }, hideDelay);  
  });  
});  



function add_friend(target_member_id){
	
	   $.post('/includes/api/friends_server.php',{
			 target_member_id:target_member_id,
			 request:'add_friend'
			 },
          function(res) 
          {  
		  //alert(res);
		  var response = $.json.deserialize(res);
		      if (response["success"] == 'yes') 
              {  
			  alert("Your friend request was submitted successfully.");
			  }
			  else 
			  {
				  
				alert(response["reason"]);  
			  }
		  
		  }
);
}

function confirm_friend(target_member_id){
	
	   $.post('/includes/api/friends_server.php',{
			 target_member_id:target_member_id,
			 request:'confirm_friend'
			 },
          function(res) 
          {  
		  //alert(res);
		  var response = $.json.deserialize(res);
		      if (response["success"] == 'yes') 
              {  
			  alert("Your friend was confirmed successfully.");
			  window.location.reload();
			  }
			  else 
			  {
				  
				alert(response["reason"]);  
			  }
		  
		  }
);
}

function ignore_friend(target_member_id){
	
	   $.post('/includes/api/friends_server.php',{
			 target_member_id:target_member_id,
			 request:'ignore_friend'
			 },
          function(res) 
          {  
		  //alert(res);
		  var response = $.json.deserialize(res);
		      if (response["success"] == 'yes') 
              {  
			  alert("The friend request was ignored.");
			   window.location.reload();
			  }
			  else 
			  {
				  
				alert(response["reason"]);  
			  }
		  
		  }
);
}

function remove_friend(target_member_id){
	
	   $.post('/includes/api/friends_server.php',{
			 target_member_id:target_member_id,
			 request:'remove_friend'
			 },
          function(res) 
          {  
		  //alert(res);
		  var response = $.json.deserialize(res);
		      if (response["success"] == 'yes') 
              {  
			  alert("Your friend was removed.");
			   window.location.reload();
			  }
			  else 
			  {
				  
				alert(response["reason"]);  
			  }
		  
		  }
);
}




