var orgApacheMyfacesPopupCurrentlyOpenedPopup;
var orgApacheMyfacesPopupFrameUnder;

/**
* fix for the div over control bug in ie
*/
function orgApacheMyfacesPopupfixIE() {
    if(document.all) {
      if(orgApacheMyfacesPopupCurrentlyOpenedPopup == null) return false;
      var iframe = document.getElementById(orgApacheMyfacesPopupCurrentlyOpenedPopup.id+"_IFRAME");


    if(iframe == null) {
      orgApacheMyfacesPopupFrameUnder = document.createElement("<iframe id='"+orgApacheMyfacesPopupCurrentlyOpenedPopup.id+"_IFRAME' style='visibility:hidden;position: absolute;  top:0px;left:0px; ' />");
        document.body.insertBefore(orgApacheMyfacesPopupFrameUnder);
      } else {
        orgApacheMyfacesPopupFrameUnder = iframe;
      }

    var popup  = orgApacheMyfacesPopupCurrentlyOpenedPopup;
      iframe = orgApacheMyfacesPopupFrameUnder;

      if(popup != null &&
        (popup.style.display == "block")) {
       
        //--fix the z index
        popup.style.zIndex  = popup.offsetParent.style.zIndex;
        if( popup.id.indexOf("higherPopup")>-1 )
        {
          popup.style.zIndex=popup.style.zIndex+1;
        }
  
   		iframe.style.zIndex = popup.style.zIndex - 1;
   		//--
        
        iframe.style.width  = popup.offsetWidth;
        iframe.style.height = popup.offsetHeight;
                
        iframe.style.top  = popup.style.top;
        iframe.style.left   = popup.style.left;
        
        var aTag = getLastAbsoluteTag(  popup);                
        addToTop( iframe, aTag.offsetTop);
        addToLeft( iframe, aTag.offsetLeft );
        
        iframe.style.marginTop    = popup.style.marginTop;
        iframe.style.marginLeft   = popup.style.marginLeft;
        iframe.style.marginRight  = popup.style.marginRight;
        iframe.style.marginBottem   = popup.style.marginBottom;

      iframe.style.display = "block";
     iframe.style.visibility = "visible"; /*we have to set an explicit visible otherwise it wont work*/
      } else {
        iframe.style.display = "none";
      }
    }
    return false;
}


function orgApacheMyfacesPopup(popupId,displayAtDistanceX,displayAtDistanceY)
{

    this.popupId = popupId;
    this.displayAtDistanceX=displayAtDistanceX;
    this.displayAtDistanceY=displayAtDistanceY;
    this.display = orgApacheMyfacesPopupDisplay;
    this.hide = orgApacheMyfacesPopupHide;
    this.redisplay=orgApacheMyfacesPopupRedisplay;
}

function orgApacheMyfacesPopupDisplay(ev)
{

    if(orgApacheMyfacesPopupCurrentlyOpenedPopup!=null)
        orgApacheMyfacesPopupCurrentlyOpenedPopup.style.display="none";

    var elem;
    var x;
    var y;

    if(document.all) /*ie browser detection already in place*/
    {
    if(orgApacheMyfacesPopupFrameUnder!=null)
      orgApacheMyfacesPopupFrameUnder.style.display="none";

        elem = window.event.srcElement;
        x=window.event.clientX;
        x+=orgApacheMyfacesPopupGetScrollingX();
        y=window.event.clientY;
        y+=orgApacheMyfacesPopupGetScrollingY();
    }
    else
    {
        elem = ev.target;
        x=ev.pageX;
        y=ev.pageY;
    }

    x+=this.displayAtDistanceX;
    y+=this.displayAtDistanceY;

    var popupElem = document.getElementById(this.popupId);

    if(popupElem.style.display!="block")
    {
        popupElem.style.display="block";
        popupElem.style.left=""+x+"px";
        popupElem.style.top=""+y+"px";
        orgApacheMyfacesPopupCurrentlyOpenedPopup = popupElem;
    }
    
    fixAbsolutePositioning();       
    orgApacheMyfacesPopupfixIE();
}

/**
* the popupElem mdoesn't have absolute positioning and it may be contaiend in a div with absolute position... problem
*/
function fixAbsolutePositioning(){
      var aTag  = orgApacheMyfacesPopupCurrentlyOpenedPopup;     

     aTag = getLastAbsoluteTag(  aTag);
        
     
        
        addToTop( orgApacheMyfacesPopupCurrentlyOpenedPopup, -1*aTag.offsetTop );
        addToLeft( orgApacheMyfacesPopupCurrentlyOpenedPopup, -1*aTag.offsetLeft );
}

function getLastAbsoluteTag(aTag){
        do {

          var str;
          aTag = aTag.offsetParent;
          if( aTag.currentStyle) 
            str = aTag.currentStyle.position; 
          else if( window.getComputedStyle) 
            str = window.getComputedStyle( aTag, null).getPropertyValue( "position");
           
          if (str == "absolute") {              
              break;              
          }                                                 
        }
        while (aTag.tagName != "BODY") ;
  
        return aTag;
}

function addToTop( aTag, offset ){
        var topText = aTag.style.top;
        var topFixed=parseInt(topText.replace( "px", "" ))+parseInt(offset);
        aTag.style.top = ""+topFixed+"px";
}

function addToLeft( aTag, offset ){
        var leftText = aTag.style.left;
        var leftFixed=parseInt(leftText.replace( "px", "" ))+parseInt(offset);
        aTag.style.left = ""+leftFixed+"px";
}

/**
* hide function which
* hides the popup from the browser
*/
function orgApacheMyfacesPopupHide()
{
    var popupElem = document.getElementById(this.popupId);
    popupElem.style.display="none";

  if(document.all && (orgApacheMyfacesPopupFrameUnder != null)) { /*ie specific popup under fix*/
    orgApacheMyfacesPopupFrameUnder.style.display = "none";
  }
  orgApacheMyfacesPopupfixIE();
}

/**
* simple redisplay
* displays an already existing hidden popup
*/
function orgApacheMyfacesPopupRedisplay()
{
    var popupElem = document.getElementById(this.popupId);
    popupElem.style.display="block";
    orgApacheMyfacesPopupCurrentlyOpenedPopup = popupElem;
  orgApacheMyfacesPopupfixIE();
}

function orgApacheMyfacesPopupGetScrollingX() {
    if (self.pageXOffset) {
        return self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        return document.documentElement.scrollLeft;
    } else if (document.body) {
        return document.body.scrollLeft;
    } else {
        return 0;
    }
}

function orgApacheMyfacesPopupGetScrollingY() {
    if (self.pageYOffset) {
        return self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        return document.documentElement.scrollTop;
    } else if (document.body) {
        return document.body.scrollTop;
    } else {
        return 0;
    }
}

function doAsOnMouseOver(ev){
  var theElement;
  if( ev.srcElement ){
    theElement = ev.srcElement;
  }
  else{
    theElement=ev.target;
  }
    theElement.onclick = theElement.onmouseover;
    theElement.onclick(ev);
}

function doAsOnMouseOut( ev ){
  var theElement;
  if( ev.srcElement ){
    theElement = ev.srcElement;
  }
  else{
    theElement=ev.target;
  }
  var theLabel = getLabelElement( theElement );
  theElement.onclick=theLabel.onmouseout;
  theElement.onclick(ev);
}

function getLabelElement( aTag ){
      do {

          var str;
          aTag = aTag.offsetParent;                                           
        }
        while (aTag.tagName != "DIV") ;
     return aTag.previousSibling;
}
