
var iex = browser.isIE || browser.isOpera;
var nav = (document.layers);
var old = browser.isNS && (!document.layers && !document.getElementById);
var n_6 = browser.isNS6up;
var opera = browser.isOpera;
if (browser.isSafari || browser.isFirefox || browser.isMozilla || browser.isKonqueror || browser.isGecko) n_6 = true; //they work the same for this


document.write('<div id="pup" style="visibility:hidden;display:inline;"></div>');

var skin = null

var minMarginToBorder = 15;	
							
var popwidth = 0;   


if(nav) document.captureEvents(Event.MOUSEMOVE);
if(n_6) document.addEventListener("mousemove", get_mouse, true);
if(nav || iex) document.onmousemove = get_mouse;


function assignSkin() {
	if(nav) skin = document.pup;
	if(iex) skin = document.getElementById('pup').style;
	if(n_6) skin = document.getElementById("pup").style;
}
  

function assignPopWidth(){
  	if (iex && !opera) popwidth = parseInt(document.getElementById("pup").currentStyle.width);
	if (opera)  popwidth = parseInt(document.defaultView.getComputedStyle(document.getElementById('pup'),null).width);
	if (n_6)  popwidth = parseInt(document.defaultView.getComputedStyle(document.getElementById("pup"),null).getPropertyValue('width'));
}

function get_mouse(e)
{
    
  var x,y;
  var scroll_x_y = getScrollXY();
  
  
  if (iex) x = scroll_x_y[0] + event.clientX;
  if (nav || n_6) x = e.pageX;


  if (iex) y = scroll_x_y[1] + event.clientY;
  if (nav || n_6) y = e.pageY;
  
  if (popwidth == 0) assignPopWidth();
  if (null === skin) assignSkin();
  
  
  x += 10; 
  
  
  var x_y = nudge(x,y);
  if (isNaN(x_y[0])) x_y[0] = 0;
  if (isNaN(x_y[1])) x_y[1] = 0;
  

  if(nav || iex) {
	  skin.left = x_y[0];
	  skin.top = x_y[1];
  }else if(n_6){
	  skin.left = x_y[0] + "px";
	  skin.top = x_y[1] + "px";
  }
}

function nudge(x,y)
{
  var dims = getInnerWindowDimensions();
  scroll_x_y = getScrollXY();
  

  var xtreme = dims[0] - popwidth - minMarginToBorder;
  if (n_6 || nav) xtreme -= 25;
  if(x > xtreme) {
	x -= (parseInt(popwidth) + minMarginToBorder + 20 );
  }


  if(x < 1) x -=  x - 1;


  est_lines = parseInt(document.getElementById("pup").innerHTML.length / (parseInt(skin.width)/15) );
  est_lines_to_decide = max(est_lines,2);
  if((y + parseInt(est_lines_to_decide * 20)) > (dims[1] + scroll_x_y[1])) {
    y -= parseInt(est_lines * 20) + 20;
  }
  
  return [ x, y ];
}


function popup(msg)
{
  if (popwidth === 0) assignPopWidth();
  if (null === skin) assignSkin();
  
  if (null !== skin) { 
	  if(old) {	
		alert(msg);
		return;
	  }
	  
	  if (!isNaN(popwidth)) {
		if(iex || nav)  skin.width = popwidth;
		if(n_6)  skin.width = popwidth + "px";
	  } else {
		if(iex || nav)  skin.width = 300;
		if(n_6)  skin.width = 300 + "px";
	  }
					
	  if(nav) { 
		skin.document.open();
		skin.document.write(msg);
		skin.document.close();
	  }
	  document.getElementById("pup").innerHTML = msg;
	  
	  skin.visibility ="visible";
	  skin.display = "inline";
  }
}

function kill()
{
  if(!old) {
    skin.visibility = "hidden";	
	skin.display = "none";
  }
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {

    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {

    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {

    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getInnerWindowDimensions(){
	var A;
	if (window.innerHeight !== undefined) A = [window.innerWidth,window.innerHeight];
	else{
	  var D = (document.documentElement.clientWidth == 0)? document.body: document.documentElement;
	  A = [D.clientWidth,D.clientHeight];
	}
	return A;
}

function max(a,b){
    if (a>b) return a;
    else return b;
}
