function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
$(function()
{
  var hideDelay = 500;  
  var currentID;
  var hideTimer = null;
  
  // One instance that's reused to show info for the current course
  var container = $('<div id="coursePopupContainer">'
      + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="coursePopupPopup">'
      + '<tr>'
      + '   <td class="corner topLeft"></td>'
      + '   <td class="top"></td>'
      + '   <td class="corner topRight"></td>'
      + '</tr>'
      + '<tr>'
      + '   <td class="left">&nbsp;</td>'
      + '   <td width="400px"><div id="coursePopupContent"></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);
  
  $('.coursePopupTrigger').live('mouseover', function()
  {
      // format of 'rel' tag: pageid,courseguid
     // var settings = $(this).attr('rel').split(',');
     // var pageID = settings[0];
      currentID = trim($(this).attr('rel'));

      // 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'
      });

      $('#coursePopupContent').html('<img src="/accd/custom_include/images/ajax-loader.gif">');

      $.ajax({
          type: 'GET',
          url: '/accd/custom_include/crsdtl.jsp',
          data: 'guid=' + currentID,
          cache: false,
          success: function(data)
          {
              // Verify that we're pointed to a page that returned the expected results.
              if (data.indexOf('coursePopupResult') < 0)
              {
                  $('#coursePopupContent').html('<span >Course ' + currentID + ' did not return a valid result.</span>');
              }

              // Verify requested course is this course since we could have multiple ajax
              // requests out if the server is taking a while.
              if (data.indexOf(currentID) > 0)
              {                  
                  var text = $(data).find("#coursePopupResult").html();
                  $('#coursePopupContent').empty();
                  $('#coursePopupContent').html(text);
      		  	  container.css('display', 'block');
              }
              else
              {
              	  $('#coursePopupContainer').hide();
              }
          }
      });

  });

  $('.coursePopupTrigger').live('mouseout', function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.css('display', 'none');
      }, hideDelay);
  });

  // Allow mouse over of details without hiding details
  $('#coursePopupContainer').mouseover(function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
  });

  // Hide after mouseout
  $('#coursePopupContainer').mouseout(function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.css('display', 'none');
      }, hideDelay);
  });
});


