// logging
var debugging = true; // or true

if(typeof console == "undefined")
{
  var console = { log: function(){} };
}
else if(!debugging || typeof console.log == "undefined")
{
  console.log = function(){};
}

/**
 * this controls the popup that houses friend adding,
 * confirming etc.
 */
jQuery(document).ready(function(){
  
  // create and add popup div if needed
  checkPopupDiv();
  
});

/**
 * create and append poopup div if not exists
 */
function checkPopupDiv()
{
  if(jQuery('#popupDiv').length == 0)
  {
    jQuery('body').append('<div id="popupDiv"></div>');
    
    jQuery('div#popupDiv').dialog({
      bgiframe: true,
      modal: true,
      autoOpen: false,
      height: 'auto'
    });
  }
}


/**
 * launch the popup
 */
function friendPopup(action, memberid, process)
{
  // if popup open then set loading button
  if(jQuery('#popupDiv').dialog('isOpen'))
  {
    jQuery('#popupDiv').dialog('option', 'buttons', {
      'Loading...': function(){}
    });
  }
  
  // make request
  jQuery.getJSON(
    '/pop_friend.php?do='+action+'&memberid='+memberid+'&process='+process+'&jsoncallback=?',
    friendHandler
  );
}

/**
 * handle the popup response
 */
function friendHandler(json_obj)
{
  console.log('JSON RESPONSE: '+json_obj.status);
  
  // var for formatted message
  var msg = '';
  
  // load correct buttons based on status
  switch(json_obj.status)
  {
    case 0:
      console.log('NO FURTHER ACTION');
      
      // just add close button
      jQuery('#popupDiv').dialog('option', 'buttons', {
        'Close': function(){
          jQuery('#popupDiv').dialog('close');
        }
      });
      
      // format message
      msg = '<div>'+json_obj.msg+'</div>';
      
      break;
    case 1:
      console.log('CONFIRM SEND REQUEST');
      
      // add buttons
      jQuery('#popupDiv').dialog('option', 'buttons', {
        'Add Friend': function(){
          friendPopup('add', json_obj.memberid, 1);
        },
        'Cancel': function(){
          jQuery('#popupDiv').dialog('close');
        }
      });
      
      // format message
      msg = '<h2>Confirm Friend Request</h2><div>'+json_obj.msg+'</div>';
      
      break;
    case 2:
      console.log('CONFIRM FRIENDS');
      
      // add buttons
      jQuery('#popupDiv').dialog('option', 'buttons', {
        'Add Friend': function(){
          friendPopup('confirm', json_obj.memberid, 1);
        },
        'Deny Request': function(){
          friendPopup('deny', json_obj.memberid, 1);
        }
      });
      
      // format message
      msg = '<h2>Confirm Friend Request</h2><div>'+json_obj.msg+'</div>';
      break;
    case 3:
      console.log('DENIED FRIEND');
      
      // reload page on close of popup
      jQuery('#popupDiv').dialog('option', 'buttons', {
        'Close': function(){
          jQuery('#popupDiv').dialog('close');
          location.reload();
        }
      });
      
      // format message
      msg = '<h2>Friend Request Denied</h2><div>'+json_obj.msg+'</div>';
      
      break;
    case 4:
      console.log('APPROVED FRIEND');
      
      // reload page on close of popup
      jQuery('#popupDiv').dialog('option', 'buttons', {
        'Close': function(){
          jQuery('#popupDiv').dialog('close');
          location.reload();
        }
      });
      
      // format message
      msg = '<h2>Friend Request Approved</h2><div>'+json_obj.msg+'</div>';
      
      break;
    case 5:
      console.log('CONFIRM REMOVE FRIEND');

      // add buttons
      jQuery('#popupDiv').dialog('option', 'buttons', {
        'Remove Friend': function(){
          friendPopup('remove', json_obj.memberid, 1);
        },
        'Cancel': function(){
          jQuery('#popupDiv').dialog('close');
        }
      });

      // format message
      msg = '<h2>Confirm Friend Removal</h2><div>'+json_obj.msg+'</div>';

      break;
    case 6:
      console.log('FRIEND REMOVED');

      // reload page on close of popup
      jQuery('#popupDiv').dialog('option', 'buttons', {
        'Close': function(){
          jQuery('#popupDiv').dialog('close');
          location.reload();
        }
      });

      // format message
      msg = '<h2>Friend Removed</h2><div>'+json_obj.msg+'</div>';

      break;
  }
  
  // insert msg
  jQuery('#popupDiv').html(msg);
  
  // open dialog if closed
  if(!jQuery('#popupDiv').dialog('isOpen'))
  {
    jQuery('#popupDiv').dialog('open');
  }
}

// /**
//  * load content into popup window
//  */
// function addFriendPopup(action, memberid)
// {
//   // set dialog buttons
//   jQuery('#popupDiv').dialog('option', 'buttons', {
//     'Add Friend': function(){
//       // clear buttons
//       jQuery('#popupDiv').dialog('option', 'buttons', {
//         'Loading...': function(){}
//       });
//       
//       jQuery.post(
//         'pop_friend.php',
//         {
//           'do': 'add',
//           'process': 1,
//           'memberid': memberid
//         },
//         function(data){
//           jQuery('#popupDiv').html(data);
//           
//           jQuery('#popupDiv').dialog('option', 'buttons', {
//             'Close': function(){
//               jQuery('#popupDiv').dialog('close');
//             }
//           });
//         },
//         'html'
//       );
//     },
//     'Cancel': function(){
//       jQuery('#popupDiv').dialog('close');
//     }
//   });
//   
//   // make request
//   jQuery.get(
//     'pop_friend.php',
//     {
//       'do': action,
//       'memberid': memberid
//     },
//     function(data){
//       // insert response into popup
//       jQuery('div#popupDiv').html(data);
//       
//       // handle already added
//       if(data.indexOf('confirm') == -1)
//       {
//         jQuery('#popupDiv').dialog('option', 'buttons', {
//           'Close': function(){
//             jQuery('#popupDiv').dialog('close');
//           }
//         });
//       }
//       
//       // launch popup
//       jQuery('div#popupDiv').dialog('open');
//     },
//     'html'
//   );
// }
// 
// /**
//  * confirm or deny friend request
//  */
// function confirmDenyFriendPopup(action, memberid)
// {
//   // add close button
//   jQuery('#popupDiv').dialog('option', 'buttons', {
//     'Close': function(){
//       jQuery('#popupDiv').dialog('close');
//     }
//   });
//   
//   // make request
//   jQuery.get(
//     'pop_friend.php',
//     {
//       'do': action,
//       'memberid': memberid
//     },
//     function(data){
//       // insert response into popup
//       jQuery('div#popupDiv').html(data);
//       
//       // launch popup
//       jQuery('div#popupDiv').dialog('open');
//     },
//     'html'
//   );
// }
