/**
  * Check.tc certificates script
  *
  * @copyright (c) Misha Yurasov 2009
  * @version 0.2
  */

// Format elements from stack
var __checktc__cert_all = function ()
{
  if (typeof(__checktc__ids) != 'undefined')
  {
    for (i = 0; i < __checktc__ids.length; i++)
    {
      __checktc__cert(__checktc__ids[i]);
    }
  }
}

// Format element
var __checktc__cert = function (cert_id)
{

  var more = '...';
  var cert = document.getElementById(cert_id);
  var cert_a = cert.firstChild;
  
  // disable selection and context menu
  cert.oncontextmenu = cert.onselectstart = cert.onmousedown = function () { return false; };
  
  // url
  var url = cert_a.href;
  cert.onclick = function () { document.location = url };
  cert.onmouseover = function () { window.status = url };
  cert.onmouseout = function () { window.status = '' };
  
  // make spaces not-braking
  cert_a.innerHTML = cert_a.innerHTML.replace(/ /g, '&nbsp;');
  
  if (cert.scrollWidth > cert.offsetWidth)
  {
    var txt0 = cert_a.data; 
    
    if (typeof txt0 == 'undefined')
    {
      txt0 = cert_a.innerHTML
      .replace(/&nbsp;/g,' ')
      .replace(/&quot;/g, '"')
      .replace(/&#039;/g, "'")
      .replace(/&lt;/g, "<")
      .replace(/&gt;/g, ">")
    }
    
    var fit = cert.offsetWidth / cert.scrollWidth;
    var txt1 = txt0.substr(0, txt0.length * fit);
    cert_a.innerHTML = (txt1 + more).replace(/ /g, '&nbsp;');
    
    if (cert.scrollWidth > cert.offsetWidth)
    {
      // shrink text
      
      while (cert.scrollWidth > cert.offsetWidth)
      {
        txt1 = txt0.substr(0, txt1.length - 1);
        cert_a.innerHTML = (txt1 + more).replace(/ /g, '&nbsp;');
      }
    }
    else
    {
      // increment text
      
      while (cert.scrollWidth <= cert.offsetWidth)
      {
        txt1 = txt0.substr(0, txt1.length + 1);
        cert_a.innerHTML = (txt1 + more).replace(/ /g, '&nbsp;');
      }
      
      cert_a.innerHTML = (txt0.substr(0, txt1.length - 1) + more).replace(/ /g, '&nbsp;');
    }
  }
}

__checktc__cert_all();
