(function(window, document, undefined){
var XBTooltip = function( element, userConf, tooltip) {
  var config = {
    id: userConf.id || undefined,
    className: userConf.className || undefined,
    x: userConf.x || 20,
    y: userConf.y || 20,
    rightMargin: userConf.rightMargin || 0,
    bottomMargin: userConf.bottomMargin || 0,
    text: userConf.text || undefined
  };
  var over = function(event) {
    tooltip.style.display = "block";
  },
  out = function(event) {
    tooltip.style.display = "none";
  },
  move = function(event) {
    event = event ? event : window.event;
    if ( event.pageX == null && event.clientX != null ) {
      var doc = document.documentElement, body = document.body;
      event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
      event.pageY = event.clientY + (doc && doc.scrollTop  || body && body.scrollTop  || 0) - (doc && doc.clientTop  || body && body.clientTop  || 0);
    }
    
    body = document.getElementsByTagName("body")[0];
	bodyHeight = body.offsetHeight;
	bodyWidth = body.offsetWidth;
	    
    if(config.rightMargin == "width")
    {
		tooltip.style.left = Math.min(bodyWidth - tooltip.offsetWidth + config.x, event.pageX+config.x) + "px";
	}
	else
	{
		tooltip.style.left = Math.min(bodyWidth - config.rightMargin + config.x, event.pageX+config.x) + "px";
	}
        
	if(config.bottomMargin == "height")
	{
		tooltip.style.top = Math.min(bodyHeight - tooltip.offsetHeight + config.y, event.pageY+config.y) + "px";
	}
	else
	{
		tooltip.style.top = Math.min(bodyHeight - config.bottomMargin + config.y, event.pageY+config.y) + "px";
	}
  }
  if (tooltip === undefined && config.id) {
    tooltip = document.getElementById(config.id);
    if (tooltip) tooltip = tooltip.parentNode.removeChild(tooltip)
  }
  if (tooltip === undefined && config.text) {
    tooltip = document.createElement("div");
    if (config.id) tooltip.id= config.id;
    tooltip.innerHTML = config.text;
  }
  if (config.className) tooltip.className = config.className;
  tooltip = document.body.appendChild(tooltip);
  tooltip.style.position = "absolute";
  element.onmouseover = over;
  element.onmouseout = out;
  element.onmousemove = move;
  over();
};
window.XBTooltip = window.XBT = XBTooltip;
})(this, this.document);


function getWindowDimensions() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  return {width: myWidth, height: myHeight};
}
