var MouseX,MouseY;

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  
  MouseX=tempX;
  MouseY=tempY;
  
  //  document.Show.MouseX.value = tempX
  //  document.Show.MouseY.value = tempY
  return true
}



function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


function posaft(t) {
	var q = t.mypop.style;
	var x = 0;
	var tt=t;
	var y = t.offsetHeight;
	var ow2=t.offsetHeight;
	var y2=t.mypop.offsetHeight;

	var w ;
	var ow = t.mypop.offsetWidth;
	w = t.offsetParent.offsetWidth;
	
	while (t) {
		x += t.offsetLeft;
		y += t.offsetTop;
		if (t.offsetParent) {w = w+t.offsetParent.offsetLeft}
		t = t.offsetParent;
	}


  if (x+ow > w)  {
     x=w-ow;
   }


   var pattern = new RegExp("(^|\\s)"+'above'+"(\\s|$)");
       if ( pattern.test(tt.className) ) {
         y=MouseY-20-tt.mypop.offsetHeight; 
         x=MouseX-tt.mypop.offsetWidth/2; //- w%2;
         x=findPosX(document.getElementById('TipAbove'))-tt.mypop.offsetWidth/2;
         y=findPosY(document.getElementById('TipAbove'))-tt.mypop.offsetHeight+20;
////       x=MouseX-document.width%2; ; //- w%2;
         }



//   if (class contains 'above' put popup above 
	q.left = x + 'px';
	q.top  = y + 'px';
}


function x2posaft(t) {
	var q = t.mypop.style;
	var x = 0;
	var tt=t;
	var y = t.offsetHeight;
	var ow2=t.offsetHeight;
	var y2=t.mypop.offsetHeight;

	var w ;
	var ow = t.mypop.offsetWidth;
	w = t.offsetParent.offsetWidth;
	


	while (t) {
		x += t.offsetLeft;
		y += t.offsetTop;
		if (t.offsetParent) {w = w+t.offsetParent.offsetLeft}
		t = t.offsetParent;
	}


  if (x+ow > w)  {
     x=w-ow;
   }


   var pattern = new RegExp("(^|\\s)"+'above'+"(\\s|$)");
       if ( pattern.test(tt.className) ) {
         y=MouseY-20-tt.mypop.offsetHeight; 
         x=MouseX-tt.mypop.offsetWidth/2; //- w%2;
         x=x;
         y=y;
////       x=MouseX-document.width%2; ; //- w%2;
         }



//   if (class contains 'above' put popup above 
	q.left = x + 'px';
	q.top  = y + 'px';
}



function xposaft(t) {
	var q = t.mypop.style;
	var x = 0;
	var y = t.offsetHeight;
	var w ;
	var ow = t.mypop.offsetWidth;
	w = t.offsetParent.offsetWidth;
	while (t) {
		x += t.offsetLeft;
		y += t.offsetTop;
		if (t.offsetParent) {w = w+t.offsetParent.offsetLeft}
		t = t.offsetParent;
	}

  if (x+ow > w)  {
     x=w-ow;
   }
	q.left = x + 'px';
	q.top  = y + 'px';
}



function togglepop() {
	var q = this.mypop.style;
	if (q.visibility != 'visible') {
		posaft(this);
		q.visibility = 'visible';
	} else {
		q.visibility = 'hidden';
	}
}

function showpop() {
	var q = this.mypop.style;
	posaft(this);
	q.visibility = 'visible';
}

function hidepop() {
	var q = this.mypop.style;
	q.visibility = 'hidden';
	}


function chpop() {
  var toolTC= getElementsByClass("tpop",null,null);
  npop = toolTC.lenght;
	for (i = 1; npop > i; i++) {
//		alert('resi');
		posaft(toolTC[i]);
	}
}

function TTinit() {
  var toolTC= getElementsByClass("tpop",null,null);
  npop = toolTC.length;
//alert("HELLO");
 for (i = 0, j = 0; i < npop; i++) {
   t=toolTC[i];
   ttid =toolTC[i].getAttribute('id');
		
   
	//	t.onclick = showpop;
		t.onmouseover = showpop;
		t.onmouseout = hidepop;
		t.mypop = document.getElementById(ttid+'Tip');
//		alert(ttid+""+"Tip");
	}
	onresize = chpop;
}

/* grab Elements from the DOM by className */

function getElementsByClass(searchClass,node,tag) { 

	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


/* window 'load' attachment */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(TTinit);
// onload=init;
