var isNS = false;
var isIE6 = false;
var isIE = false;

if(navigator.appName == "Microsoft Internet Explorer")
    isIE6 = true;
if(navigator.appName == "Netscape")
    isNS = true;

function ShowPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset)
{    
    DrawPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset);
} 

function RefreshPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset)
{    
    ClosePopupWindow(iFrame, popupWindowDiv);    
    DrawPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset); 
}

function CorrectPosition(popupWindowDiv,x_offset)
{

   //popupWindowDiv.style.display = 'block';
    
   var iconY = getY(popupWindowDiv);
   if(iconY < 0) 
     iconY = 0;
   
   var iconX = getX(popupWindowDiv);
   if(iconX < 0) 
     iconX = 0;
     
   if (x_offset == null)
         x_offset = 0;
     
   var right = iconX + popupWindowDiv.offsetWidth;
   var bottom = iconY + popupWindowDiv.offsetHeight;
                
    //we'll need 4 display positions depending on location of iconElement
    // - topLeft, bottomLeft, topRight, bottomRight
    //... choose the correct one depending on whether or not the popupWindowDiv is completely on the page(make sure it is!)
    
    var Canvas = new Object();
    
    getDisplayDims(Canvas);

    //alert("Right:" + Canvas.Right + " Bottom:" + Canvas.Bottom);
    
     

  if((Canvas.Bottom) < bottom)//use an upper position
  {
    if((Canvas.Right) < right)
    {
        //upper left
        if ((bottom - Canvas.Bottom)<iconY)
            popupWindowDiv.style.top =  - (bottom - Canvas.Bottom) + 'px';
        else
            popupWindowDiv.style.top = - iconY+'px';
            
        popupWindowDiv.style.left = (x_offset - (right - Canvas.Right)) + 'px'; 
    }
    else
    {   
        //upper 
        if ((bottom - Canvas.Bottom)<iconY)
            popupWindowDiv.style.top =  - (bottom - Canvas.Bottom) + 'px';
        else
            popupWindowDiv.style.top = - iconY+'px';
    }
  }
  else//use a lower position
  {
    if(Canvas.Right < right)
    {
        //left
        popupWindowDiv.style.left = (x_offset- (right - Canvas.Right))  + 'px'; 
         
    }
    else
    {        
        //Its ok! 
    }
  }     
      
}


function DrawPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset)
{
    if( xOffset == null)
        xOffset  = 0;
    if (iconElement==null)
    {
        popupWindowDiv.style.display = 'block';
        var iconY = popupWindowDiv.offsetHeight;
        var iconX = popupWindowDiv.offsetWidth;
    }
    else
    {
        var iconY = getY(iconElement);
        var iconX = getX(iconElement) + xOffset;
        if(iconX < 0) 
            iconX = 0;
    }     
    //we'll need 4 display positions depending on location of iconElement
    // - topLeft, bottomLeft, topRight, bottomRight
    //... choose the correct one depending on whether or not the popupWindowDiv is completely on the page(make sure it is!)
    
    var Canvas = new Object();
    
    getDisplayDims(Canvas);

    //alert("Right:" + Canvas.Right + " Bottom:" + Canvas.Bottom);
    
    popupWindowDiv.style.display = 'block'; 

  if((Canvas.Bottom - iconY) < popupWindowDiv.offsetHeight)//use an upper position
  {
    iconY = iconY - (iconY + popupWindowDiv.offsetHeight - Canvas.Bottom) ;
    if (iconY<0) 
        iconY=0;
    if((Canvas.Right - iconX) < popupWindowDiv.offsetWidth)
    {
        //upper left
        popupWindowDiv.style.top = iFrame.style.top = iconY + 'px';
        iconX = iconX - (iconX + popupWindowDiv.offsetWidth - Canvas.Right) ;
        if (iconX<0) 
            iconX=0;
        popupWindowDiv.style.left = iFrame.style.left = iconX + 'px'; 
    }
    else
    {   
        //upper right
        popupWindowDiv.style.top = iFrame.style.top = iconY + 'px';
        popupWindowDiv.style.left = iFrame.style.left = iconX + 'px'; 
    }
  }
  else//use a lower position
  {
    if((Canvas.Right - iconX) < popupWindowDiv.offsetWidth)
    {
        //bottom left
        popupWindowDiv.style.top = iFrame.style.top = iconY + 'px';
        iconX = iconX - (iconX + popupWindowDiv.offsetWidth - Canvas.Right) ;
        if (iconX<0) 
            iconX=0;
        popupWindowDiv.style.left = iFrame.style.left = iconX + 'px'; 
    }
    else
    {        
        //bottom right
        popupWindowDiv.style.top = iFrame.style.top  = iconY  + 'px';
        popupWindowDiv.style.left = iFrame.style.left = iconX + 'px'; 
    }
  }     
    
    iFrame.style.display = 'block';   
    iFrame.style.width= popupWindowDiv.offsetWidth+"px";
    iFrame.style.height = popupWindowDiv.offsetHeight+"px"; //bottom height    
}



