var undefined; //Important for older browsers that don't support 'undefined' by default

if (innerPopup == undefined) {
  var innerPopup = "<<CONTENT>>";
}
if (offsetTop == undefined) {
  var offsetTop = 8;
}
if (offsetLeft == undefined) {
  var offsetLeft = 8;
}
if (mouseOffTime == undefined) {
  var mouseOffTime = 400;
}
if (moveWithMouse == undefined) {
  var moveWithMouse = true;
}

document.write("<div id='yttpopup' style='position: absolute; display: none; z-index: 99;' onmouseover='clearTimeout(window.timeout)' onmouseout='yttpopupHide()'>"+innerPopup+"</div>");

var moveDiv = false;
var done = false;
var newdiv = new Array();

var windowWidth,windowHeight;

function yttpopupGetBoundries() {
  if (self.innerHeight) { //Mozilla
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  }
  else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
}

function returnObjById(id) {
  if (document.getElementById)
    var returnVar = document.getElementById(id);
  else if (document.all)
    var returnVar = document.all[id];
  else if (document.layers)
    var returnVar = document.layers[id];
  return returnVar;
}

function yttpopupInit() {
  for (var i=0;i < document.links.length;i++) {    var link = document.links[i];
    var content = link.getAttribute('yttpopup');    if (content!=null) {
    	if(window.addEventListener){ // Mozilla
        link.addEventListener("mouseover", yttpopupShow, false);
        link.addEventListener("mouseout", yttpopupHide, false);
      } else { // IE
      	link.attachEvent("onmouseover", yttpopupShow, false);
      	link.attachEvent("onmouseout", yttpopupHide, false);
      }
    	link.content = content;

    	htmlContent = innerPopup;
      htmlContent = htmlContent.replace(/<<CONTENT>>/,content);

	    idcontent = content.replace(/[^A-Za-z]/g,'');

	    newdiv[i] = document.createElement('div');
      newdiv[i].setAttribute('id', idcontent);
      newdiv[i].style.position = "absolute";
      newdiv[i].style.display = "none";
      newdiv[i].innerHTML = htmlContent
      document.body.appendChild(newdiv[i]);
    }
  }
}

function yttpopupShow(evt) {
  var content;
  var ie_var = "srcElement";
  var moz_var = "target";

  var prop_var = "content";
  // "target" for Mozilla, "srcElement" for IE
	evt[moz_var] ? content = evt[moz_var][prop_var] : content = evt[ie_var][prop_var];

	moveDiv=true;
	htmlContent = innerPopup;
  htmlContent = htmlContent.replace(/<<CONTENT>>/,content);

	returnObjById('yttpopup').innerHTML = htmlContent;
	returnObjById('yttpopup').style.display = 'block';
	clearTimeout(window.timeout);
}

function yttpopupHide() {
  window.timeout = setTimeout("returnObjById('yttpopup').style.display = 'none'; moveDiv=false;", mouseOffTime);
}

document.onmousemove = function(evt) {	if (moveDiv===true) {    yttpopupGetBoundries();
  	if (!evt) evt = window.event;

  	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
  	var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;

  	x = evt.clientX;
  	y = evt.clientY;

  	popupHeight = returnObjById('yttpopup').offsetHeight;
  	popupWidth = returnObjById('yttpopup').offsetWidth;

  	//Don't go off the top or bottom
  	if ( (y+popupHeight+offsetTop+10<windowHeight) && (y+offsetTop-10>0) ) {
      returnObjById('yttpopup').style.top = ((y+offsetTop+scrollTop) + 'px');
    }
    else if (y+offsetTop-10<0) {
      returnObjById('yttpopup').style.top = 10+scrollTop;
    }
    else if (y+popupHeight+offsetTop+10>windowHeight) {
      returnObjById('yttpopup').style.top = (windowHeight-popupHeight-10+scrollTop);
    }

    //Don't go off the left or right
    if ( (x+popupWidth+offsetLeft+10<windowWidth) && (x+offsetLeft-10>0) ) {
      returnObjById('yttpopup').style.left = ((x+offsetLeft+scrollLeft) + 'px');
    }
    else if (x+offsetLeft-10<0) {
      returnObjById('yttpopup').style.left = 10+scrollLeft;
    }
    else if (x+popupWidth+offsetLeft+10>windowWidth) {
      returnObjById('yttpopup').style.left = (windowWidth-popupWidth-10+scrollLeft);
    }
    if (moveWithMouse===false) {      moveDiv=false;
    }
	}
}
