function lib_bwcheck(){ //Browsercheck (needed)
  this.ver=navigator.appVersion;
  this.agent=navigator.userAgent;
  this.dom=document.getElementById?1:0;
  this.opera5=this.agent.indexOf("Opera 5")>-1;
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6;
  this.mac=this.agent.indexOf("Mac")>-1
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
  return this;
}

var bw=new lib_bwcheck();

function setPopup() {
  if(bw.ns4)
    document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = popmousemove;
  tipObj = new makeObj('idTips','idTipsText');
}

//Makes crossbrowser object.
function makeObj(obj,textObj){								
  this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
  if(!this.evnt) 
    return false
  
  this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;	
  this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;
  this.innerEvnt=bw.dom? document.getElementById(textObj):bw.ie4?document.all[textObj]:bw.ns4?document.layers[textObj]:0;	   		
  return this;
}

function showTip(msg) {
  if (bw.ie5||bw.ie6) 
    descy = descy+document.body.scrollTop;

  tipObj.css.left = (descx+fromX)+px;
  tipObj.css.top = (descy+fromY)+px;
  tipObj.css.visibility = "visible";
  tipObj.innerEvnt.innerHTML = msg;
}
function hideTips() {
  tipObj.css.visibility = "hidden";
}

var tipObj;

fromX = -20; //How much from the actual mouse X should the description box appear?
fromY = -25; //How much from the actual mouse Y should the description box appear?

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

//Capturing mousemove
var descx = 0;
var descy = 0;
function popmousemove(e){
  descx=bw.ns4||bw.ns6?e.pageX:event.x;
  descy=bw.ns4||bw.ns6?e.pageY:event.y;
}


var balloon = null;
var balloonText = null;
var balloonWidth = 250;

function getBody() {
  return document.getElementsByTagName('body')[0]
}

function addClickEvent(obj, lambda) {
  if (obj.addEventListener)
    obj.addEventListener('click', lambda, true);
  else
    obj.onclick = lambda;
}

function newCloseButton() {
  closeButton = document.createElement('div');
  closeButton.appendChild(document.createTextNode("Close"));
  closeButton.style.fontSize = '8pt';
  closeButton.style.fontStyle = 'italic';
  closeButton.style.paddingTop = '5px';
  closeButton.style.color = '#00a';
  closeButton.style.cursor = 'pointer';
  closeButton.style.textAlign = 'right';
  addClickEvent(closeButton, function (ev) {hideBalloon(ev);});
  return closeButton;
}

function newBalloon() {
  balloon = document.createElement('div');
  balloon.id = 'balloon_tip';
  balloon.className = 'balloon balloon_ptr';
  balloon.style.position = 'absolute';
  balloon.style.width = balloonWidth + 'px';
  balloon.style.display = 'none';
  balloonText = document.createElement('div');
  balloonText.className = 'balloon_ptr'
  balloonText.appendChild(document.createTextNode("I'm a balloon"))
  balloon.appendChild(balloonText);
  balloon.appendChild(newCloseButton());
  body = getBody();
  body.insertBefore(balloon, body.firstChild);
  addClickEvent(window.document, function (ev) {toggleBalloon(ev);});
}

function toggleBalloon(event) {
  ev = (event) ? event : window.event;
  t = (ev.srcElement) ? ev.srcElement : ev.target;
  if (t.getAttribute('balloon') != null) {
    maxX = (event) ? document.width : window.screen.width;
    curX = ((event) ? ev.pageX : ev.x);
    diffX = (maxX - curX);
    if (diffX < balloonWidth) {
      curX = curX - (balloonWidth - diffX);
    }
    balloon.style.display = '';
    tp = (event) ? ev.pageY : ev.y;
    balloon.style.top = (tp - 5)  + 'px';
    newX = ((curX - 5) < 0) ? curX : curX - 5;
    balloon.style.left = newX + 'px';
    balloon.style.zIndex = 100000;
    message = t.getAttribute('balloon');
    if (message.indexOf(' ') == -1)
      message = document.getElementById(message).innerHTML;
    balloonText.innerHTML = message;
  } else {
    hideBalloon(ev);
  }
}

function hideBalloon(ev) {
  balloon.style.display = 'none';
}
