// --- global variables -----------------------------------------------------

var vFontSizeGrade = null;


// --- internal use functions -----------------------------------------------

function getCookie (sName)
  {
    var aCookie = document.cookie.split ('; ');
    for (var i = 0; i < aCookie.length; ++i)
      {
        var aCrumb = aCookie [i].split ('=');
        if (sName == aCrumb [0])
          {
            return unescape (aCrumb [1]);
          }
      }
    return null;
  } // getCookie


function setCookie (sName, sValue, iMode)
  {
    var expDate = new Date ();
    if (iMode == 1)
        {
          expDate.setDate (expDate.getDate () + 365);
        }
      else if (iMode == 2)
        {
          expDate.setHours (expDate.getHours () + 12);
        }
    document.cookie = sName + '=' + escape (sValue) +
                      '; expires=' + expDate.toGMTString () + '; path=/';
  } // setCookie


function isFeatureSupported (sFeature)
  {
    return (window.focus) ?   // secure (?) check for JS 1.1
                              // can safely use 'typeof'
             eval ('typeof (' + sFeature + ') != "undefined"') :
             false;           // JS 1.0 supports nothing questionable...
  } // isFeatureSupported


function setFontSizeGrade (vValue)
  {
    if (vValue == null)
      {
        return;
      }

    document.body.style.fontSize = '' + (100 * Math.pow (1.2, vValue)) + '%';
    setCookie ('FontSizeGrade0', vFontSizeGrade, 1);
  } // setFontSizeGrade


function isPlatformOk4PageControls ()
  {
    return isFeatureSupported ('document.body.style.fontSize') &&
           isFeatureSupported ('document.getElementById');
  } // isPlatformOk4PageControls


function propertiesChunk (sName, sValue)
  {
    return '&amp;' + sName + '=' + escape (sValue);
  } // propertiesChunk


function reportBrowserProperties ()
  {
    if (getCookie ('BrowserProperties'))
      {
        if ((vFontSizeGrade == null) ||
            (vFontSizeGrade == getCookie ('FontSizeGrade1')))
          {
            return;                              // avoid repeated reporting
          }
      }

    document.cookie = 'BrowserProperties=T; path=/';
    var sProperties = 'c=' + ((document.cookie) ? 'Y' : 'N');

    sProperties +=
      propertiesChunk ('tz', (new Date ()).getTimezoneOffset () / 60 * (-1));

    if ((navigator.appVersion.indexOf ('MSIE 5') >= 0 ||
         navigator.appVersion.indexOf ('MSIE 6') >= 0 ||
         navigator.appVersion.indexOf ('MSIE 7') >= 0) &&
        (navigator.userAgent.indexOf ('Opera') < 0))
      {
        document.write ('<span style="behavior: url(#default#clientCaps)"' +
                        ' id="client-caps"></span>');
        var oClientCaps = document.all.item ("client-caps");
        var sFlashVersion =
          oClientCaps.getComponentVersion
            ('{D27CDB6E-AE6D-11CF-96B8-444553540000}', 'ComponentID');
        sProperties += propertiesChunk ('flash', sFlashVersion);
        sProperties += propertiesChunk ('conn', oClientCaps.connectionType);
      }

    if (isFeatureSupported ('navigator.javaEnabled'))
      {
        sProperties +=
          propertiesChunk ('java', (navigator.javaEnabled () ? 'Y' : 'N'));
      }

    if (isFeatureSupported ('screen.width'))
      {
        sProperties += propertiesChunk ('sw', screen.width);
      }
    if (isFeatureSupported ('screen.height'))
      {
        sProperties += propertiesChunk ('sh', screen.height);
      }

    if (isFeatureSupported ('screen.colorDepth'))
      {
        sProperties += propertiesChunk ('cld', screen.colorDepth);
      }
    if (isFeatureSupported ('screen.pixelDepth'))
      {
        sProperties += propertiesChunk ('pxd', screen.pixelDepth);
      }

    if (isFeatureSupported ('screen.deviceXDPI'))
      {
        sProperties += propertiesChunk ('dxdpi', screen.deviceXDPI);
        sProperties += propertiesChunk ('dydpi', screen.deviceYDPI);
      }
    if (isFeatureSupported ('screen.logicalXDPI'))
      {
        sProperties += propertiesChunk ('lxdpi', screen.logicalXDPI);
        sProperties += propertiesChunk ('lydpi', screen.logicalYDPI);
      }

    // see http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=16
    if (isFeatureSupported ('window.innerWidth'))
        {
          sProperties += propertiesChunk ('wiw', window.innerWidth);
          sProperties += propertiesChunk ('wih', window.innerHeight);
        }
      else if (isFeatureSupported ('document.documentElement.clientWidth') &&
               (document.documentElement.clientWidth))
        {
          sProperties +=
            propertiesChunk ('wiw', document.documentElement.clientWidth);
          sProperties +=
            propertiesChunk ('wih', document.documentElement.clientHeight);
        }
      else if (isFeatureSupported ('document.body.clientWidth') &&
               (document.body.clientWidth))
        {
          sProperties += propertiesChunk ('wiw', document.body.clientWidth);
          sProperties += propertiesChunk ('wih', document.body.clientHeight);
        }

    sProperties += propertiesChunk ('nan', navigator.appName);
    sProperties += propertiesChunk ('nacn', navigator.appCodeName);
    sProperties += propertiesChunk ('nav', navigator.appVersion);
    if (isFeatureSupported ('navigator.cpuClass'))
      {
        sProperties += propertiesChunk ('cpu', navigator.cpuClass);
      }
    if (isFeatureSupported ('navigator.platform'))
      {
        sProperties += propertiesChunk ('plat', navigator.platform);
      }
    if (isFeatureSupported ('navigator.systemLanguage'))
      {
        sProperties += propertiesChunk ('slang', navigator.systemLanguage);
      }
    if (isFeatureSupported ('navigator.userLanguage'))
      {
        sProperties += propertiesChunk ('ulang', navigator.userLanguage);
      }

    if (vFontSizeGrade != null)
      {
        sProperties += propertiesChunk ('fsg', vFontSizeGrade);
        setCookie ('FontSizeGrade1', vFontSizeGrade, 1);
      }

    document.write ('<img src="/img/transparent1x1.gif?' + sProperties +
                    '" width="1" height="1">');

    setCookie ('BrowserProperties', 'R', 2);
  } // reportBrowserProperties


