////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ui.js
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Define namespace.
UI = {};

// Initialize.
switch(Browser.agent)
{
  case Browser.InternetExplorer:
/*
    UI.getPos = function(a)
    {
      var pos = { x:0, y:0 };
      if (a.offsetParent)
      {
        while (1)
        {
          pos.x += a.offsetLeft;
          pos.y += a.offsetTop;
          if (!a.offsetParent)
          {
            break;
          }
          a = a.offsetParent;
        }
      }
      else if (a.x)
      {
        pos.x += a.x;
        pos.y += a.y;
      }
      return pos;
    }
    break;
*/
    UI.getPos = function(a)
    {
      var pos = { x:0, y:0 };
      if (a.self || a.nodeType === 9)
      {
        // Return default;
      }
      else
      {
        var d = a.getClientRects();
        if (d && d.length)
        {
          var e = a.ownerDocument.parentWindow;
          var g = e.screenLeft - top.screenLeft - top.document.documentElement.scrollLeft + 2;
          var h = e.screenTop - top.screenTop - top.document.documentElement.scrollTop + 2;
          var c = e.frameElement || null;
          if (c)
          {
            var b = c.currentStyle; 
            g += (c.frameBorder || 1) * 2 + (parseInt(b.paddingLeft) || 0) + (parseInt(b.borderLeftWidth) || 0) - a.ownerDocument.documentElement.scrollLeft;
            h += (c.frameBorder || 1) * 2 + (parseInt(b.paddingTop) || 0) + (parseInt(b.borderTopWidth) || 0) - a.ownerDocument.documentElement.scrollTop;
          }
          var f = d[0];
          pos = { x: f.left - g, y: f.top - h };
        }
      }
      return pos;
    }
    break;
  case Browser.Safari:
    UI.getPos = function(c)
    {
      var pos = { x:0, y:0 };
      if (c.window && c.window === c || c.nodeType === 9)
      {
        // Return default;
      }
      else
      {
        var g = 0, h = 0, j = null, f = null, b;
        for (var a = c; a; j = a, (f = b, a = a.offsetParent))
        {
          b = $getCurrentStyle(a);
          var e = a.tagName; 
          if ((a.offsetLeft || a.offsetTop) && (e !== "BODY" || (!f || f.position !== "absolute")))
          {
            g += a.offsetLeft;
            h += a.offsetTop;
          }
        }
        b = $getCurrentStyle(c);
        var d = b ? b.position : null, k = d && d !== "static"; 
        if (!d || d !== "absolute")
        {
          for (var a = c.parentNode; a; a = a.parentNode)
          {
            e = a.tagName; 
            if (e !== "BODY" && e !== "HTML" && (a.scrollLeft || a.scrollTop))
            {
              g -= a.scrollLeft || 0;
              h -= a.scrollTop || 0;
            }
            b = Sys.UI.DomElement._getCurrentStyle(a);
            var i = b ? b.position : null;
            if (i && i === "absolute")
            {
              break;
            }
          }
        }
        pos = { x: g, y: h };
      }
      return pos;
    }
    break; 
  case Browser.Opera:
    UI.getPos = function(b)
    {
      var pos = { x:0, y:0 };
      if (b.window && b.window === b || b.nodeType === 9)
      {
        // Return default;
      }
      else
      {
        var d = 0, e = 0, i = null;
        for (var a = b; a; i = a, a = a.offsetParent)
        {
          var f = a.tagName;
          d += a.offsetLeft || 0;
          e += a.offsetTop || 0;
        }
        var g = b.style.position, c = g && g !== "static";
        for(var a = b.parentNode; a; a = a.parentNode)
        {
          f = a.tagName;
          if (f !== "BODY" && f !== "HTML" && (a.scrollLeft || a.scrollTop) && (c && (a.style.overflow === "scroll" || a.style.overflow === "auto")))
          {
            d -= a.scrollLeft || 0;
            e -= a.scrollTop || 0;
          }
          var h = a && a.style ? a.style.position : null;
          c = c || h && h !== "static";
        }
        pos = { x: d, y: e };
      }
      return pos;
    }
    break;
  default:
    UI.getPos = function(d)
    {
      var pos = { x:0, y:0 };
      if (d.window && d.window === d || d.nodeType === 9)
      {
        // Return default;
      }
      else
      {
        var e = 0, f = 0, i = null, h = null, b = null;
        for (var a = d; a; i = a, (h = b, a = a.offsetParent))
        {
          var c = a.tagName;
          b = $getCurrentStyle(a); 
          if ((a.offsetLeft || a.offsetTop) &&!(c === "BODY" && (!h || h.position !== "absolute")))
          {
            e += a.offsetLeft; 
            f += a.offsetTop;
          }
          if (i !== null && b)
          {
            if (c !== "TABLE" && c !== "TD" && c !== "HTML")
            {
              e += parseInt(b.borderLeftWidth) || 0;
              f += parseInt(b.borderTopWidth) || 0;
            }
            if (c === "TABLE" && (b.position === "relative" || b.position === "absolute"))
            {
              e += parseInt(b.marginLeft) || 0;
              f += parseInt(b.marginTop) || 0;
            }
          }
        }
        b = $getCurrentStyle(d);
        var g = b ? b.position : null, j = g && g !== "static";
        if (!g || g !== "absolute")
        {
          for (var a = d.parentNode; a; a = a.parentNode)
          {
            c = a.tagName; 
            if (c !== "BODY" && c !== "HTML" && (a.scrollLeft || a.scrollTop))
            {
              e -= a.scrollLeft || 0; 
              f -= a.scrollTop || 0; 
              b = Sys.UI.DomElement._getCurrentStyle(a); 
              e += parseInt(b.borderLeftWidth) || 0; 
              f += parseInt(b.borderTopWidth) || 0;
            }
          }
        }
        pos = { x: e, y: f };
      }
      return pos;
    }
    break;
};
UI.hasScrollBars = function(e)
{
  return ((e.scrollHeight > e.clientHeight) || (e.scrollWidth > e.clientWidth));
}
UI.showPopup = function(refElement, popupId, show)
{
  var popupElement = $getElement(popupId);
  if (popupElement)
  {
    if (!refElement)
    {
      refElement = popupElement.parentElement;
    }
    var pos = UI.getPos(refElement);
    if (pos)
    {
      popupElement.style.left = pos.x;
      popupElement.style.top = pos.y + 30;
    }
    popupElement.style.visibility = (show ? 'visible' : 'hidden');
  }
};
UI.toggleDisplay = function(divId, textElement, showText, hideText)
{
  var divElement = $getElement(divId);
  if (divElement)
  {
    var show = ('none' == divElement.style.display);
    divElement.style.display = (show ? 'block' : 'none');
  }
  if (textElement)
  {
    textElement.innerHTML = (show ? hideText : showText);
  }
};
UI.attachCopyCode = function(cw, pr, pos, ccId)
{
  var cc = $getElement(ccId);
  if (!cc)
  {
    cc = $createElement('div');
    cc.id = ccId;
    cc.className = 'copycode';
    cc.innerHTML = '&nbsp;';
    cc.title = 'Copy Code Snippet';
    cc.onclick = function()
    {
      $copyToClipboard(pr);
    }
    cc.onmouseover = function()
    {
      with (cc.style)
      {
        backgroundColor = '#f4f4f4';
        borderColor = 'gray';
      }
    }
    cc.onmouseout = function()
    {
      with (cc.style)
      {
        backgroundColor = borderColor = 'white';
      }
    }
    $addElement(cw, cc);
  }
  if (pos)
  {
    cc.style.left = (pos.x + ((Browser.agent == Browser.InternetExplorer)
      ? (cw.offsetWidth - 3) : cw.offsetWidth) - cc.clientWidth - 4) + 'px';
    cc.style.top = (pos.y - ((Browser.agent == Browser.InternetExplorer)
      ? cc.offsetHeight : cc.clientHeight) - 6) + 'px';
  }
  if (cc.style.visibility !== 'visible')
  {
    cc.style.visibility = 'visible';
  }
};
UI.attachCodePage = function(cw, pr, pos, cpId)
{
  var cp = $getElement(cpId);
  if (!cp)
  {
    cp = $createElement('div');
    cp.id = cpId;
    cp.className = 'codepage';
    cp.innerHTML = '&nbsp;';
    cp.title = 'Show Full Code Snippet (new window)';
    cp.onclick = function()
    {
      var wnd = $openWindow('');
      if (wnd)
      {
        with (wnd.document)
        {
          open();
          write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
          write('<html xmlns="http://www.w3.org/1999/xhtml">');
          write('<head>');
          write('<title>Leo Vildosola\'s Blog - Code Snippet</title>');
          write('<link href="http://blogs.incyclesoftware.com/styles/main.css" rel="stylesheet" type="text/css"/>');
          write('</head>');
          write('<body>');
          write('<pre class="csharpcode">');
          write(pr.innerHTML);
          write('</pre>');
          write('</body>');
          write('</html>');
          close();
        }
      }
    }
    cp.onmouseover = function()
    {
      with (cp.style)
      {
        backgroundColor = '#f4f4f4';
        borderColor = 'gray';
      }
    }
    cp.onmouseout = function()
    {
      with (cp.style)
      {
        backgroundColor = borderColor = 'white';
      }
    }
    $addElement(cw, cp);
  }
  if (pos)
  {
    cp.style.left = (pos.x + ((Browser.agent == Browser.InternetExplorer)
      ? (cw.offsetWidth - 3) : cw.offsetWidth) - cp.clientWidth - 24) + 'px';
    cp.style.top = (pos.y - ((Browser.agent == Browser.InternetExplorer)
      ? cp.offsetHeight : cp.clientHeight) - 6) + 'px';
  }
  if (cp.style.visibility !== 'visible')
  {
    cp.style.visibility = 'visible';
  }
};
UI.attachCodeBar = function(cw)
{
  if (!cw.id)
  {
    throw 'Code wrapper element must have an identifier (ID) attribute!';
  }
  
  var pr = cw.getElementsByTagName('pre')[0];
  if (!pr)
  {
    throw 'Code must have a PRE element!';
  }

  var pos = UI.getPos(pr);
  UI.attachCopyCode(cw, pr, pos, cw.id + '$CopyCode1');
  if (UI.hasScrollBars(cw))
  {
  	UI.attachCodePage(cw, pr, pos, cw.id + '$CodePage1');
  }
};
UI.attachCodeBarByClassName = function(cn)
{
  var classElements = $getElementsByClassName(cn);
  if (classElements)
  {
    for (var i = 0; i < classElements.length; i++)
    {
      var classElement = classElements[i];
      if (!classElement.id)
      {
        classElement.id = 'ccw' + i;
      }
      UI.attachCodeBar(classElement);
    }
  }
};
UI.reposSearchBar = function(refElementId, searchBarId, topOffset, rightOffset)
{
  if (!refElementId)
  {
    refElementId = 'header-wrapper';
  }
  if (!searchBarId)
  {
    searchBarId = 'searchbar';
  }
  if (!topOffset)
  {
    topOffset = 12;
  }
  if (!rightOffset)
  {
    rightOffset = 54;
  }

  var refElement = $getElement(refElementId);
  var searchBar = $getElement(searchBarId);
  var pos = UI.getPos(refElement);
  if (pos)
  {
    searchBar.style.left = (pos.x + ((Browser.agent == Browser.InternetExplorer)
      ? (refElement.offsetWidth - 3) : refElement.offsetWidth) - searchBar.clientWidth - rightOffset) + 'px';
    searchBar.style.top = (pos.y + topOffset) + 'px';
  }
  if (searchBar.style.visibility !== 'visible')
  {
    searchBar.style.visibility = 'visible';
  }
};