// Get browser version fun
var isIE = document.all?true:false;

function getIsIE() {
        return isIE;
}

function getObjHeight(id) {
        if (isIE)
                return document.getElementById(id).clientHeight;
        else
                return document.getElementById(id).offsetHeight;
}

function getObjWidth(id) {
        if (isIE)
                return document.getElementById(id).clientWidth;
        else
                return document.getElementById(id).offsetWidth;
}

function getInsideWindowWidth() {
        if (isIE)
                return document.body.clientWidth + document.body.scrollLeft;
        else
                return window.innerWidth + window.pageXOffset;
}

function getInsideWindowHeight() {
        if (isIE)
                return document.body.clientHeight + document.body.scrollTop;
        else
                return window.innerHeight + window.pageYOffset;
}

function getRealWindowWidth() {
        if (isIE)
                return document.documentElement.clientWidth;
        else
                return window.innerWidth;
}

function getRealWindowHeight() {
        if (isIE)
                return document.documentElement.clientHeight;
        else
                return window.innerHeight;
}

function getScrollDistance() {
        if (isIE)
                return document.documentElement.scrollTop;
        else
                return window.pageYOffset;
}

// Actual functions that do things
function popup_text(id,e) {
        var _x;
        var _y;
        var xoffset = 10;
        var yoffset = 10;
        var objWidth = getObjWidth(id);
        var objHeight = getObjHeight(id);
        if(!getIsIE()) {
                _x = e.pageX + xoffset;
                _y = e.pageY + yoffset;
        } else {
                _x = event.clientX + document.body.scrollLeft + xoffset;
                _y = event.clientY + document.body.scrollTop + yoffset;
        }
        if(_x + objWidth > getInsideWindowWidth())
                _x = _x - (xoffset*2) - objWidth;
        if(_y + objHeight > getInsideWindowHeight())
                _y = _y - (yoffset*2) - objHeight;

        document.getElementById(id).style.visibility = 'visible';
        document.getElementById(id).style.left = _x+"px";
        document.getElementById(id).style.top = _y+"px";
}

function popup_pic(id,e) {
  var _x;
  var _y;
  var objWidth = getObjWidth(id);
  var objHeight = getObjHeight(id);
//   var startObjWidth = getObjWidth(id);
//   var startObjHeight = getObjWidth(id);
  var maxHeight = getRealWindowHeight();
  var maxWidth = 500;
  
  var bufferLeft = (getInsideWindowWidth() - 910)/2;
  
  var targetHeight;
  var targetWidth;
  var targetScale;
  var imgid = id+'img';

if (objHeight > maxHeight) {
  targetHeight = .9 * maxHeight;
  targetScale = targetHeight/objHeight; // use to scale width
  var targetWidth = targetScale * objWidth;

  document.getElementById(imgid).style.height = targetHeight+"px";
  document.getElementById(id).style.height = "auto";

  document.getElementById(imgid).style.width = targetWidth+"px";
  document.getElementById(id).style.width = targetWidth+"px";

}
var objWidth = getObjWidth(id);
if (objWidth > maxWidth) {

  targetWidth = maxWidth;
  document.getElementById(id).style.width = targetWidth+"px";
  document.getElementById(imgid).style.width = targetWidth+"px";
  targetScale = targetWidth/objWidth;
}

var finalWidth = getObjWidth(id);
var finalHeight = getObjHeight(id);

_x = (500 - finalWidth)/2 + bufferLeft;
_y = (getRealWindowHeight() - finalHeight)/2 + getScrollDistance();

        document.getElementById(id).style.visibility = 'visible';
        document.getElementById(id).style.left = _x+"px";
        document.getElementById(id).style.top = _y+"px";
}


function hide(id) {
document.getElementById(id).style.visibility = 'hidden';
}

function source(iden) {
	document.writeln('<a class=foot onMouseOver="popup_text(&#039;' + iden + '&#039;, event);" onMouseOut=hide(&#039;' + iden + '&#039;);><span id="close">source</span></a>');
}

