/****************************************************************
 * CFontSize.css
 * description:
 *    defines the class "CFontSize", which provides functions to
 *      - increase the font size
 *      - decrease the font size
 * Hoang Viet Do
 * indenso.de
 * created: March 19th, 2008
 * last modified: March 19th, 2008
 */

function CFontSize() {
  /* Constants */  
  var c_nMin = 1;                 /* decrease()'s minimum */
  var c_nMax = Number.MAX_VALUE;  /* increase()'s maximum */
  var c_strSize        = "fontsize";         /* lowercase only, string used to save the value of m_nSize        (member variable) */
  var c_strSizeDefault = "fontsize_default";  /* lowercase only, string used to save the value of m_nSizeDefault (member variable) */
  /* Member variables: it will be initialize in this.init(), which you have to call explicitly! */
  var m_strTarget    = "";
  var m_nSize        = 0;
  var m_nSizeDefault = 0;
  
  /* init() is the constructor, which you have to call explicitly. It initialize the member variables.
     First it looks for the fontsize of the given div. In case it is not specified it will look for the fontsize
     of the body. If this fail as well it will init m_nSize to 11px.
     We can not define a constructor since the object might have been created before the div is loaded
  */
  this.init = function(pTarget, min, max) {
    m_strTarget = pTarget;
    if(!document.getElementById(m_strTarget)) {
      alert("Debug: div#"+m_strTarget+" existiert nicht!");
    }
    
    /* load settings from cookie */
    if(document.cookie) {
      buf = document.cookie;

      // Suche nach Schriftgröße und Reset Schriftgröße
      while((m_nSize == 0 || m_nSizeDefault == 0) && buf != "") {
        subbuf = buf.substring(0,buf.search('='));
        if(subbuf.toLowerCase() == c_strSize) {
          subbuf  = buf.substring(buf.search('=')+1, buf.search(';') > 0 ? buf.search(';') : buf.length);
          m_nSize = parseInt(subbuf);
        }
        if(subbuf.toLowerCase() == c_strSizeDefault) {
          subbuf  = buf.substring(buf.search('=')+1, buf.search(';') > 0 ? buf.search(';') : buf.length);
          m_nSizeDefault = parseInt(subbuf);
        }
        
        if(buf.search(';') > 0)
          buf = buf.substring(buf.search(';')+2, buf.length); /*+2: ';' und ' ' */
        else
          buf = "";
      }
    }
    
    // fail to load settings from cookies
    if(!m_nSize || !m_nSizeDefault) {
      /* get current fontsize */
      m_nSize   = parseInt(document.getElementById(m_strTarget).style.fontSize);
      if(!m_nSize)
        m_nSize = parseInt(document.body.style.fontSize ? document.body.style.fontSize : "11px");
      m_nSizeDefault = m_nSize;
      document.cookie = document.cookie = c_strSizeDefault+"="+m_nSizeDefault+";";
    }
    else
      document.getElementById(m_strTarget).style.fontSize = m_nSize+"px";

    /* set min and max */
    if(min > 1)
      c_nMin = min;
    if(max >= min)
      c_nMax = max;
  }
  
  /* decrease(): decrease the font size. (min: c_nMin)*/
  this.decrease = function() {
    if(m_nSize > c_nMin) {
      m_nSize--;
      document.getElementById(m_strTarget).style.fontSize = m_nSize+"px";
      document.cookie = c_strSize+"="+m_nSize+";";
    }
  }
  /* reset(): set the font size back to m_nSizeDefault */
  this.reset = function() {
    m_nSize = m_nSizeDefault;
    document.getElementById(m_strTarget).style.fontSize = m_nSize+"px";
    document.cookie = c_strSize+"="+m_nSize+";";
  }
  /* increase(): increase the font size (max: c_nMax)*/
  this.increase = function() {
    if(m_nSize < c_nMax) {
      m_nSize++;
      document.getElementById(m_strTarget).style.fontSize = m_nSize+"px";
      document.cookie = c_strSize+"="+m_nSize+";";
    }
  }
}
