/* lrbc.js
   Liquid rounded box corners

   Requires:
     /ws/j/lib/on-x.js

   CC, Peter Asquith, 2006.

   This work is presented under a Creative Commons 2.5 Attribution License 
   See http://creativecommons.org/licenses/by/2.5/ for more information.
   Feel free to make use of the code if it works for you. I don't make any
   claims about fitness for purpose or any kind of warranty expressed or implied.
   This is all presented 'as is' but I hope you find it useful.
*/
addLoadEvent(liquidRoundedBoxCorners);

function liquidRoundedBoxCorners() {

  if(!document.getElementsByTagName) return false;
  
  // Get a list of div elements that have the class "rbcl"
  var l_lstCandidates = document.getElementsByTagName("div");
  for(var l_iIndex = 0; l_iIndex < l_lstCandidates.length; l_iIndex++) {
    if(l_lstCandidates[l_iIndex].className.indexOf("rbcl") > -1) {
      applyLiquidRoundedCorners(l_lstCandidates[l_iIndex]);
    }
  } 
  // Get a list of form elements that have the class "rbcl"
  l_lstCandidates = document.getElementsByTagName("form");
  for(var l_iIndex = 0; l_iIndex < l_lstCandidates.length; l_iIndex++) {
    if(l_lstCandidates[l_iIndex].className.indexOf("rbcl") > -1) {
      applyLiquidRoundedCorners(l_lstCandidates[l_iIndex]);
    }
  } 
  // Get a list of list elements that have the class "rbcl"
  l_lstCandidates = document.getElementsByTagName("a");
  for(var l_iIndex = 0; l_iIndex < l_lstCandidates.length; l_iIndex++) {
    if(l_lstCandidates[l_iIndex].className.indexOf("rbcl") > -1) {
      applyLiquidRoundedCorners(l_lstCandidates[l_iIndex]);
    }
  } 

  // Get a list of list elements that have the class "rbcl"
  l_lstCandidates = document.getElementsByTagName("ul");
  for(var l_iIndex = 0; l_iIndex < l_lstCandidates.length; l_iIndex++) {
    if(l_lstCandidates[l_iIndex].className.indexOf("rbcl") > -1) {
      applyLiquidRoundedCorners(l_lstCandidates[l_iIndex]);
    }
  } 
  
  // Get a list of table elements that have the class "rcnrs"
  l_lstCandidates = document.getElementsByTagName("table");
  for(var l_iIndex = 0; l_iIndex < l_lstCandidates.length; l_iIndex++) {
    if(l_lstCandidates[l_iIndex].className.indexOf("rbcl") > -1) {
      applyLiquidRoundedCorners(l_lstCandidates[l_iIndex]);
    }
  }   
  
} // liquidRoundedBoxCorners


