/*
* Doms' cool sticky!!
* ==================
* Compatible with IE 5, NN6 and Mozzila 1.6 (unlike microsofts version!)
* You should only call this function directly -> showTooltip(sTitle, sMessage)
* All others should be internal to this code.
*
* Variable Description
* ====================
* sTitle   = The string you want to appear as the title.
* sMessage = The string that will make up the message body.
*
* Example
* =======
* -----------
* +Title    +
* +Message  +
* +goes here+
* -----------
*
* Note the size will increase automaticly as required (unlike microsofts version!) and will scroll
* so that all the sticky is on the page.
*/

//Global variables

var iX = 0;
var iY = 0;
var sID = 'toolTip';
var aNodes = null;
var oNode = null;

/*
* This function allows a event to be added to both microsoft and netscape style browsers
* 
* Variable Description
* ====================
* sEvent    = The string describing the event such as "onclick";
* oFunction = The function obect such as helloworld()
* oAttachTo = The object you want to attach the event to such as body
* bAttach   = This allows you to add or remove the function from this object true -> add
*
* This function will be very useful in converting the rest of the javascript to be NN compatible and
* should be used elsewhere.
*/ 

function handleEvents(sEvent, oFunction, oAttachTo, bAttach)
{
  if (bAttach)
  {
    if (oAttachTo.addEventListener)
    {
      oAttachTo.addEventListener(sEvent, oFunction, true);
    }
    else if (oAttachTo.attachEvent)
    {
      oAttachTo.attachEvent(sEvent, oFunction)
    }
  }
  else
  {
    if (oAttachTo.removeEventListener)
    {
      oAttachTo.removeEventListener(sEvent, oFunction, true);
    }
    else if (oAttachTo.detachEvent)
    {
      oAttachTo.detachEvent(sEvent, oFunction)
    }
  }
}

// scrolls the page if need be

function checkScrolling(oDiv)
{
  var iBottom = (parseInt(oDiv.style.top) + parseInt(oDiv.style.height));
  var iTop = parseInt(oDiv.style.top);
  var iYpos = 0;
  if (document.body.clientHeight)
  {
    iYpos = document.body.clientHeight;
  }
  else
  {
    iYpos = window.innerHeight;
  }
  if (iTop < document.body.scrollTop) 
  {
    window.scrollBy(0,(iTop - document.body.scrollTop));
  }
  if (iBottom > iYpos)
  {
    window.scrollBy(0,(iBottom - iYpos));
  }
}

function reposition(oDiv)
{
  var iWidth;
  var iHeight;
  if (document.body.clientHeight)
  {
    iHeight = document.body.clientHeight;
  }
  else
  {
    iHeight = window.innerHeight;
  }
  
  if (document.body.clientWidth)
  {
    iWidth = document.body.clientWidth;
  }
  else
  {
    iWidth = window.innerWidth;
  }
  
  if (parseInt(oDiv.style.top) < 0)
  {
    oDiv.style.top = 0;
  }
   
  if (parseInt(oDiv.style.left) < 0)
  {
    oDiv.style.left = 0;
  }
  
  if ((parseInt(oDiv.style.left) + parseInt(oDiv.style.width)) > (iWidth + document.body.scrollLeft))
  {
	oDiv.style.left = parseInt(oDiv.style.left) + (iWidth + document.body.scrollLeft) - (parseInt(oDiv.style.left) + parseInt(oDiv.style.width)) + 8;
  }
  if ((parseInt(oDiv.style.top) + parseInt(oDiv.style.height)) > (iHeight + document.body.scrollTop))
  { 
    oDiv.style.top = parseInt(oDiv.style.top) + (iHeight + document.body.scrollTop) - (parseInt(oDiv.style.top) + parseInt(oDiv.style.height)) + 8;
  }
}

//sets the x and y position of the mouse (the sticky will appear here)

function getXY(e) 
{
  if (window.Event)
  {
    iX = e.pageX;
  }
  else
  {
    iX = event.clientX;
  }
  if (window.Event)
  {
    iY = e.pageY;
  }
  else
  {
    iY = event.clientY + document.body.scrollTop;
  }
}


//only needs to return one size as will be making a square and
//both lengths will be the same
  
function calculateSize(sTitle, sMessage)
{
  var iIntialHeight = 0;
  var iIntialWidth = 0;
  var oTestSpan = document.createElement('span');
  var iWidthPerLetter = 0;
  var iNoLetters = 0;

  oNode.appendChild(oTestSpan);
  oTestSpan.innerHTML = sTitle;
  iIntialWidth = oTestSpan.offsetWidth;
  iIntialHeight = oTestSpan.offsetHeight;
  oNode.removeChild(oTestSpan);
  
  iWidthPerLetter = Math.floor(iIntialWidth / sTitle.length);
  iNoLetters = (sTitle.length + sMessage.length);
  return Math.floor(Math.sqrt(((iWidthPerLetter * iNoLetters) * iIntialHeight))+iIntialHeight);
}

//removes the sticky from the node structure

function hideTooltip()
{
  var oOldToolTip = document.getElementById(sID);
  if (oOldToolTip)
  {
    oNode.removeChild(oOldToolTip);
  }
}

// This is the only function you should call directly
function showTooltip(sTitle, sMessage, iXAxis, iYAxis, bCancelBubble)
{
  if (typeof(iXAxis) != "number")
  {
    iXAxis = 0;
  }
  if (typeof(iYAxis) != "number")
  {
    iYAxis = 0;
  }
  if (typeof(bCancelBubble) != "boolean")
  {
    bCancelBubble = true;
  }
  
  handleEvents("click", hideTooltip, document.body, false);
  handleEvents("click", hideTooltip, document.body, true);
  
  if (document.body)
  {
    handleEvents("onclick", hideTooltip, document.body, false);
    handleEvents("onclick", hideTooltip, document.body, true);
    if (typeof(event) != "undefined")
    {
      event.cancelBubble = bCancelBubble;
    }
  }
  handleEvents("onclick", hideTooltip, document, false);
  handleEvents("onclick", hideTooltip, document, true);
  
  aNodes = document.getElementsByTagName("body");
  oNode = aNodes[0];
  
  var sInfo = "<span class=\"title\">" + sTitle + "</span></br><span =\"message\">" + sMessage + "</span>";
  var oDiv = document.createElement("div");
  var iSize = calculateSize(sTitle, sMessage);
  hideTooltip();
  oDiv.id = sID;
  oDiv.innerHTML = sInfo;
  oNode.appendChild(oDiv);
  
  oDiv.style.border = "solid black 1px";
  oDiv.style.bgcolor = "#ffffff";
  oDiv.style.position = "absolute";
  oDiv.style.left = iX + iXAxis;
  oDiv.style.top = iY + iYAxis;
  oDiv.style.padding = 3;
  oDiv.style.width = iSize;
  oDiv.style.height = iSize;
  oDiv.style.zIndex = 101;
  reposition(oDiv);
 // checkScrolling(oDiv);
}

handleEvents("mousemove", getXY, document, false);
handleEvents("mousemove", getXY, document, true);
handleEvents("onmousemove", getXY, document, false);
handleEvents("onmousemove", getXY, document, true);

