var _xbase_, _ybase_;
var _col_ = new Array ( 3 );
var _sourceheight_;
var _pagenow_ = 0;
var _pagecount_;
var _curcols_;
var _conheight_;
var _colspc_ = 10;
var _cfs_ = 1;
var _maxcfs_ = 10;
var _fontsizes_     = [  10,     11,     12,     13,     15,     20,     25,     30,     35,     40,     45];
var _lineheights_  = [  12,     13,     15,     16,     19,     24,     29,     36,     43,     48,     54];

//-------------------------------------------------------
function _GetBrowserType_ ( )
{
  if ( navigator.userAgent.indexOf ( "MSIE" ) != -1 )
    if ( navigator.userAgent.indexOf ( "Opera" ) != -1 )
      return "opera";
    else
      return "msie";
  else
    if ( navigator.appName == "Netscape" )
      if ( navigator.userAgent.indexOf ( "Netscape" ) != -1 )
        return "netscape";
      else
        return "mozilla";
    else
      return "unknown";
}
//---------------------------------------------
function _GetElCoords_ ( _elem_ )
{
  _xbase_ = 0;
  _ybase_ = 0;
  do
    {
      _xbase_ += _elem_.offsetLeft;
      _ybase_ += _elem_.offsetTop;
      _elem_ = _elem_.offsetParent;
    }
  while ( _elem_ != null );
}
//-------------------------------------------------------
function _ShowColumns_ ( _colcount_ )
{
  var _i_;
  var _conwidth_;
  var _colwidth_;
  var _contentx_;
  var _contenty_;
  var _h1_;

  if ( _colcount_ == 0 )
    _colcount_ = _curcols_;
  else
    _curcols_ = _colcount_;
    
  _conwidth_  = parseInt ( document.getElementById ( "_widthinput_" ).value );
  _conheight_ = parseInt ( document.getElementById ( "_heightinput_" ).value );
    _conheight_ = _conheight_ - _conheight_ % _lineheights_ [_cfs_];
  _pagebase_ = _conheight_ * _pagenow_ * _colcount_;

  _baseelem_ = document.getElementById ( "_baseel_" );
  _GetElCoords_ ( _baseelem_ );
  _baseelem_.style.width = _conwidth_ + 2;
  _baseelem_.style.height = _conheight_ + 3;
  _contentx_ = _xbase_ + 1;
  _contenty_ = _ybase_ + 1;
  
  _colwidth_ = _conwidth_ / _colcount_;
  if ( _colcount_ > 1 )
    _colwidth_ -= ( _colcount_ - 1 ) * _colspc_ / _colcount_;

  _overlapper_ = document.getElementById ( "_overlapel_" );
  _overlapper_.style.display = "block";
  _overlapper_.style.left = _xbase_;
  _overlapper_.style.top = 0;
  _overlapper_.style.width = _conwidth_;
  _overlapper_.style.height = _ybase_;
  _overlapper_.style.overflow = "hidden";

  _overlapper_ = document.getElementById ( "_overlapel2_" );
  _overlapper_.style.display = "block";
  _overlapper_.style.left = _xbase_;
  _overlapper_.style.top = 0;
  _overlapper_.style.width = _conwidth_;
  _overlapper_.style.height = _ybase_ - 100;
  _overlapper_.style.overflow = "hidden";

  _source_ = document.getElementById ( "_content_" );  
  _h1_ = 1;
  for ( _i_ = 0; _i_ < 3; _i_++ )
    {
      if ( !_col_ [_i_] )
        {
          _col_ [_i_] = _source_.cloneNode ( true );
          document.body.appendChild ( _col_ [_i_] );
          _col_ [_i_].style.position = "absolute";
          _col_ [_i_].style.overflow = "hidden";
          _col_ [_i_].style.zIndex = 0;
        }
      if ( _i_ < _colcount_ )
        {
          _col_ [_i_].style.width = _colwidth_;
          if ( _i_ == 0 )
            {
              _col_ [_i_].style.visibility = "hidden";
              _col_ [_i_].style.height = "auto";
              _sourceheight_ = _col_ [_i_].clientHeight ? _col_ [_i_].clientHeight : _col_ [_i_].offsetHeight;
            }
          _col_ [_i_].style.height = _conheight_ + _h1_ + _pagebase_;
          _col_ [_i_].style.left = _contentx_ + _i_ * ( _colwidth_ + _colspc_ );
          _col_ [_i_].style.top = _contenty_ - _h1_ - _pagebase_;
          _col_ [_i_].style.display = "block";
          _col_ [_i_].style.visibility = "visible";
          _h1_ = _h1_ + _conheight_;
        }
      else
        _col_ [_i_].style.display = "none";
    }

  _pagecount_ = Math.ceil ( Math.ceil ( _sourceheight_ / _conheight_ ) / _colcount_ );
    
  if ( _pagenow_ < _pagecount_ - 1 )
    document.getElementById ( "_PgDn_" ).disabled = false;
    
  _pagenumstr_ = "Página " + ( _pagenow_ + 1 ) + " de " + _pagecount_;

  document.getElementById ( "_pagenumel_" ).innerHTML = _pagenumstr_;
}
//-------------------------------------------------------
function _PageUp_ ( )
{
  if ( _pagenow_ == 0 )
    return;
  
  _pagenow_--;
    
  document.getElementById ( "_PgDn_" ).disabled = false;
    
  if ( _pagenow_ == 0 )
    document.getElementById ( "_PgUp_" ).disabled = true;

  _ShowColumns_ ( 0 );
}
//-------------------------------------------------------
function _PageDown_ ( )
{
  if ( _pagenow_ >= _pagecount_ - 1 )
    return;
  
  _pagenow_++;
    
  if ( _pagenow_ == _pagecount_ - 1 )
    document.getElementById ( "_PgDn_" ).disabled = true;
    
  document.getElementById ( "_PgUp_" ).disabled = false;

  _ShowColumns_ ( 0 );
}
//-------------------------------------------------------
function _EnlargeFont_ ( )
{
  if ( _cfs_ == _maxcfs_ - 1 )
    return;

  _cfs_++;    
  for ( _i_ = 0; _i_ < 3; _i_++ )
    if ( _col_ [_i_] )
      _EnlargeInDepth_ ( _col_ [_i_] );
  _ShowColumns_ ( 0 );
  _enlargefirst_ = false;
}
//-------------------------------------------------------
function _DecreaseFont_ ( )
{
  if ( _cfs_ == 0 )
    return;
  
  _cfs_--;  
  for ( _i_ = 0; _i_ < 3; _i_++ )
    if ( _col_ [_i_] )
      _DecreaseInDepth_ ( _col_ [_i_] );
  _ShowColumns_ ( 0 );

  if ( _pagenow_ >= _pagecount_ )
    {
      _pagenow_ = _pagecount_ - 1;
      _ShowColumns_ ( 0 );
    }

  _enlargefirst_ = false;
}
//-------------------------------------------------------
function _EnlargeInDepth_ ( _elem_ )
{
  var _stack_ = Array ( 100 );
  var _stacki_ = Array ( 100 );
  var _sp_;
  
  _stack_ [0] = _elem_;
  _stacki_ [0] = -1;
  _sp_ = 0;
  while ( _sp_ >= 0 )
    {
      _stacki_ [_sp_]++;
      _stack_ [_sp_].style.fontSize = _fontsizes_ [_cfs_] + "px";
      _stack_ [_sp_].style.lineHeight = _lineheights_ [_cfs_] + "px";
        
      if ( _sp_ < 99 )
        if ( _stacki_ [_sp_] < _stack_ [_sp_].childNodes.length )
          {
            if ( _stack_ [_sp_].childNodes [_stacki_ [_sp_]].nodeType == 1 )
              {
                _sp_++;
                _stacki_ [_sp_] = -1;
                _stack_ [_sp_] = _stack_ [_sp_ - 1].childNodes [_stacki_ [_sp_ - 1]];
              }
          }
        else
          _sp_--;
      else
        _sp_--;
   }
}
//-------------------------------------------------------
function _DecreaseInDepth_ ( _elem_ )
{
  var _stack_ = Array ( 100 );
  var _stacki_ = Array ( 100 );
  var _sp_;
  
  _stack_ [0] = _elem_;
  _stacki_ [0] = -1;
  _sp_ = 0;
  while ( _sp_ >= 0 )
    {
      _stacki_ [_sp_]++;
      _stack_ [_sp_].style.fontSize = _fontsizes_ [_cfs_] + "px";
      _stack_ [_sp_].style.lineHeight = _lineheights_ [_cfs_] + "px";
        
      if ( _sp_ < 99 )
        if ( _stacki_ [_sp_] < _stack_ [_sp_].childNodes.length )
          {
            if ( _stack_ [_sp_].childNodes [_stacki_ [_sp_]].nodeType == 1 )
              {
                _sp_++;
                _stacki_ [_sp_] = -1;
                _stack_ [_sp_] = _stack_ [_sp_ - 1].childNodes [_stacki_ [_sp_ - 1]];
              }
          }
        else
          _sp_--;
      else
        _sp_--;
   }
}
