/* Functions shared between todd and webforms. Should not have dependencies on any todd/webforms libraries (including internals.js/api.js) */


/********************************************************************
   XMLHttpRequest
*/
/* Create a request object (aka ajax)
   ADDME: Probably needs to deal with multiple versions of the MSXML components
   */
function toddCreateRequestObject()
{
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer")
  {
    var ro = new ActiveXObject("Microsoft.XMLHTTP");
    //var ro = new ActiveXObject("MSXML2.XMLHTTP");
  }
  else
  {
    var ro = new XMLHttpRequest();
  }
  return ro;
}

// Prevent event from bubbling up
function toddDontPropagateEvent(theEvent)
{
  if(theEvent.stopPropagation)
  {
    if (theEvent.cancelable)
      theEvent.preventDefault();
    theEvent.stopPropagation();
  }
  else
  {
    theEvent.returnValue = false;
    theEvent.cancelBubble = true;
    if(theEvent.keyCode)
      theEvent.keyCode = 0;
  }
  return false;
}


/** @short returns the size of the visual area of window
    @return object with width and height
*/
function toddGetViewPortDimensions()
{
  // all except Explorer
  if (window.innerHeight)
    return { width:  window.innerWidth
           , height: window.innerHeight
           };

  // Explorer 6 Strict Mode
  if (document.documentElement && document.documentElement.clientHeight)
    return { width:  document.documentElement.clientWidth
           , height: document.documentElement.clientHeight
           };

  return { width:  document.body.clientWidth
         , height: document.body.clientHeight
         };
}


/** @short returns the full size of the document
    @return size of the document
*/
function toddGetPageDimensions()
{
  var htmlwidth = document.documentElement.scrollWidth;
  var htmlheight = document.documentElement.scrollHeight;

  // IE6-QuirksMode and SF (all versions? tested with 3.2.1) give page height in <BODY>
  // All other browsers give the size in documentElement (<HTML>)
  var bodywidth = document.body.scrollWidth;
  var bodyheight = document.body.scrollHeight;

  // return largest values
  return { width:  htmlwidth  > bodywidth  ? htmlwidth  : bodywidth
         , height: htmlheight > bodyheight ? htmlheight : bodyheight
         };
}


/** @short returns the absolute position an element in <body> would need to have the same position as the given element
    @param obj reference to element
    @return {left:X,top:Y}
*/
function toddGetBodyPos(obj)
{
  // getBoundingClientRect is supported by IE5+, FF3+, OP9.50+, SF4+
  if (obj.getBoundingClientRect)
  {
    var bounds = obj.getBoundingClientRect();

    var xscroll = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
    var yscroll = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;

    return { left: bounds.left + xscroll - document.documentElement.clientLeft - document.body.clientLeft
           , top:  bounds.top + yscroll - document.documentElement.clientTop - document.body.clientTop
           };
  }


  // getBoxObjectFor works in FF2, deprecated in FF3/3.5, removed in FF3.6
  // Only use it for Firefox, since other Gecko based browsers may not correctly implement getBoxObjectFor
  if (toddUserAgentIsFirefox && document.getBoxObjectFor)
  {
    var box   = document.getBoxObjectFor(obj);
    var vpBox = document.getBoxObjectFor(document.documentElement);

    return { left: box.screenX - vpBox.screenX
           , top:  box.screenY - vpBox.screenY
           };
  }


  // the oldskool method of getting positions
  // Safari <4 & Opera 9.25 will use this method
  var elemx = obj.offsetLeft;
  var elemy = obj.offsetTop;
  var parent = obj.offsetParent;

  while (parent)
  {
    // add offset to content part of our offsetParent
    elemx += parent.offsetLeft;
    elemy += parent.offsetTop;

    // Quirks:
    // - Opera already adds clientTop/Left to offsetTop/Left
    // - Firefox <3 (Gecko <1.9) doesn't support clientTop/Left
    if (!toddClientTopIsAddedToOffsetTop && typeof parent.clientLeft != 'undefined')
    {
      // also add the edge size of the offsetParent
      elemx += parent.clientLeft;
      elemy += parent.clientTop;
    }

    // subtract the amount the offsetParent is scrolled, except if it's the scrollposition of the page
    if (parent != document.body && parent != document.documentElement)
    {
      elemx -= parent.scrollLeft;
      elemy -= parent.scrollTop;
    }

    parent = parent.offsetParent;
  }

  return { left: elemx
         , top:  elemy
         };
}

// deprecated
function toddGetObjectPosX(obj)
{
  return toddGetBodyPos(obj).left;
}

// deprecated
function toddGetObjectPosY(obj)
{
  return toddGetBodyPos(obj).top;
}

// deprecated
function toddGetObjectScreenX(obj)
{
  return toddGetBodyPos(obj).left;
}

// deprecated
function toddGetObjectScreenY(obj)
{
  return toddGetBodyPos(obj).top;
}

if(typeof toddMarkScriptComplete != 'undefined')
  toddMarkScriptComplete("base.js");