// Peter Bittner <peter.bittner@gmx.net>
// 01-Sept-2010

var closeButtonHtml='<button id="closeButton" onclick="hideSocialBox()">X</button>';
var socialbox = null;
var ajax = null;

function toggleSocialBox(url)
{
  if (socialbox)
    hideSocialBox();
  else
    showSocialBox(url);
}

function showSocialBox(url)
{
  socialbox = document.getElementById('socialbox');
  socialbox.style.display = 'inline';

  ajax = initAjax(stateChanged);
  if (ajax != null)
  {
    var profile = url.substring(url.lastIndexOf('/') + 1);
    var script = '/cgi-bin/cv-xing-aboutme.py';
    var url = script + '?profile=' + profile
    ajax.open('GET', url, true);
    ajax.send(null);
  }
}

function hideSocialBox()
{
  socialbox.style.display = 'none';
  socialbox = null;
}

// Create a new instance of an xmlHttp object to perform all the
// Ajax magic, attach the event handler, and return the object.
function initAjax(eventHandler)
{
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
    //alert("Your browser does not support XMLHTTP!");
    return null;
  }

  xmlhttp.onreadystatechange = eventHandler;
  return xmlhttp;
}

// Ajax event handler
function stateChanged()
{
  switch (ajax.readyState)
  {
    case 0:	// Uninitialized (request is not initialized)
      socialbox.innerHTML = 'No request active.';
      break;

    case 1:	// Open (request has been set up)
      socialbox.innerHTML = 'Fetching social details...';
      break;

    case 2:	// Sent (request has been sent)
      socialbox.innerHTML = 'Waiting for content...';
      break;

    case 3:	// Receiving (request is in process)
      socialbox.innerHTML = 'Receiving response...';
      break;

    case 4:	// Loaded (request is complete)
      socialbox.innerHTML = closeButtonHtml + ajax.responseText;
      if (initExternalLinks)
        initExternalLinks();
      break;
  }
}