function applyLiquidRoundedCorners(p_e) {
  var l_bIE = (navigator.userAgent.indexOf('MSIE') != -1)
  var l_eParent = p_e.parentNode;
  var l_iDisplayWidth = parseInt(p_e.offsetWidth);
  var l_iDisplayLeft = parseInt(p_e.offsetLeft);
  var l_strLinkClass = "";

  // Bail out if the element we're trying to round doesn't have a width
  if(l_iDisplayWidth == 0) return false;
  
  // See if there's a directive to only round the top or the bottom corners
  var l_bTop = (p_e.className.indexOf("rbcl-top") > -1);
  var l_bBtm = (p_e.className.indexOf("rbcl-btm") > -1);
  // If neither then both:
  if(!l_bTop && !l_bBtm) {
    l_bTop = true;
    l_bBtm = true;
  }  

  if(p_e.getAttribute("id") != "") {
    l_strLinkClass = " r-" + p_e.getAttribute("id");
  }
  
  // Create and set the widths of the corner elements
  var l_iSteps = 12;
  var l_iPosition = 0;
  
  var l_eTopHolder = document.createElement("div");
  var l_eBtmHolder = document.createElement("div");
  if(l_bTop) {
    l_eTopHolder.className = "top-holder";
    l_eTopHolder.style.height = l_iSteps + "px";
    l_eTopHolder.style.overflow = "hidden";
    if(p_e.firstChild) {
      p_e.insertBefore(l_eTopHolder, p_e.firstChild);    
    }
    else {
      p_e.appendChild(l_eTopHolder);
    }
  }
  if(l_bBtm) {
    l_eBtmHolder.className = "btm-holder";
    l_eBtmHolder.style.clear = "both";
    l_eBtmHolder.style.width = "100%";
    // psapsapsa - quick 'n' dirty hack to temporarily fix the IE btm-holder is one pixel to deep problem
    if(l_bIE) {
      l_eBtmHolder.style.height = (l_iSteps - 1) + "px";
    }
    else {
      l_eBtmHolder.style.height = l_iSteps + "px";
    }
    l_eBtmHolder.style.overflow = "hidden";
    p_e.appendChild(l_eBtmHolder);
  }

  if(l_bTop) {    
    for(var l_iIndex = (l_iSteps - 1); l_iIndex >= 0; l_iIndex--) {
  
      var l_iTopDeduction = l_iSteps - Math.floor(Math.sqrt((l_iSteps * l_iSteps) - (l_iIndex * l_iIndex)));
      var l_eTopLeft = document.createElement("span");
      var l_eTopRight = document.createElement("span");

      l_eTopHolder.appendChild(l_eTopLeft);
      l_eTopHolder.appendChild(l_eTopRight);
      
      l_eTopLeft.className = "r" + l_iIndex + "tl rnd rnd-left" + l_strLinkClass;
      l_eTopLeft.style.display = "block";
      l_eTopLeft.style.height = "1px";
      l_eTopLeft.style.fontSize = "1px";
      l_eTopLeft.style.width = l_iTopDeduction + "px";
      l_eTopLeft.style.overflow = "hidden";
      
      l_eTopRight.className = "r" + l_iIndex + "tr rnd rnd-right" + l_strLinkClass;
      l_eTopRight.style.display = "block";
      l_eTopRight.style.height = "1px";
      l_eTopRight.style.fontSize = "1px";
      l_eTopRight.style.width = l_iTopDeduction + "px";      
      l_eTopRight.style.overflow = "hidden";
      
      /*
.rnd_left {
  display: block;
  clear: left;
  float: left;
}
.rnd_right {
  display: block;
  float: right;
  clear: right;
}      
      */
      
    }
  }  

  if(l_bBtm) {
    for(var l_iIndex = 0; l_iIndex < l_iSteps; l_iIndex++) {
  
      var l_iBtmDeduction = l_iSteps - Math.floor(Math.sqrt((l_iSteps * l_iSteps) - (l_iIndex * l_iIndex)));
      var l_eBtmLeft = document.createElement("span");
      var l_eBtmRight = document.createElement("span");

      l_eBtmHolder.appendChild(l_eBtmLeft);
      l_eBtmHolder.appendChild(l_eBtmRight);
      
      l_eBtmLeft.className = "r" + l_iIndex + "bl rnd rnd-left" + l_strLinkClass;
      l_eBtmLeft.style.display = "block";
      l_eBtmLeft.style.height = "1px";
      l_eBtmLeft.style.fontSize = "1px";
      l_eBtmLeft.style.width = l_iBtmDeduction + "px";
      l_eBtmLeft.style.overflow = "hidden";

      l_eBtmRight.className = "r" + l_iIndex + "br rnd rnd-right" + l_strLinkClass;
      l_eBtmRight.style.display = "block";
      l_eBtmRight.style.height = "1px";
      l_eBtmRight.style.fontSize = "1px";
      l_eBtmRight.style.width = l_iBtmDeduction + "px";
      l_eBtmRight.style.overflow = "hidden";
    }
  }
  /* Override any padding in the containing box - this will interfere with the
     corners of the box
   */
  p_e.style.padding = "0 0 0 0";
} // applyLiquidRoundedCorners
