// fullUtils.js
// Copyright (c) 2003 - 2008 Citrix Systems, Inc. All Rights Reserved.
// Web Interface 5.0.1.29110


function getElementPosition(elt)
{
  var positionX = 0;
  var positionY = 0;

  while (elt != null)
  {
    positionX += elt.offsetLeft;
    positionY += elt.offsetTop;
    elt = elt.offsetParent;
  }

  return [positionX, positionY];
}


function getFrameViewportSize()
{
  var sizeX = 0;
  var sizeY = 0;

  if (typeof window.innerWidth != 'undefined')
  {
      sizeX = window.innerWidth;
      sizeY = window.innerHeight;
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
  {
      sizeX = document.documentElement.clientWidth;
      sizeY = document.documentElement.clientHeight;
  }
  else
  {
      sizeX = document.getElementsByTagName('body')[0].clientWidth;
      sizeY = document.getElementsByTagName('body')[0].clientHeight;
  }

  return [sizeX, sizeY];
}


function getFrameScrollingPosition()
{
  var scrollX = 0;
  var scrollY = 0;

  if (typeof window.pageYOffset != 'undefined')
  {
      scrollX = window.pageXOffset;
      scrollY = window.pageYOffset;
  }

  else if (typeof document.documentElement.scrollTop != 'undefined'
      && (document.documentElement.scrollTop > 0 ||
      document.documentElement.scrollLeft > 0))
  {
      scrollX = document.documentElement.scrollLeft;
      scrollY = document.documentElement.scrollTop;
  }

  else if (typeof document.body.scrollTop != 'undefined')
  {
      scrollX = document.body.scrollLeft;
      scrollY = document.body.scrollTop;
  }

  return [scrollX, scrollY];
}


function isMatchedAttribute(element, attribute, attributeValue)
{
    if (attribute == "class")
    {
      var pattern = new RegExp("(^| )" + attributeValue + "( |$)");

      return pattern.test(element.className);
    }
    else if (attribute == "for")
    {
      if (element.getAttribute("htmlFor") || element.getAttribute("for"))
      {
        return element.htmlFor == attributeValue;
      }
    }
    else
    {
        return element.getAttribute(attribute) == attributeValue;
    }
}


function getFrameCursorPosition(event)
{
    if (typeof event == "undefined")
    {
        event = window.event;
    }

    var scrollingPosition = getFrameScrollingPosition();
    var cursorX = 0;
    var cursorY = 0;

    if (typeof event.pageX != "undefined" && typeof event.x != "undefined")
    {
        cursorX = event.pageX;
        cursorY = event.pageY;
    }
    else
    {
        cursorX = event.clientX + scrollingPosition[0];
        cursorY = event.clientY + scrollingPosition[1];
    }

    return [cursorX, cursorY];
}


function getEventTarget(event)
{
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  return targetElement;
}


function attachEventHandler(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldHandler = target[eventType];

      target[eventType] = function()
      {
        oldHandler();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true;
}

function getDefaultPopupShowDelay()
{
    return 500;
}


function getDefaultPopupHideDelay()
{
    return 200;
}


function getPopupId(associatedId)
{
    return "Popup_" + associatedId;
}


function show_popup_helper(associatedId)
{
    clearPopupTimer(associatedId);

    var popupId = getPopupId(associatedId);
    var popup = document.getElementById(popupId);
    var associated = document.getElementById(associatedId);

    
    if(!isPopupWanted(associatedId))
    {
        return;
    }

    
    var desiredPosition = popup.savedCursorPosition;
    var onScreenPosition;

    if(desiredPosition)
    {
        onScreenPosition = shuffle(popup, desiredPosition, 10, 25);
    }
    else
    {
        
        desiredPosition = getElementPosition(associated);
        
        desiredPosition = [desiredPosition[0]+1, desiredPosition[1]+2];
        
        onScreenPosition = shuffle(popup, desiredPosition, 0, associated.offsetHeight);
    }

    popup.style.left = onScreenPosition[0] + 'px';
    popup.style.top = onScreenPosition[1] + 'px';

    
}


shuffle.margin = 25;


function shuffle(popup, position, offsetAbove, offsetBelow)
{
    viewPortSize = getFrameViewportSize();
    scollingPos = getFrameScrollingPosition();

    
    var rightmost = position[0] + popup.offsetWidth;
    var rightmostVisible = scollingPos[0] + viewPortSize[0] - shuffle.margin;
    if( rightmost > rightmostVisible )
    {
        position[0] = rightmostVisible - popup.offsetWidth;
    }

    
    var leftmost = position[0];
    var leftmostVisible = scollingPos[0] + shuffle.margin;
    if( leftmost < leftmostVisible )
    {
        position[0] = leftmostVisible;
    }

    
    var bottommost = position[1] + popup.offsetHeight + offsetBelow;
    var bottommostVisible = scollingPos[1] + viewPortSize[1] - shuffle.margin;
    if( bottommost > bottommostVisible )
    {
        
        var topmost = position[1] - popup.offsetHeight - offsetAbove;
        var topmostVisible = scollingPos[1] + shuffle.margin;
        if( topmost >= topmostVisible )
        {
            
            position[1] -= (popup.offsetHeight + offsetAbove);
        }
        else {
            
            position[1] = topmostVisible;
        }
    }
    else {
        
        position[1] += offsetBelow;
    }

    return position;
}


function hide_popup_help(associatedId)
{
    clearPopupTimer(associatedId); // this function can be a timer callback, so clear any timer

    var popupId = getPopupId(associatedId);
    var popup = document.getElementById(popupId);

    if( popup != null )
    {
        removeIframeLayer(popup);
        popup.style.left = '-999px';
        popup.style.top = '-999px';
        popup.savedCursorPosition = null;
    }
}


function createIframeLayer(popup)
{
    
    var layer = popup.iframeLayer;

    if(layer==null)
    {
        
        layer = document.createElement('iframe');
        layer.className = "hiddenFrameLayer"; 
        layer.tabIndex = '-1';
        layer.src = 'javascript:false;';
        popup.parentNode.appendChild(layer);

        
        popup.iframeLayer = layer;
    }

    
    layer.style.left = popup.offsetLeft + 'px';
    layer.style.top = popup.offsetTop + 'px';
    layer.style.width = popup.offsetWidth + 'px';
    layer.style.height = popup.offsetHeight + 'px';
}


function removeIframeLayer(popup)
{
    var layer = popup.iframeLayer;

    if(layer != null )
    {
        layer.parentNode.removeChild(layer);
        popup.iframeLayer = null;
    }
}



function setPopupWanted(associatedId, timer, wanted)
{
    associated = document.getElementById(associatedId);
    associated.popupWanted = wanted;
    
    clearPopupTimer(associatedId);

    
    associated.popupTimer = timer;
}

function isPopupWanted(associatedId)
{
    associated = document.getElementById(associatedId);
    return associated.popupWanted;
}

function clearPopupTimer(associatedId)
{
    associated = document.getElementById(associatedId);
    if(associated.popupTimer != null)
    {
        window.clearTimeout(associated.popupTimer);
        associated.popupTimer = null;
    }
}


function setup_inline_help(eltId)
{
    var associated = document.getElementById(eltId);

    setup_message_summary(eltId); 

    associated.onclick=function()
    {
        wi_popup_show(eltId);

        return false; 
    }

    associated.onblur=function()    
    {
        wi_popup_hide_delayed(eltId);
    }

    var popup = document.getElementById(getPopupId(eltId));
    if(popup) {
        
        popup.onmouseover = popup.onclick = function() { setPopupWanted(eltId, null, true); }
        popup.onmouseout = function() { wi_popup_hide_delayed(eltId); }
    }
}


function setup_message_summary(eltId)
{
    var associated = document.getElementById(eltId);

    associated.hasPopup = true;

    associated.onmouseover=function()
    {
        wi_popup_show_delayed(eltId);
    }

    associated.onmouseout=function()
    {
        wi_popup_hide_delayed(eltId);
    }

    attachEventHandler(associated, "mousemove", record_cursor_position, false);
}


function setup_drop_down_menu(eltId)
{
    var associated = document.getElementById(eltId);
    var popupId = getPopupId(eltId);
    var popup = document.getElementById(popupId);

    associated.hasPopup = true;

    // reduce the delay to 100ms from the default of 500ms
    var delay = 100;
    popup.onmouseover=function()
    {
        wi_popup_show_delayed(eltId, delay);
    }

    popup.onmouseout=function()
    {
        wi_popup_hide_delayed(eltId, delay);
    }

    associated.onmouseover=function()
    {
        wi_popup_show_delayed(eltId, delay);
    }

    associated.onmouseout=function()
    {
        wi_popup_hide_delayed(eltId, delay);
    }

    var show = function() { wi_popup_show(eltId); };
    var hide = function() { wi_popup_hide(eltId); };

    // ensure keyboard navigation opens the menu
    var popupLinks = popup.getElementsByTagName("a");
    for(var i=0;i<popupLinks.length;i++) {
        popupLinks[i].onfocus = show;
        popupLinks[i].onblur = hide;
    };
}


function record_cursor_position(event)
{
    var target = getEventTarget(event);

    
    while(target)
    {
        if(target.hasPopup)
        {
            break;
        }

        target = target.parentNode;
    }

    if(target)
    {
        var popup = document.getElementById(getPopupId(target.id));
        popup.savedCursorPosition = getFrameCursorPosition(event);
    }

    return true;
}


function setup_behaviour_helper(elementClass, behaviour)
{
    var content = document.getElementById("content");
    if(content)
    {
        
        apply_behaviour(elementClass, behaviour, content.getElementsByTagName("a"));
        apply_behaviour(elementClass, behaviour, content.getElementsByTagName("li"));
    }
}

function apply_behaviour(elementClass, behaviour, elements)
{
        for (var i = 0; i < elements.length; i++)
        {
            if(isMatchedAttribute(elements[i], "class", elementClass))
            {
                behaviour(elements[i].id);
            }
        }
}


function wi_popup_show(associatedId)
{
    wi_popup_show_delayed(associatedId, 0);
}


function wi_popup_show_delayed(associatedId, delay)
{
    if(delay==null)
    {
        delay = getDefaultPopupShowDelay();
    }

    if( delay > 0 )
    {
        setPopupWanted(
            associatedId,
            window.setTimeout("show_popup_helper('" + associatedId + "');", delay),
            true
            );
    }
    else
    {
        setPopupWanted(associatedId, null, true);
        show_popup_helper(associatedId);
    }
}


function wi_popup_hide(associatedId)
{
    wi_popup_hide_delayed(associatedId, 0);
}


function wi_popup_hide_delayed(associatedId, delay)
{
    if(delay==null)
    {
        delay = getDefaultPopupHideDelay();
    }

    if( delay > 0 )
    {
        setPopupWanted(
            associatedId,
            window.setTimeout("hide_popup_help('" + associatedId + "');", delay),
            false
            );
    }
    else
    {
        setPopupWanted(associatedId, null, false);
        hide_popup_help(associatedId);
    }
}


function setup_popup_behaviour()
{

    setup_behaviour_helper("inlineHelpLink", setup_inline_help); 
    setup_behaviour_helper("dropDownMenu", setup_drop_down_menu); 
    setup_behaviour_helper("messageSummary", setup_message_summary); 
}


function updateLayout() {
	/*
     if (document.getElementById) {
        var viewportSize = getFrameViewportSize();
        positionFooter(viewportSize[1]);
        
    }
 */
}


function positionFooter(viewportHeight) {
     if (viewportHeight > 0) {
        var totalHeight = 0;

        var contentElement = document.getElementById("content");
        if(contentElement) {
            totalHeight += contentElement.offsetHeight;
        }

       var footerElement = document.getElementById("footer");
        var footerHeight = 0;
        if (footerElement) {
            footerHeight = footerElement.offsetHeight;
            totalHeight += footerHeight;
        }

        var space = viewportHeight - totalHeight;

        if (space > 0) {
            var heightFillerElement = document.getElementById("heightFiller");
            if (heightFillerElement) {
                heightFillerElement.style.height = space + 'px';
            }
        }
    } 
}


function setOverallWrapperSize(viewportWidth) {
    // get width of wrapper, but leave room for borders either side
    var wrapperWidth = viewportWidth - 40;

    // ensure it is within limits
    if (wrapperWidth < 780) {
        wrapperWidth = 780;
    }
    if (wrapperWidth > 1000) {
        wrapperWidth = 1000;
    }

    // save as the best guess for next time
    setItemInCookie("wrapperWidth", wrapperWidth);

    var overallWrapper = document.getElementById("overallWrapper");
    if (overallWrapper) {
        overallWrapper.style.width = wrapperWidth + 'px';
    }
}

window.onresize = updateLayout;


//----------------------
//--- CDI Functions
//----------------------
function wait(msecs) {
   var start = new Date().getTime();
   var cur = start
   while(cur - start < msecs) {
      cur = new Date().getTime();
   }
} 
 
function removeElement(containerElem, elemId) {
   var parentElem = document.getElementById(containerElem);
   var elem = document.getElementById(elemId);
   if (parentElem != null && elem != null) {
      parentElem.removeChild(elem);
   }
}
 
function loadScript(elemIn, elemOut, containerElem) {
   if (document.getElementById(elemOut) == null && document.getElementById(elemIn) != null) { // does not exist
      var head = document.getElementsByTagName("head")[0];
      script = document.createElement("script");
      script.id = elemOut;
      script.type = "text/javascript";
	  script.text = document.getElementById(elemIn).innerHTML;
      removeElement(containerElem, elemIn);
      head.appendChild(script);
   }
}