function getDisplayDims(CANVAS) {
  var CWidth, CHeight, CScrollX, CScrollY;

  if (isNS) {
    CWidth = self.innerWidth;
    CHeight = self.innerHeight;
    CScrollX = window.pageXOffset;
    CScrollY = window.pageYOffset;
  } else if (isIE6) {
    CWidth = document.documentElement.clientWidth;
    CHeight = document.documentElement.clientHeight;
    if (CWidth==0) {
      CWidth = document.body.clientWidth;
      CHeight = document.body.clientHeight;
    }
    CScrollX = document.body.scrollLeft +
               document.documentElement.scrollLeft;
    CScrollY = document.body.scrollTop +
               document.documentElement.scrollTop;
  } else if (isIE) {
    CWidth = document.body.clientWidth;
    CHeight = document.body.clientHeight;
    CScrollX = document.body.scrollLeft;
    CScrollY = document.body.scrollTop;
  }
  CANVAS.Left    = CScrollX;
  CANVAS.Right   = CWidth + CScrollX;
  CANVAS.Top     = CScrollY;
  CANVAS.Bottom  = CHeight + CScrollY;
  CANVAS.xMiddle = Math.round(CWidth/2) + CScrollX;
  CANVAS.yMiddle = Math.round(CHeight/2) + CScrollY;
}

function ClosePopupWindow(iFrame, popupWindowDiv)
{    
    popupWindowDiv.style.display = 'none';
    iFrame.style.display = 'none';
}

//function biggestSizeInfo(iconElement, iFrame, popupWindowDiv, textElement, xOffset)
//{
//    UpdateSize(textElement,"20px");
//    RefreshPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset);
//}
//function biggerSizeInfo(iconElement, iFrame, popupWindowDiv,textElement, xOffset)
//{
//    UpdateSize(textElement,"14px");
//    RefreshPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset);
//}
//function normalSizeInfo(iconElement, iFrame, popupWindowDiv,textElement, xOffset)
//{
//    UpdateSize(textElement,"11px");
//    RefreshPopupWindow(iconElement, iFrame, popupWindowDiv, xOffset);
//}

//function UpdateSize(element,size)
//{
//    var i=0;
//    element.style.fontSize=size;
//    if (element.children!=null)
//    {
//        for(i=0;i<element.children.length;i++)
//        {
//            UpdateSize(element.children[i],size);
//        }
//    }
//}

function getY( oElement )
{
    var iReturnValue = 0;    
    while( oElement != null ) 
    {        
        iReturnValue += oElement.offsetTop;
        //if( oElement.offsetTop > 0)
        //    return oElement.offsetTop;  
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
    //return 0; 
}
function getX( oElement)
{
    var iReturnValue = 0;            
    while( oElement != null ) 
    {
        iReturnValue += oElement.offsetLeft; 
        //if( oElement.offsetLeft > 0)
        //    return oElement.offsetLeft;         
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
    //return 0; 
}

function CenterPopupWindowOnScreen(iFrameName, popupWindowDiv)
{
    var iFrame = document.getElementById(iFrameName); 
    var Canvas = new Object();
    getDisplayDims(Canvas);
     
    popupWindowDiv.style.display = 'block';
    popupWindowDiv.style.top = iFrame.style.top = Canvas.yMiddle - (popupWindowDiv.offsetHeight/2)  + 'px';
    popupWindowDiv.style.left = iFrame.style.left = Canvas.xMiddle - (popupWindowDiv.offsetWidth/2) + 'px';
       
    iFrame.style.display = 'block';
    iFrame.style.width= popupWindowDiv.offsetWidth+"px";
    iFrame.style.height = popupWindowDiv.offsetHeight+"px"; //bottom height  
}
