//Scripts to control popups (From Apple Developer Network)
//
// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var xOffset = 30;
var yOffset = 0;

function showPopup (targetObjectId, eventObj) {

    if(eventObj) {
	    // hide any currently-visible popups
	    hideCurrentPopup();
	    // stop event from bubbling up any farther
	    eventObj.cancelBubble = true;
	    // move popup div to current cursor position 
	    // (add scrollTop to account for scrolling for IE)
	    
	    var viewPortWidth = windowState.getWidth();
        var viewPortHeight = windowState.getHeight();
        var horizontalScroll = windowState.getScrollX();
        var verticalScroll = windowState.getScrollY();
        var divWidth=100;
        var divHeight=100;
	    
	    var hPos = Math.round(horizontalScroll+((viewPortWidth-divWidth)/2));
	    var vPos = Math.round(verticalScroll+((viewPortHeight-divHeight)/2));
	    
	    hPos = (hPos < 0)?0:hPos;
	    vPos = (vPos < 0)?0:vPos;
	    
	    var newXCoordinate = hPos;
	    var newYCoordinate = vPos;
	    //var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
	    //var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
	
	    moveObject(targetObjectId, newXCoordinate, newYCoordinate);
	       //  if ( moveObject(targetObjectId, newXCoordinate, newYCoordinate) ) {
	       //  }
	       // and make it visible
	    
	    if( changeObjectVisibility(targetObjectId, 'visible') ) {
	        // if we successfully showed the popup
	        // store its Id on a globally-accessible object
	        window.currentlyVisiblePopup = targetObjectId;
	        return true;
	    } else {
	        // we couldn't show the popup
	        return false;
	    }
    } else {
        // there was no event object, so we won't be able to position anything, so give up
        return false;
    }
} // showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
        changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
        window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup



// ***********************
// hacks and workarounds *
// ***********************



// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

// globals to help find the center of the window
var windowState = (function(){
var readScroll = {scrollLeft:0,scrollTop:0};
var readSize = {clientWidth:0,clientHeight:0};
var readScrollX = 'scrollLeft';
var readScrollY = 'scrollTop';
var readWidth = 'clientWidth';
var readHeight = 'clientHeight';
function otherWindowTest(obj){
if((document.compatMode)&&
(document.compatMode == 'CSS1Compat')&&
(document.documentElement)){
return document.documentElement;
}else if(document.body){
return document.body;
}else{
return obj;
}
};
if((typeof this.innerHeight == 'number')&&
(typeof this.innerWidth == 'number')){
readSize = this;
readWidth = 'innerWidth';
readHeight = 'innerHeight';
}else{
readSize = otherWindowTest(readSize);
}
if((typeof this.pageYOffset == 'number')&&
(typeof this.pageXOffset == 'number')){
readScroll = this;
readScrollY = 'pageYOffset';
readScrollX = 'pageXOffset';
}else{
readScroll = otherWindowTest(readScroll);
}
return {
getScrollX:function(){
return (readScroll[readScrollX]||0);
},
getScrollY:function(){
return (readScroll[readScrollY]||0);
},
getWidth:function(){
return (readSize[readWidth]||0);
},
getHeight:function(){
return (readSize[readHeight]||0);
}
};
})();

