/* CONTENTS:
   ---------
   1. position functions
   2. string
   3. DOM
   4. Math
   5. time Interval
   6. css
*/

// get object that can be queried for client area dimensions (varies by DOCTYPE declaration)
function UTILS_GetClientViewObject (win)
{
  if (typeof(win) === typeof(undefined))
    win = window;
    
  //if Doctype is XHTML 1.0 Transitional then document.documentElement.client* = value and document.body.client* = 0
  //if Doctype is missing then document.documentElement.client* = 0 and document.body.client* = value
  var oClientView = win.document.documentElement;
  if (!(oClientView.clientHeight > 0 || oClientView.clientWidth > 0))
    oClientView = win.document.body;
    
  return oClientView;
}

function COMMON_GetBounds (oEl, oElParent)
//       ~~~~~~~~~~~~~~~~
{
  var rc = new Object();
  rc.left = 0;
  rc.top = 0;
  rc.width = oEl.offsetWidth;
  rc.height = oEl.offsetHeight;
  while(1)
  {
    rc.left += oEl.offsetLeft;    
    rc.top  += oEl.offsetTop;        
    if (oEl == document.body)
      break;
    oEl = oEl.offsetParent;    
    if (oEl == oElParent)
      break;
  }   
  rc.right  = rc.left + rc.width;
  rc.bottom = rc.top + rc.height;
  return rc;
}
//--------------------------------------------------

function COMMON_IsPointInElement (oEl, offsetX, offsetY)
//       ~~~~~~~~~~~~~~~~~~~~~~~
{
  var rc = COMMON_GetBounds(oEl);
  return (offsetX >= rc.left && offsetX < rc.right &&
          offsetY >= rc.top  && offsetY < rc.bottom  )
}
//--------------------------------------------------

function COMMON_IsClientPointInElement (oEl, clientX, clientY)
//       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
  var rc = oEl.getBoundingClientRect();
  return (clientX >= rc.left && clientX < rc.right &&
          clientY >= rc.top  && clientY < rc.bottom  )
}
//--------------------------------------------------

function COMMON_MouseXToAbsoluteLeft (nMouseX)
//       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
  var bScrollBar;
  if (document.body.currentStyle.direction == "rtl")
  {
    var sOverflow = document.body.style.overflow;

    if (sOverflow == "visible" || sOverflow == "hidden")
      bScrollBar = false;
    else  
    if (sOverflow == "" || sOverflow == "scroll")
      bScrollBar = true;
    else
    if (document.body.scrollHeight > document.body.clientHeight)
      bScrollBar = true;
    else  
      bScrollBar = false;
    if (bScrollBar)
      nMouseX -= 18;

    nMouseX += (document.body.scrollLeft - (document.body.scrollWidth - document.body.clientWidth));
    
  }
  return nMouseX;

}
//--------------------------------------------------

function COMMON_IsSpecificEvent (oEvent)
//       ~~~~~~~~~~~~~~~~~~~~~~
{
  /* returns true if any element inside document.body is getting the given event.
     parameter is the event object
 */
  var el = oEvent.srcElement;
  while (true)
  {
    if (el == document.body)
      return false;
    if (eval("el.on" + oEvent.type))
      return true;
    el = el.parentNode;
  }
}
//--------------------------------------------------

function COMMON_TrimString (str)
//       ~~~~~~~~~~~~~~~~~
{
  // trim leading, trailing and multiple blanks
  var result = str;
  while (result.search("  ") > -1) {
    result = result.replace("  ", " ");
  }
  if (result.length > 0 && result.charAt(0) == " ")
    result = result.substr(1);
  if (result.length > 0 && result.charAt(result.length - 1) == " ")
    result = result.substr(0, result.length - 1);

  return result;
}
//----------------------------------------------------------------

function COMMON_GetAncestorByTag (el, sTagName)
//       ~~~~~~~~~~~~~~~~~~~~~~~
{
  for (; el != document; el = el.parentNode)
  {
    if (el.tagName.toUpperCase() == sTagName.toUpperCase())
      return el;
  }
  return null;
}
//----------------------------------------------------------------


function COMMON_hypotenuse (n1, n2)
//       ~~~~~~~~~~~~~~~~~
{
  return Math.sqrt(n1*n1 + n2 * n2);
}
//--------------------------------------------------
function GetWord(OffsetY)
{
  var oWord;
  for(i = 0; i < OffsetY; i++)
  {
    oWord = TEXTRANGE_GetWordFromPoint(event.clientX, event.clientY + DragText_CopyCurrDragElement.offsetHeight/2 + i);
    if (oWord != null && oWord.text != null && oWord.text != 'undefined' && oWord.text.length > 1)
    {
      
      //if (oWord.text.charAt(oWord.text.length-1) == "_")
      //{
      //  return oWord;      
      //}
      //return oWord;    
    }
    oWord = TEXTRANGE_GetWordFromPoint(event.clientX, event.clientY - DragText_CopyCurrDragElement.offsetHeight/2 - i);
    if (oWord != null && oWord.text != null && oWord.text != 'undefined' && oWord.text.length > 1)
    {
     
//      if (oWord.text.charAt(oWord.text.length-1) == "_")
//      {
//        return oWord;
//      }
      return oWord;
    }
  }
  // We didn't get to the "_" word
  return "";
}
//--------------------------------------------------

// replace window.setInterval. use a single interval at a time.

var COMMON_nDelay;
var COMMON_IntervalCallback;
var COMMON_nNextOccurrence;
var COMMON_dtStart;
var COMMON_Intervalhandle;

function COMMON_SetInterval(vCallback, nDelay)
{
  COMMON_nDelay = nDelay;
  COMMON_IntervalCallback = (typeof(vCallback) == "function" ? vCallback : new Function(vCallback));
  COMMON_nNextOccurrence = 1;
  COMMON_dtStart = new Date();
  COMMON_Intervalhandle = window.setTimeout(COMMON_OnTimeout, COMMON_nDelay);
}

// private function
function COMMON_OnTimeout() 
{
  COMMON_nNextOccurrence++;
  var nTarget = COMMON_nNextOccurrence * COMMON_nDelay;
  var nElapsed = (new Date()).getTime() - COMMON_dtStart.getTime();
  COMMON_Intervalhandle = window.setTimeout(COMMON_OnTimeout, nTarget - nElapsed);

  COMMON_IntervalCallback();
}

function COMMON_ClearInterval()
{
  window.clearTimeout(COMMON_Intervalhandle);
}

// CSS
//--------
// tools to add and remove a class name in className property
function CSS_AddClass(elem, sClassName) 
{
  if (!elem.className)
    elem.className = "";
  var sClassList = " " + elem.className + " ";
  if (sClassList.toLowerCase().indexOf(" " + sClassName.toLowerCase() + " ") < 0)
  {
    sClassList += sClassName;
    sClassList = sClassList.substr(1); // drop added leading blank-space
    elem.className = sClassList;
  }
}

function CSS_RemoveClass(elem, sClassName) 
{
  var sClassList = " " + elem.className + " ";
  var sNewClassList = "";
  var nPos = sClassList.toLowerCase().indexOf(" " + sClassName.toLowerCase() + " ");
  if (nPos >= 0)
  {
    if (nPos > 0)
      sNewClassList = sClassList.substr(1, nPos-1);
    sNewClassList += sClassList.substr(nPos + sClassName.length + 1);
    sNewClassList = sNewClassList.substr(0, sNewClassList.length - 1); // drop added trailing blank-space
    elem.className = sNewClassList;
  }  
}
