
// this does not use a timer or the allPopupData array
// this is meant to be called via mouseover/mouseoff

// global var for the popup iframe
// used for ie6 rendering work around

var popupIFrame = null ;

function showPopup( obj, idx ){

	// var popupframe = document.getElementById('popupFrame');

	//
	var popup = document.getElementById( "popup" );
	// var popup = popupframe.contentWindow.document.getElementById( "popup" );
	popup.style.width = '250px';

	// hide the popup if something has gone wrong
	popup.style.visibility = "hidden";

	var glos = document.getElementById('glossary');

	var dt = findDT( idx );
	if( dt == null ){
		return ;
	}

	// need the scolling offset to calc where we put the popup
	var scrollDims = getWindowScrollDims();

	var dd = getDD( dt );

	// we want the popup to be close the the current mouse pos
    var coors = findPos( obj );
    var x = coors[0] + 25 ;
    var y = coors[1] + 20 ;

    popup.style.left = x +'px';
    popup.style.top = y +'px';

    var content = document.getElementById( "popupTerm" );
    content.innerHTML = dt.innerHTML ;

    content = document.getElementById( "popupDesc" );
    content.innerHTML = dd.innerHTML ;

    var dims = getPopupDims( popup );
    var wdims = getWindowDims();
	
	// are we clipping the bottom?
	if( clippingBottom( popup ) ){

    	// widen and try again
		popup.style.width = ( dims[0] + 50 ) + 'px' ;

		var dims = getPopupDims( popup );
    	var wdims = getWindowDims();

		// if we are still clipping try to display above and to the right
		if( clippingBottom( popup ) ){

			// now try above to the right
			var x = coors[0] + 25 ;
    		var y = coors[1] - 15 ;
			// get the current

			var newleft = x
			var newtop = y - dims[1];

			popup.style.top = newtop + 'px';
			popup.style.left = newleft + 'px';

			if( clippingRight( popup ) ){

				// coors is current mouse pos
				var x = coors[0] + 25 ;
    			var y = coors[1] - 15 ;
				// get the current

				var newleft = x - dims[0];
				var newtop = y - dims[1];

				// make sure they are not negative
				if( (newleft > 0) && (newtop > 0) ){
					popup.style.top = newtop + 'px';
					popup.style.left = newleft + 'px';

				}

			}

		}
	}

	// if this is ie6 place the popup iframe
	if( detectIE6() ){

		popupIFrame = document.createElement("IFRAME");
		popupIFrame.setAttribute("src", "");

		// the iframe position must match the popup position
		popupIFrame.style.position="absolute";
		popupIFrame.style.left = popup.offsetLeft + 'px';
		popupIFrame.style.top = popup.offsetTop + 'px';
		popupIFrame.style.width = popup.offsetWidth + 'px';
		popupIFrame.style.height = popup.offsetHeight + 'px';
		popupIFrame.style.zIndex = 50 ;

		document.body.appendChild(popupIFrame);

	}

	// make the popup visible
	popup.style.zIndex = 100 ;
    popup.style.visibility="visible";

	return false ;
}


function clippingBottom( popup ){

	var dims = getPopupDims( popup );
    var wdims = getWindowDims();
    var scrollDims = getWindowScrollDims();

	// are we clipping the bottom?
	var h1 = parseFloat( popup.style.top );
	var h2 = parseFloat( dims[1] );
	var h3 = parseFloat( wdims[1] );
	var h4 = parseFloat( scrollDims[1] );
	if( ( h1 + h2 ) > ( h3 + h4 ) ){
		return true ;
	}

	return false ;
}

function clippingRight( popup ){

	var dims = getPopupDims( popup );
    var wdims = getWindowDims();
    var scrollDims = getWindowScrollDims();

	// are we clipping the bottom?
	var left = parseFloat( popup.style.left );
	var width = parseFloat( dims[0] );
	var w1 = parseFloat( wdims[0] );
	var w2 = parseFloat( scrollDims[0] );
	if( ( left + width ) > ( w1 + w2 ) ){
		return true ;
	}

	return false ;
}

function findDT( idx ){

	var dl = document.getElementById('specifications');

	// loop through the children
	for( var i=0; i<dl.childNodes.length; i++ ){

		var ele = dl.childNodes [i];

		if( ele.nodeName == 'dt' || ele.nodeName == 'DT' ){

			if( ele.firstChild.nodeValue == idx ){
				return ele ;
			}
			// IE 6 work around
			// for some reason IE is appending a trailing ' '
			// to the ele.firstChild.nodeValue string
			if( ele.firstChild.nodeValue.indexOf( idx, 0 ) == 0  ){
				return ele ;
			}

		}
	}

	return null ;
}

function getDD( ele ){

	var sib = ele.nextSibling;
	while( sib != null ){

		if( sib.nodeName == 'DD' || sib.nodeName == 'dd' ){
			return sib;
		}

		sib = sib.nextSibling ;

	}

}


// returns an array with the current mouse pos
function findPos( obj ) {
            var curleft = curtop = 0;
            if( obj.offsetParent) {
                curleft = obj.offsetLeft
                curtop = obj.offsetTop
                while (obj = obj.offsetParent) {
                    curleft += obj.offsetLeft
                    curtop += obj.offsetTop
                }
            }
            return [curleft,curtop];
}


// close the current popup
function closePopup() {

	// hide the popup div
	var popup = document.getElementById( "popup" );
    popup.style.zIndex = -100 ;
    popup.style.visibility="hidden";

    // hide the popup iframe
    if( detectIE6() ){

    	if( popupIFrame ){
    		document.body.removeChild( popupIFrame);
			popupIFrame=null;
    	}
	}

    return false ;
}


function getPopupDims( obj ){
	var w = h = 0;

	if( obj ){
		if( obj.offsetWidth ){
			w = obj.offsetWidth ;
			h = obj.offsetHeight ;
		}
		else if( obj.clip && obj.clip.width ) {
			w = obj.clip.width ;
			h = obj.clip.height;
		}
		else if ( obj.styl && obj.style.pixelWidth ){
			w = obj.style.pixelWidth ;
			h = obj.style.pixelHeight ;
		}
	}

	return [w,h];
}

function getWindowDims(){
	var w = h = 0;
	if( window.innerWidth ){
		w = window.innerWidth ;
		h = window.innerHeight ;
	}
	else if( document.body.parentElement.clientWidth ){
		w = document.body.parentElement.clientWidth ;
		h = document.body.parentElement.clientHeight ;
	}
	else if ( document.body && document.body.clientWidth ){
		w = document.body.clientWidth ;
		h = document.body.clientHeight ;
	}


	return [w,h];
}



function getWindowScrollDims(){
	var sx = sy = 0 ;
	if( document.body && typeof document.body.scrollTop != "undefined" ){
		sx = document.body.scrollLeft ;
		sy = document.body.scrollTop ;
		if( document.body.parentNode
			&& typeof document.body.parentNode.scrollTop != "undefined" ){
			sx = document.body.parentNode.scrollLeft ;
			sy = document.body.parentNode.scrollTop ;
		}
	}
	else if( typeof window.pageXOffset != "undefined" ){
		sx = window.pageXOffset ;
		sy = window.pageYOffset ;
	}

	return [sx,sy];
}


function detectIE6(){
	// some versions of IE7+ also have msie 6 in the userAgent string
	var isIE6 =
		(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
		&& (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);

	return isIE6 ;
}