// --- external use functions -----------------------------------------------

function setupFontSize ()
  {
    if (isPlatformOk4PageControls ())
      {
        vFontSizeGrade = getCookie ('FontSizeGrade0');
        if (vFontSizeGrade != null)
          {
            setFontSizeGrade (vFontSizeGrade);
          }
      }

    reportBrowserProperties ();             // a good place to do it...
  } // setupFontSize


function showPageControls (sSpecificControlsCode, sRootPfx)
  {
    if (!isPlatformOk4PageControls ())
      {
        return;
      }
    var sControlsCode =
      (sSpecificControlsCode != null) ?
        sSpecificControlsCode :
        '<img src="' + sRootPfx + 'img/zoom-in.gif" width="16" ' +
          'height="16" alt="Увеличить размер шрифта" class="hand" ' +
          'title="Увеличить размер шрифта" ' +
          'onclick="increaseFontSize ();"> <img src="' + sRootPfx +
          'img/zoom-out.gif" width="16" height="16" ' +
          'alt="Уменьшить размер шрифта" title="Уменьшить размер шрифта" class="hand" ' +
          'onclick="decreaseFontSize ();"> <a href="' + sRootPfx +
          'ru/help.html" title="Подсказка"><img src="' + sRootPfx +
          'img/help.gif" width="16" height="16" alt="Подсказка"></a>';
    document.write ('<div id="page-controls">' + sControlsCode + '</div>');
  } // showPageControls


function decreaseFontSize (sName, sValue)
  {
    setFontSizeGrade (--vFontSizeGrade);
  } // decreaseFontSize


function increaseFontSize (sName, sValue)
  {
    setFontSizeGrade (++vFontSizeGrade);
  } // increaseFontSize


function highlightAnchorRaw ()
  {
    if (!(document.getElementById))
      {
        return; // won't mess with ancient browsers...
      }
    var hash = window.location.hash;
    if (hash.length <= 1)
      {
        return;
      }
    var targetName = hash.substring (1);
    var targetAnchor = document.getElementById (targetName);
    if (!targetAnchor)
      {
        return; // might be a hand-crafted URL...
      }
    var targetTr = targetAnchor.parentNode.parentNode;
    targetTr.style.backgroundColor = '#FFFF00';
} // onPageLoad
